以不同的格式显示表格

时间:2011-07-06 03:14:27

标签: sql-server

我有返回结果的SQL代码

Set1   Value
A       A1      
A       A2
A       A3
A       A4

如何显示结果

Set1    Value1      Value2       Value3      Value4
A           A1        A2           A3          A4

由于

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

以下会做到这一点。使用ROW_NUMBER可以使用A1 - A4

以外的值

SQL语句

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