Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi All,My first question on here, should be quite a simple one but I can't seem to be able to search through to find my answer. I have a data set, looks like this..Date Hour Value1/1/2012 1 501/1/2012 2 701/3/2012 1 60And so on...I would like for it to display the data asDate Hour 1 2 31/1/2012 50 70 n/a1/3/2012 60 n/a n/aWhat is the easiest way of doing this?Thank you!
I have attempted to do this and it doesn't work, as alternate suggestion.SELECT Value, Hour, Datefrom(select*, row_number over (partition by Hour order by Date) RowNumber from MyTable) as ppivot(min(Date) for Hour in (Value, Hour, Date))as torder by RowNumber
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2012-03-05 : 15:26:41
Maybe this will get you going:
DECLARE @Foo TABLE ([Date] DATE, [Hour] TINYINT, Value INT)INSERT @Foo VALUES('1/1/2012', 1, 50),('1/1/2012', 2, 70),('1/3/2012', 1, 60)SELECT [Date], [1], [2], [3]-- ....FROM ( SELECT [Date], [Hour], Value FROM @Foo AS F) AS SourceTablePIVOT( MAX(Value) FOR [Hour] IN ([1], [2], [3]) -- etc) AS PivotTable