I have a mock up of a sql query that will represent a real sql query.
Create table #tmp ( Atype varchar(10), Btype varchar(10) ) insert into #tmp values ('a','x') insert into #tmp values ('b','x') insert into #tmp values ('a','y') insert into #tmp values ('a','y') insert into #tmp values ('b','z') insert into #tmp values ('b','y') select atype, btype,count(*) as count from #tmp group by atype, btype order by atype drop table #tmp
This will give me the result of:
atype btype count ----------------- a x 1 a y 2 b x 1 b y 1 b z 1
What i am looking for after this is to be able to create a report that basically looks like:
atype|x| y| z ------------- a |1| 2| 0 b |1| 1| 1
I am sure you can do this using come crazy t-sql code but i am struggeling to get it right.
EDIT:
I agree that you can use the TSQL PIVOT command but what happens when Btype is variable. Meaning that i dont know how many types there will be?
1 Answer