Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 ROW TO COLUMN

Author  Topic 

shamasm
Starting Member

11 Posts

Posted - 2011-08-22 : 05:58:47
HI ALL,
I need to convert Destination Row to Column and the UserCount as the values for the column ..

SELECT
RefDestinationID ,
COUNT(RefUserID) AS UserCount
FROM UserDestinationMap
GROUP BY RefDestinationID
RefDestinationID UserCount
----------- ------------------
1 2
2 1
3 2
4 1
5 1
6 1
7 1
8 3

this is the o/p of the query .. i need like this
1 2 3
-- -- ----
2 1 2

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2011-08-22 : 06:16:01
select [1], [2], [3], [4], [5], [6], [7],
from (select RefDestinationID, UserCount from @t) p1
pivot
(max(UserCount) for RefDestinationID in ([1], [2], [3], [4], [5], [6], [7], )) p2

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-22 : 06:31:03
For dynamic columns use PIVOT as suggested below

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -