为字段值使用UNPIVOT和列名称

时间:2016-06-08 03:08:51

标签: sql-server-2012

背景是在线评估回复。我必须使用的源数据是每个参与者(测试者)一个记录,超过140列表示每个问题的答案值。列名将对应于特定问题的“ItemCode”。

我想将此数据移至下表

ResponseItems
    ParticipantID
    Form   ( 1 or 2 depending on ParticipantID )
    ItemCode   ( Name of the Column )
    ItemDesc   ( pulled from Codebook on ItemCode and Form
    AnswerValue

我可以用一个大的Union语句攻击它,但它不能扩展到140列宽。

但是作为一个例子,这里的代码适用于前3个项目代码或答案列:

Select ar.ParticipantID, ar.Form, c.ItemCode, c.ItemDesc, ar.SJT_01 as 'AnswerValue'
From dbo.Responses ar
Inner Join dbo.CodeBook c on c.ItemCode = 'SJT_01'
    and c.Form = ar.Form

UNION ALL
Select ar.ParticipantID, ar.Form, c.ItemCode, c.ItemDesc, ar.SJT_02 as 'AnswerValue'
From dbo.Responses ar
Inner Join dbo.CodeBook c on c.ItemCode = 'SJT_02'
    and c.Form = ar.Form

UNION ALL
Select ar.ParticipantID, ar.Form, c.ItemCode, c.ItemDesc, ar.SJT_03 as 'AnswerValue'
From dbo.Responses ar
Inner Join dbo.CodeBook c on c.ItemCode = 'SJT_03'
    and c.Form = ar.Form

如果我有100个测试者和3个项目代码(3列我想要捕获的答案,我正在拍摄100个记录表并创建300条记录。

我研究了UNPIVOT命令,但我似乎无法让它工作。 任何建议都将不胜感激。

Sample source data with 10 test takers

有10名考生的数据......期望的结果是

ParticipantID, Form, ItemCode, AnswerValue

AICPAPSS003    1    SJT_01   5
AICPAPSS003    1    SJT_02   1
AICPAPSS003    1    SJT_03   3
AICPAPSS007    1    SJT_01   3
AICPAPSS007    1    SJT_02   1
AICPAPSS007    1    SJT_03   5

等......(将创建总共30条记录)

1 个答案:

答案 0 :(得分:1)

你走了 - 我想这就是你所追求的。如果您有140多列,则可能需要使用一些动态SQL来生成最终查询。 SO上有很多例子。

设置

IF(OBJECT_ID('Tempdb..#Test')) IS NOT NULL 
DROP TABLE  #Test;


SELECT * INTO #Test FROM (VALUES
('AICPAPSS003',1,5, 1, 3),
('AICPAPSS007',1,3, 1, 5),
('AICPAPSS012',1,2, 1, 4),
('AICPAPSS016',1,3, 2, 5),
('AICPAPSS019',1,1, 2, 5),
('AICPAPSS024',1,3, 2, 4),
('AICPAPSS025',1,1, 1, 4),
('AICPAPSS032',1,1, 2, 4),
('AICPAPSS033',1,3, 4, 5),
('AICPAPSS034',1,1, 2, 4)) A (ParticipantID, Form, SJT_01, SJT_02, SJT_03);

查询

SELECT ParticipantID, Form, ItemCode, Value 
FROM #Test t
UNPIVOT 
(
    Value FOR ItemCode IN (SJT_01, SJT_02, SJT_03)
) u;

结果

ParticipantID Form        ItemCode                                                                                                                         Value
------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- -----------
AICPAPSS003   1           SJT_01                                                                                                                           5
AICPAPSS003   1           SJT_02                                                                                                                           1
AICPAPSS003   1           SJT_03                                                                                                                           3
AICPAPSS007   1           SJT_01                                                                                                                           3
AICPAPSS007   1           SJT_02                                                                                                                           1
AICPAPSS007   1           SJT_03                                                                                                                           5
AICPAPSS012   1           SJT_01                                                                                                                           2
AICPAPSS012   1           SJT_02                                                                                                                           1
AICPAPSS012   1           SJT_03                                                                                                                           4
AICPAPSS016   1           SJT_01                                                                                                                           3
AICPAPSS016   1           SJT_02                                                                                                                           2
AICPAPSS016   1           SJT_03                                                                                                                           5
AICPAPSS019   1           SJT_01                                                                                                                           1
AICPAPSS019   1           SJT_02                                                                                                                           2
AICPAPSS019   1           SJT_03                                                                                                                           5
AICPAPSS024   1           SJT_01                                                                                                                           3
AICPAPSS024   1           SJT_02                                                                                                                           2
AICPAPSS024   1           SJT_03                                                                                                                           4
AICPAPSS025   1           SJT_01                                                                                                                           1
AICPAPSS025   1           SJT_02                                                                                                                           1
AICPAPSS025   1           SJT_03                                                                                                                           4
AICPAPSS032   1           SJT_01                                                                                                                           1
AICPAPSS032   1           SJT_02                                                                                                                           2
AICPAPSS032   1           SJT_03                                                                                                                           4
AICPAPSS033   1           SJT_01                                                                                                                           3
AICPAPSS033   1           SJT_02                                                                                                                           4
AICPAPSS033   1           SJT_03                                                                                                                           5
AICPAPSS034   1           SJT_01                                                                                                                           1
AICPAPSS034   1           SJT_02                                                                                                                           2
AICPAPSS034   1           SJT_03                                                                                                                           4

(30 row(s) affected)