December 24, 2011 at 2:18 pm
Hello,
My query is:
select ID, Size from TableName
The results are:
And I need the output to be like this:
I would prefer doing this without making any temporary tables.
I appreciate any help. Thanks!
December 24, 2011 at 2:37 pm
December 24, 2011 at 3:33 pm
I'd rather use the good old CrossTab approach as described in the related link in my signature.
It'd not only easier to remember but usually also faster than the PIVOT.
December 27, 2011 at 7:09 pm
I second LutzM's crosstab suggestion as I find it easier than pivot as well. One tip for after you read the article he suggested if you go the crosstab route: Use MAX(case when ...) instead of SUM(case when ...).
Good luck and nice problem.
December 27, 2011 at 9:01 pm
select ID,
(case when [Large] > 0 then 'Yes' else 'No' end)[Large],
(case when [Small] > 0 then 'Yes' else 'No' end)[Small]
from ##TableName a
pivot
(
count(Size)
for Size in ([Large], [Small])
) b
"Often speak with code not with word,
A simple solution for a simple question"
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply