SQL monthly join and monthly total percentage

Unjumbjaffoff

New Member
My head is smoking from (stupid) tries of using \[code\]JOIN\[/code\], \[code\]WITH\[/code\] and \[code\]GROUP BY\[/code\] 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):\[code\]ColorCount:ColorID Count Date1 42 2010-09-071 1 2010-09-082 22 2010-09-141 20 2010-10-103 4 2010-10-14\[/code\]\[code\]ColorName:ColorID Name1 Purple2 Green3 Yellow4 Red\[/code\]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:\[code\]Output:Month Color Count Percentage09 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%\[/code\](Please note the total Count of Month \[code\]09\[/code\] is \[code\]65\[/code\], hence the \[code\]66%\[/code\] for \[code\]Purple\[/code\] and also the \[code\]0\[/code\]'s for non-existing colors):I hope somebody dreams in SQL and this is an easy task...
 
Back
Top