I am trying to make a pivot table with rollup and a case statement in the grouping function.When the data in the column section of the pivot does not need to be altered my query works great,
his is an example.
http://sqlfiddle.com/#!3/3143e/2
But when I need to have a case statement in the column section it messes it up. For example One of the columns needs to return
“1 of 3” if someone voted in one of three elections,
“2 of 3” if someone voted in two of three elections,
“3 of 3” if someone voted in three of three elections,
This is the case statement
CAST( (CASE WHEN election1 IS NOT NULL THEN 1 ELSE 0 END)
+(CASE WHEN election2 IS NOT NULL THEN 1 ELSE 0 END)
+(CASE WHEN election3 IS NOT NULL THEN 1 ELSE 0 END) AS CHAR(1)) + '-3' as election
I need help figuring out how to put this case statement in the Grouping() function or maybe there is a different way to do this. I tried something like this
http://sqlfiddle.com/#!3/3143e/3
but of course it didn’t work.
This is what the final pivot should look like
1 http://img842.imageshack.us/img842/6884/84834931.jpg
Thanks in advance
So i have a suggestion for you. I did it in another way but it works.
Test data
A
PIVOTwith aUNION ALL