My head is smoking from (stupid) tries of using JOIN
, WITH
and GROUP BY
to come up with a solution for my pretty common scenario - I just can't wrap my head around it. Let me throw the example at you right away:
I have two tables (ColorCount and Colorname):
ColorCount:
ColorID Count Date
1 42 2010-09-07
1 1 2010-09-08
2 22 2010-09-14
1 20 2010-10-10
3 4 2010-10-14
ColorName:
ColorID Name
1 Purple
2 Green
3 Yellow
4 Red
Now all I want is to join the ColorName table to the ColorCount table, sum up all the counts of colors per month and calculate the percentage of each count from the the monthly total. Tables are better than words:
Output:
Month Color Count Percentage
09 Purple 43 66%
09 Green 22 33%
09 Yellow 0 0%
09 Red 0 0%
10 Purple 20 83%
10 Green 0 0%
10 Yellow 4 16%
10 Red 0 0%
(Please note the total Count of Month 09
is 65
, hence the 66%
for Purple
and also the 0
's for non-existing colors):
I hope somebody dreams in SQL and this is an easy task...