SQL查询一行中的所有记录

时间:2013-04-30 08:51:17

标签: sql sql-server

我有像

这样的数据表
CANDID  QID   OptID
11      56    116
11      56    117
11      57    118
11      57    119
11      60    124
11      60    125
11      60    126
11      62    129
11      62    130
11      62    131

如何显示此数据,如

CANDID   QID    OptID1   OptID2   OptID3   OptID4 
11       56     116      117      null     null
....
....

11       60     124      125      126      null

我试过了,但这不是我想要的输出。

SELECT CANDID,
       QID,
       MIN(OptID) AS OptID1,
       MAX(OptID) AS OptID4
FROM   #TEMP1
GROUP  BY CANDID,
          QID 

2 个答案:

答案 0 :(得分:1)

CREATE TABLE #TEMP1
    ([CANDID] int, [QID] int, [OptID] int)
;

INSERT INTO #TEMP1
    ([CANDID], [QID], [OptID])
VALUES
    (11, 56, 116),
    (11, 56, 117),
    (11, 57, 118),
    (11, 57, 119),
    (11, 60, 124),
    (11, 60, 125),
    (11, 60, 126),
    (11, 62, 129),
    (11, 62, 130),
    (11, 62, 131)
;

WITH T
     AS (SELECT [CANDID], 
                [QID], 
                [OptID],
                ROW_NUMBER() OVER (PARTITION BY [CANDID], [QID] ORDER BY [OptID]) AS RN
         FROM   #TEMP1)
SELECT CANDID,
       QID,
       [1] AS OptID1,
       [2] AS OptID2,
       [3] AS OptID3,
       [4] AS OptID4
FROM   T PIVOT (MAX([OptID]) FOR RN IN ([1], [2], [3], [4])) AS P 


DROP TABLE #TEMP1

返回

CANDID      QID         OptID1      OptID2      OptID3      OptID4
----------- ----------- ----------- ----------- ----------- -----------
11          56          116         117         NULL        NULL
11          57          118         119         NULL        NULL
11          60          124         125         126         NULL
11          62          129         130         131         NULL

答案 1 :(得分:0)

这就是你要找的东西:

DECLARE @cols  AS NVARCHAR(MAX),
        @query AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.OptID) 
            FROM #TEMP1 c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')        

SET @query = 'SELECT CANDID, QID, ' + @cols + ' from 
            (
                select CANDID
                    , QID
                    , OptID
                from #TEMP1
           ) x
            pivot 
            (
                 AVG(OptID)
                for OptID in (' + @cols + ')
            ) p '

EXECUTE(@query) 

返回

CANDID      QID         116         117         118         119         124         125         126         129         130         131
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
11          56          116         117         NULL        NULL        NULL        NULL        NULL        NULL        NULL        NULL
11          57          NULL        NULL        118         119         NULL        NULL        NULL        NULL        NULL        NULL
11          60          NULL        NULL        NULL        NULL        124         125         126         NULL        NULL        NULL
11          62          NULL        NULL        NULL        NULL        NULL        NULL        NULL        129         130         131