Display table in different format
i have the sql code that returns the result
Set1 Value
A A1
A A2
A A3
A开发者_Go百科 A4
How do i display the result like this
Set1 Value1 Value2 Value3 Value4
A A1 A2 A3 A4
Thanks
take a look at this
SQL Server: Examples of PIVOTing String data
Following would do the trick. Using ROW_NUMBER
allows for values other than A1
-A4
SQL Statement
SELECT [Set1] = Set1
, [Value1] = MIN(CASE WHEN rn = 1 THEN Value END)
, [Value2] = MIN(CASE WHEN rn = 2 THEN Value END)
, [Value3] = MIN(CASE WHEN rn = 3 THEN Value END)
, [Value4] = MIN(CASE WHEN rn = 4 THEN Value END)
FROM (
SELECT Set1
, Value
, rn = ROW_NUMBER() OVER (PARTITION BY Set1 ORDER BY Value)
FROM AResult
) q
GROUP BY
Set1
精彩评论