SQL查询以基于列值重复显示列

时间:2019-05-08 08:09:55

标签: sql sql-server

我的桌子是:-

DECLARE @Tab TABLE(VId INT,QuestionNoId INT,AnswerType VARCHAR(20),Answer VARCHAR(100))
INSERT INTO @Tab(Vid,   QuestionNoId,   AnswerType, Answer)VALUES
(2370,  1,  'Text', '1'),
(2370,  1,  'Per',  '78'),
(2370,  1,  'Date', '04/05/2019'),
(2370,  2,  'Text', '1'),
(2370,  3,  'Text', '2'),
(2370,  4,  'YesNo','D'),
(2370,  5,  'Text', '68'),
(2370,  6,  'Text', '44'),
(2370,  6,  'Date', '05/05/2019'),
(2370,  7,  'YesNo','Y'),
(2370,  8,  'YesNo','N'),
(2370,  9,  'YesNo', 'Y')

select * from @Tab

我想显示基于我的QuestionNoId的结果查询,应基于QuestionNoId增加列数。如果Question3Id重复3次,则应创建3列,如果为1,则仅创建一列。

在下面找到我想要的输出:-

DECLARE @Tab1 TABLE(VId INT,QuestionNoId INT,AnswerType VARCHAR(20),Answer VARCHAR(100),AnswerType1 VARCHAR(20),Answer1 VARCHAR(100),AnswerType2 VARCHAR(20),Answer2 VARCHAR(100))
INSERT INTO @Tab1(Vid,  QuestionNoId,   AnswerType, Answer, AnswerType1,    Answer1,    AnswerType2,    Answer2)VALUES
(2370,  1,  'Text', '1','Per',  '78','Date',    '04/05/2019'),
(2370,  2,  'Text', '1',NULL,NULL,NULL,NULL),
(2370,  3,  'Text', '2',NULL,NULL,NULL,NULL),
(2370,  4,  'YesNo','D',NULL,NULL,NULL,NULL),
(2370,  5,  'Text', '68',NULL,NULL,NULL,NULL),
(2370,  6,  'Text', '44','Date','05/05/2019',NULL,NULL),
(2370,  7,  'YesNo','Y',NULL,NULL,NULL,NULL),
(2370,  8,  'YesNo','N',NULL,NULL,NULL,NULL),
(2370,  9,  'YesNo','Y',NULL,NULL,NULL,NULL)

select * from @Tab1

谢谢

1 个答案:

答案 0 :(得分:1)

SQL Server PIVOT是您在Using PIVOT and UNPIVOT的朋友:

DECLARE @Tab TABLE(VId INT,QuestionNoId INT,AnswerType VARCHAR(20),Answer VARCHAR(100))
INSERT INTO @Tab(Vid,   QuestionNoId,   AnswerType, Answer)VALUES
(2370,  1,  'Text', '1'),
(2370,  1,  'Per',  '78'),
(2370,  1,  'Date', '04/05/2019'),
(2370,  2,  'Text', '1'),
(2370,  3,  'Text', '2'),
(2370,  4,  'YesNo','D'),
(2370,  5,  'Text', '68'),
(2370,  6,  'Text', '44'),
(2370,  6,  'Date', '05/05/2019'),
(2370,  7,  'YesNo','Y'),
(2370,  8,  'YesNo','N'),
(2370,  9,  'YesNo', 'Y')


;with AnswerType as (
    select Vid, QuestionNoId, max([1]) as AnswerType1, max([2]) as AnswerType2, max([3]) as AnswerType3, max([4]) as AnswerType4
    from (
        select *, ROW_NUMBER() OVER (PARTITION BY QuestionNoId ORDER BY QuestionNoId) AS qrc
        from @Tab
    ) as src1
    pivot (
        max(AnswerType)
        for qrc in ([1],[2],[3],[4] /*Max number of expected answers*/)
    ) as pvt1
    group by Vid, QuestionNoId
)
,Answer as (
    select Vid, QuestionNoId, max([1]) as Answer1, max([2]) as Answer2, max([3]) as Answer3, max([4]) as Answer4
    from (
        select *, ROW_NUMBER() OVER (PARTITION BY QuestionNoId ORDER BY QuestionNoId) AS qrc
        from @Tab
    ) as src1
    pivot (
        max(Answer)
        for qrc in ([1],[2],[3],[4] /*Max number of expected answers*/)
    ) as pvt1
    group by Vid, QuestionNoId
)
select t.VId, t.QuestionNoId, t.AnswerType1, a.Answer1, t.AnswerType2, a.Answer2, t.AnswerType3, a.Answer3, t.AnswerType4, a.Answer4
from AnswerType as t
inner join Answer as a
    on t.VId = a.VId
    and t.QuestionNoId = a.QuestionNoId