MSSQL:选择值作为行

时间:2018-11-01 07:25:40

标签: sql sql-server sql-server-2008

我的桌子后面跟着

UserId  Value
1        V1
1        V2
2        V3
2        V4
3        V5
3        V6
1        V7
4        V8
1        V9
2        V10
4        V11
1        V10
3        V10

我需要选择查询以获取以下结果

UserId  Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8
1        V1   V2   V7   V9  V10
2        V3   V4   V10
3        V5   V6   V10
4        V8   V11

我尝试使用分区,但无法输出 预先感谢

3 个答案:

答案 0 :(得分:0)

您可以尝试使用PIVOT

select 
   userid,max(C1) as C1,max(C2) as C2,max(C3) as C3,
   max(C4) as C4,max(C5) as C5,max(C6) as C6 from
   (
     select userid, concat('C',cast(a.id as varchar(10))) as cval,
     value 
     from table1 a inner join table2 b on a.id=bTbl1_ID
   )X
   pivot(select max(value) for cval in ([C1],[C2],[C3],[C4],[C5],[C6])) as pv
 group by userid

答案 1 :(得分:0)

您可以尝试此查询

DECLARE @TempTable TABLE (UserId INT,  Value VARCHAR(10))
INSERT INTO @TempTable VALUES (1 , 'V1'), (1 , 'V2'), (2 , 'V3'), (2 , 'V4'), (3 , 'V5'), (3 , 'V6'), (1 , 'V7'), (4 , 'V8'), (1 , 'V9'), (2 , 'V10'), (4 , 'V11'), (1 , 'V10'), (3 , 'V10')

SELECT UserId, [1] AS Col1, [2] AS Col2 ,[3] AS Col3, [4] AS Col4, [5] AS Col5, [6] AS Col6, [7] AS Col7, [8] AS Col8 
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY UserId ORDER BY (SELECT NULL) ) AS RN 
    FROM @TempTable
) AS SRC
PIVOT(MAX(Value) FOR RN IN ([1],[2],[3],[4],[5],[6],[7],[8])) PVT

结果:

UserId      Col1       Col2       Col3       Col4       Col5       Col6       Col7       Col8
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1           V1         V2         V7         V9         V10        NULL       NULL       NULL
2           V10        V3         V4         NULL       NULL       NULL       NULL       NULL
3           V5         V6         V10        NULL       NULL       NULL       NULL       NULL
4           V11        V8         NULL       NULL       NULL       NULL       NULL       NULL

答案 2 :(得分:0)

使用“交叉表”技术转换的另一种语法,我发现它更易读。

DECLARE @TempTable TABLE (UserId INT,Value VARCHAR(10))
INSERT INTO @TempTable VALUES (1,'V1'),(1,'V2'),(2,'V3'),(2,'V4'),(3,'V5'),(3,'V6'),(1,'V7'),(4,'V8'),(1,'V9'),(2,'V10'),(4,'V11'),(1,'V10'),(3,'V10')

SELECT  SQ1.UserID,
        MAX(CASE WHEN RN = 1 THEN SQ1.Value END) AS Col1,
        MAX(CASE WHEN RN = 2 THEN SQ1.Value END) AS Col2,
        MAX(CASE WHEN RN = 3 THEN SQ1.Value END) AS Col3,
        MAX(CASE WHEN RN = 4 THEN SQ1.Value END) AS Col4,
        MAX(CASE WHEN RN = 5 THEN SQ1.Value END) AS Col5,
        MAX(CASE WHEN RN = 6 THEN SQ1.Value END) AS Col6,
        MAX(CASE WHEN RN = 7 THEN SQ1.Value END) AS Col7,
        MAX(CASE WHEN RN = 8 THEN SQ1.Value END) AS Col8
FROM    (
            SELECT  ROW_NUMBER() OVER(PARTITION BY TT.UserId ORDER BY CA1.ValNum ASC ) AS RN , TT.UserID, TT.Value
            FROM    @TempTable AS TT
            CROSS
            APPLY   (SELECT CAST(SUBSTRING(TT.Value,2,LEN(TT.Value)-1) AS INT)) AS CA1(ValNum)
        ) AS SQ1
GROUP   BY SQ1.UserId;

更多阅读内容:

posts