views:

49

answers:

3
+3  A: 

What you're looking for is Pivot

David Hedlund
+1  A: 

You'll want to look at the PIVOT operation:

http://www.tsqltutorials.com/pivot.php

John Boker
+1  A: 

You need to use pivot. One issue that may give you problems is that you must know how many categories you have since a query can't ever return a dynamic number of columns (not even one that you're pivoting).

I don't have your schema, but this should be about right (may take a few touchups here and there, but it should get you started)...

select
  Date, [1] as Cat1, [2] as Cat2, [3] as Cat3, ...
from
  (select date, category from table) p
pivot (
  count(*)
for
  Category in ([1],[2],[3],...)
) pivoted
order by
  pivoted.date
Donnie
Thanks for the example, it was quite helpful, and I learned something new! :) Pivot works for about 2/3 of our cases, since they have standard categories - however, for the rest of them they are dynamic, so this is a no-go.BUT, we're able to get them all the data they want by using WITH ROLLUP. It's not as pretty as we'd have liked, but it definitely works.
Zind