Alan Alan - 1 month ago 7
MySQL Question

Pivot style calculation on multiple columns

I have a MySQL table with a team for a user and for each user there is a player1, player2 & player3 field.

I am trying to calculate a total of a chosen player from all users across as 3 player columns..

I.e.

user1 can pick playerA, playerC & playerG.

user2 can pick playerE, playerF & playerH.

user3 can pick playerB, playerA & playerF.

user4 can pick playerC, playerE & playerA.

I can run a statement against each player column i.e.

SELECT player1, count(*) as total from users;
SELECT player2, count(*) as total from users;
SELECT player3, count(*) as total from users;


The problem is I need an all encompassing result-set that will show me the total of all the players selected so I would have something like below

player total
playerA 3
playerB 1
playerC 2
playerE 2
playerF 2
playerG 1
playerH 1


My table structure is below

CREATE TABLE `leaderboard_api` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`competition_id` char(3) DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`position` int(4) DEFAULT NULL,
`name` varchar(128) DEFAULT NULL,
`player1_name` varchar(128) DEFAULT NULL,
`player1_score` tinyint(11) DEFAULT NULL,
`player2_name` varchar(128) DEFAULT NULL,
`player2_score` tinyint(11) DEFAULT NULL,
`player3_name` varchar(128) DEFAULT NULL,
`player3_score` tinyint(11) DEFAULT NULL,
`total_score` tinyint(11) DEFAULT NULL,
`missed_cut` tinyint(1) DEFAULT NULL,
`withdrawn` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
);


When I run the following

select player1_name, count(player1_name) as total from leaderboard_api where year = '2015' and competition_id = '014' group by player1_name order by total desc;


I get a list of players and their respective count. I can then run this for player2_name and player3_name respectively and get their totals too.

The problem I am having is added each players total from each query to another to get a grand total. I have run each separately and getting the values as expected.

I tried the solution below from @sagi but it's not counting the sum of all 3 queries and just giving me 1 as a total.

SELECT t.player,count(*) FROM (
SELECT player1 as player FROM `users` UNION ALL
SELECT player2 FROM `users` UNION ALL
SELECT player3 FROM `users` UNION ALL
....) t
GROUP BY t.player


This is the top 100 using query

select id, player1_name, player2_name, player3_name from leaderboard_api limit 100;


Output:

1 Donald, Luke Toms, David Bradley, Keegan
2 Stricker, Steve Bradley, Keegan Watson, Bubba
3 Choi, KJ Westwood, Lee Bradley, Keegan
4 Scott, Adam Stricker, Steve Immelman, Trevor
5 Stricker, Steve Garcia, Sergio Haas, Bill
6 Fowler, Rickie Schwartzel, Charl Bradley, Keegan
7 Watney, Nick Stricker, Steve Levin, Spencer
8 Stricker, Steve Johnson, Zach Bradley, Keegan
9 Donald, Luke Points, DA Fowler, Rickie
10 Westwood, Lee Donald, Luke Barnes, Ricky
11 Schwartzel, Charl Choi, KJ Haas, Bill
12 Woodland, Gary Choi, KJ Garcia, Sergio
13 Donald, Luke Stricker, Steve Molder, Bryce
14 Garcia, Sergio Kuchar, Matt Furyk, Jim
15 Noh, Seung-Yul Kuchar, Matt Watney, Nick
16 Donald, Luke Mickelson, Phil Barnes, Ricky
17 Scott, Adam Fowler, Rickie Noren, Alexander
18 Johnson, Zach Molinari, Francesco Toms, David
19 Choi, KJ Westwood, Lee Poulter, Ian
20 Mcilroy, Rory Points, DA Westwood, Lee
21 McIlroy, Rory Donald, Luke Na, Kevin
22 Choi, KJ Scott, Adam Dyson, Simon
23 Fowler, Rickie Johnson, Zach Bradley, Keegan
24 Mcilroy, Rory Scott, Adam Senden, John
25 Mickelson, Phil Kuchar, Matt Barnes, Ricky
26 Jimenez, Miguel Angel Mickelson, Phil Westwood, Lee
27 Donald, Luke Mcilroy, Rory Palmer, Ryan
28 Mickelson, Phil Choi, KJ Poulter, Ian
29 Mcilroy, Rory Mickelson, Phil Points, DA
30 Johnson, Zach Stricker, Steve Molinari, Francesco
31 Toms, David Love, Davis Mickelson, Phil
32 McIlroy, Rory Westwood, Lee Wilson, Mark
33 Mcilroy, Rory Garcia, Sergio Immelman, Trevor
34 Garcia, Sergio Love, Davis Westwood, Lee
35 Donald, Luke Stricker, Steve Sabbatini, Rory
36 Mcilroy, Rory Karlsson, Robert Romero, Andres
37 Choi, KJ Yang, Y.E Toms, David
38 Mcilroy, Rory Watney, Nick Kirk, Chris
39 Mcilroy, Rory Kuchar, Matt Noren, Alexander
40 Scott, Adam Yang, Y.E Romero, Andres
41 Donald, Luke Fowler, Rickie Jimenez, Miguel Angel
42 Donald, Luke Jimenez, Miguel Angel Fowler, Rickie
43 Donald, Luke Love, Davis Choi, KJ
44 Mcilroy, Rory Westwood, Lee Barnes, Ricky
45 Donald, Luke Fowler, Rickie Molinari, Edoardo
46 Garcia, Sergio Choi, KJ Yang, Y.E
47 Choi, KJ Fowler, Rickie Dyson, Simon
48 Fowler, Rickie Molinari, Francesco Johnson, Zach
49 Stricker, Steve Choi, KJ Casey, Paul
50 McIlroy, Rory Choi, KJ Molinari, Francesco
51 Fowler, Rickie Jimenez, Miguel Angel Mickelson, Phil
52 Mcilroy, Rory Garcia, Sergio Kim, Kyung-Tae
53 Mcilroy, Rory Garcia, Sergio Kim, Kyung-Tae
54 Mcilroy, Rory Mickelson, Phil Barnes, Ricky
55 Mcilroy, Rory Mickelson, Phil Barnes, Ricky
56 Fowler, Rickie Mcilroy, Rory Wilson, Mark
57 Fowler, Rickie Love, Davis Watson, Bubba
58 Fowler, Rickie Harrington, Padraig Poulter, Ian
59 McIlroy, Rory Mickelson, Phil Jimenez, Miguel Angel
60 Mcilroy, Rory Fowler, Rickie Jimenez, Miguel Angel
61 Day, Jason Johnson, Zach Bradley, Keegan
62 Stricker, Steve Overton, Jeff Mickelson, Phil
63 Day, Jason Furyk, Jim Toms, David
64 Donald, Luke Westwood, Lee Gay, Brian
65 Stricker, Steve Day, Jason Kim, Kyung-Tae
66 Mahan, Hunter Stricker, Steve Van Pelt, Bo
67 Day, Jason Scott, Adam Barnes, Ricky
68 Day, Jason Johnson, Zach Love, Davis
69 Fowler, Rickie Jacobson, Fredrik Bradley, Keegan
70 Karlsson, Robert Watney, Nick Quiros, Alvaro
71 Schwartzel, Charl Johnson, Dustin Love, Davis
72 Mcilroy, Rory Scott, Adam Slocum, Heath
73 Day, Jason Johnson, Zach Dyson, Simon
74 Day, Jason Johnson, Zach Dyson, Simon
75 Day, Jason Mcilroy, Rory Garrigus, Robert
76 Donald, Luke Mcilroy, Rory Byrd, Jonathan
77 Baddeley, Aaron Garcia, Sergio Toms, David
78 Day, Jason Kuchar, Matt Sabbatini, Rory
79 Johnson, Dustin Choi, KJ Fisher, Ross
80 Simpson, Webb Stricker, Steve Yang, Y.E
81 Day, Jason Mickelson, Phil Barnes, Ricky
82 Johnson, Zach Howell, Charles Snedeker, Brandt
83 Donald, Luke Mcilroy, Rory Stallings, Scott
84 Kim, Anthony Mickelson, Phil Rose, Justin
85 Kaymer, Martin Marino, Steve Westwood, Lee
86 Mahan, Hunter Kaymer, Martin Poulter, Ian
87 Mcilroy, Rory Johnson, Dustin Jimenez, Miguel Angel
88 Mcilroy, Rory Johnson, Dustin Jimenez, Miguel Angel
89 Laird, Martin Scott, Adam Snedeker, Brandt
90 Johnson, Dustin Johnson, Zach Manassero, Matteo
91 Day, Jason Watney, Nick Tringale, Cameron
92 Garcia, Sergio Day, Jason Quiros, Alvaro
93 Day, Jason Moore, Ryan Toms, David
94 Day, Jason Mcilroy, Rory Barnes, Ricky
95 Baddeley, Aaron Mcilroy, Rory Yang, Y.E
96 Westwood, Lee Jacobson, Fredrik Kim, Kyung-Tae
97 Day, Jason Mcilroy, Rory Molinari, Edoardo
98 Day, Jason Garcia, Sergio Cink, Stewart
99 Bjorn, Thomas Scott, Adam Woods, Tiger
100 Day, Jason Johnson, Dustin Levin, Spencer

Answer

After getting started on a solution from @sagi above I eventually worked it out, the counts were missing from the sub table if that's the correct language.

Here's my solution for those that might need it...

select t.player, sum(count) as total from (
select player1_name as player, count(player1_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player1_name
UNION
select player2_name, count(player2_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player2_name
UNION
select player3_name, count(player3_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player3_name)
t group by t.player order by total desc
Comments