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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Reformat Data?

Author  Topic 

mitin
Yak Posting Veteran

81 Posts

Posted - 2013-03-06 : 12:16:22
Hi, I have data that is loaded into a table automatically, in this table it is in the following format:



Server Metric TS Value
a 1 1pm 50
a 2 1pm 0
a 3 1pm 9
a 4 1pm 81



I want to know how I can get the data from this table, into another table in the following format:


Server M1 M2 M3 M4 TimeStamp
a 50 0 9 81



Really hope this is possible, how can it be acieved, anyone got a quer for this? seems like it would be difficult to me but... hopefully I will be surprised :)

Thanks guys!! :)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-06 : 12:23:50
[code]
SELECT Server,
MAX(CASE WHEN Metric=1 THEN Value END) AS M1,
MAX(CASE WHEN Metric=2 THEN Value END) AS M2,
MAX(CASE WHEN Metric=3 THEN Value END) AS M3,
MAX(CASE WHEN Metric=4 THEN Value END) AS M4,
GETDATE() AS Timestamp
FROM Table
GROUP BY Server
[/code]

I hope MEtric will always have values as 1 to 4

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

Go to Top of Page
   

- Advertisement -