My head smokes from (stupid) attempts to use it JOIN, WITHand GROUP BYto come up with a solution for my fairly common scenario, I just can't wrap my head around me. Let me give you an example 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 to do is join the ColorName table in the ColorCount table, summarize all the color counts per month and calculate the percentage of each count of the total amount. 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 that the total number of months 09is equal 65, therefore 66%for Purple, as well as 0for non-existent colors):
I hope someone dreams in SQL, and this is not an easy task ...