Sql server 2012 对字段值使用UNPIVOT和列名

Sql server 2012 对字段值使用UNPIVOT和列名,sql-server-2012,Sql Server 2012,上下文是在线评估响应。我必须处理的源数据是每个参与者(考生)一条记录,其中140多列代表每个问题的答案值。列名将对应于特定问题的“ItemCode” 我想将此数据移动到下表中 ResponseItems ParticipantID Form ( 1 or 2 depending on ParticipantID ) ItemCode ( Name of the Column ) ItemDesc ( pulled from Codebook on It

上下文是在线评估响应。我必须处理的源数据是每个参与者(考生)一条记录,其中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
我可以用一个大的联合声明来攻击这个问题,但它不能扩展到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命令,但似乎无法使其发挥作用。 如有任何建议,将不胜感激

有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条记录)

给你-我想这就是你想要的。如果你有140多个列,你可能需要使用一些动态SQL来生成最终查询。这里有大量的例子

安装程序 查询 结果
请发布样本数据和预期结果。
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)