I’m not an expert at SQL and i’m not even sure if this type of query is doable.
I want to return a count(*) for each “MediaTypeID” for each Month based off of “MediaDate”.
Any help would be greatly appreciated!
Thanks.
My Table looks like:

The Table Data looks like:
1 | 1 | Funny Cat Video | 2006-01-25 00:00:00.000
2 | 1 | Funny Dog Video | 2006-01-20 00:00:00.000
3 | 2 | Angry Birds Game | 2006-03-13 00:00:00.000
4 | 4 | Blonde Joke | 2006-03-16 00:00:00.000
5 | 3 | Goofy Clown Picture | 2006-02-27 00:00:00.000
6 | 2 | Racing Game | 2006-02-10 00:00:00.000
7 | 1 | Star Wars Video | 2006-07-15 00:00:00.000
The query would return 12 rows of results for Jan-Dec looking like:
Month | MediaTypeID1Count | MediaTypeID2Count | MediaTypeID3Count | MediaTypeID4Count
Jan | 400 | 255 | 15 | 65
Feb | 100 | 25 | 75 | 35
Mar | 320 | 155 | 50 | 99
Apr | 56 | 0 | 98 | 313
This type of data transformation is known as a
PIVOT. SQL Server 2005+ has a pivot function that can be implemented:See SQL Fiddle with Demo
If you have an unknown number of values that you want to transpose into columns, then you can use dynamic sql:
See SQL Fiddle with Demo
The result will look like: