April 8, 2009 at 12:51 am
How do I change the row data into column? Refer below:
Results (By Rows)
---------------------------------
UserID param_value EnterDtm
1001 182 2008-02-28
1002 152 2008-03-02
1003 114 2008-03-15
1001 132 2008-04-01
1005 145 2008-04-15
1003 165 2008-04-15
1001 191 2009-01-25
---------------------------------
Expected results (By Columns)
---------------------------------
UserID 2008-02 2008-03 2008-04 2009-01
1001 182 null 132 191
1002 null 152 null null
1003 null 114 165 null
1005 null null 145 null
---------------------------------
my current code is as per below:
WITH Users
AS
(
SELECT ROW_NUMBER() OVER( PARTITION BY UserID, DATEADD( MONTH, DATEDIFF( MONTH, 0, EnterDtm ), 0 ) ORDER BY param_value DESC ) AS RowNumber, *
FROM TABLE_1
)
SELECT UserID, param_value, EnterDtm
FROM Users
WHERE RowNumber = 1
Thanks in advance!
April 8, 2009 at 11:57 am
Hi,
if the columns you want to change your rows to are always the same and you're I'd recommend to use the PIVOT function.
If you need dynamic columns you should have a look at the following article by Jeff Moden that comes really close to your scenario (at least from what I can get out of your sample data):
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply