Here's a SQL Fiddle with the source data, what I've tried so far, and what I expect to get as output. http://sqlfiddle.com/#!18/daf90/2
I have a GameWinnings
table, with rows containing Contestant name, the round he/she played, and the amount won in that round.
I need to produce a report that SUMs up all the winnings per round, by Contestant.
The condition I'm stuck in is that if a Contestant has not played in a particular round, the RoundWinningsAmount
for that round should be NULL
not 0
. I think a CROSS APPLY
or PIVOT
/ UNPIVOT
might do it, haven't been able to nail it down yet.
Here's the table and initial data, followed by Problem statement and expected output vs what I've tried.
CREATE TABLE dbo.GameWinnings
(
Contestant varchar(100) NOT NULL,
GameRound int NOT NULL,
RoundWinningsAmount numeric(38, 6) NULL
);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Ms Junaiqua',2,33333);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Mr Wang',1,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Mr Wang',1,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Mr Wang',1,100);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Mr Wang',2,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',1,99);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',1,1);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',1,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',2,50);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',2,150);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',2,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',2,NULL);
INSERT INTO dbo.GameWinnings (Contestant,GameRound, RoundWinningsAmount)
VALUES('Thad Chad ',3,300);
CREATE TABLE ExpectedOutput
(
Contestant varchar(100) NOT NULL,
Round_1_Winnings numeric(38, 6) NULL,
Round_2_Winnings numeric(38, 6) NULL,
Round_3_Winnings numeric(38, 6) NULL
);
-- Expected output
INSERT INTO ExpectedOutput VALUES ('Mr Wang', 100 , 0 , NULL)
INSERT INTO ExpectedOutput VALUES ('Ms Junaiqua', NULL , 33333 , NULL)
INSERT INTO ExpectedOutput VALUES ('Thad Chad', 100 , 200 , 300)
IF you do a SELECT *
, then this is the Data.
There are 3 Rounds in a Game. So GameRound column value will always be only 1 or 2 or 3.
NULL
, not
0
So in the sample data above
SUM
should be NULL
not 0
. Round 2 SUM
should be 0
since he played in Round 2, but didn't win any money.SUM
should be NULL
not 0
.SUM
value.-- Current query I've tried to get desired output.
-- The query is returning 0, instead of NULL for Rounds where Contestant didn't participate.
-- I know that this is happening because I am returning 0 in the ELSE of the CASE.
-- Not sure how to fix it.
SELECT
Contestant,
SUM ( CASE WHEN GameRound = 1 THEN RoundWinningsAmount ELSE 0 END) Round_1_Winnings,
SUM ( CASE WHEN GameRound = 2 THEN RoundWinningsAmount ELSE 0 END) Round_2_Winnings,
SUM ( CASE WHEN GameRound = 3 THEN RoundWinningsAmount ELSE 0 END) Round_3_Winnings
FROM dbo.GameWinnings
GROUP BY Contestant
The values in Red need to be the values shown in green.
you can use a PIVOT query like below:
see live demo
select * from
(
select
RoundWinnings=ISNULL(sum(RoundWinningsAmount),0),
Contestant,
GameRound
from GameWinnings
group by Contestant, GameRound
)src
pivot
(
max(RoundWinnings)
for GameRound in ([1],[2],[3])
)p
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With