如何解决取消透视两个独立的列
我正在尝试取消透视表中的多个列。到目前为止,我已经尝试仅使用标准的Unpivot,并且在第一部分中成功了,但在第二部分中没有成功。我想同时取消这两列。我已附上我要做什么的图片。该表与错误有关。所以E1代表error1,E2代表error2等等...
INSERT INTO #tempWorkflowItem
SELECT AssignmentId,Code,Response FROM #temp2
UNPIVOT(Response FOR Code in (E1,E2,E3 ))AS WorkflowItemsUnpivot
UNPIVOT(Reason FOR Code in (E1Reason,E2Reason,E3Reason )) AS WorkflowItemsUnpivot2
解决方法
使用apply
:
select v.code,v.reason
from #temp2 t cross apply
(values ('E1',E1Reason),('E2',E2Reason),('E3',E3Reason)
) v(code,reason,e1,e2,de3);
我怀疑您还需要where
子句:
where v.code = 'E1' and t.e1 > 0 and
v.code = 'E2' and t.e2 > 0 and
v.code = 'E3' and t.e3 > 0 ;
或:
where v.reason is not null
将NULL
的值也拉回来似乎很奇怪。
我会推荐cross apply
:
select x.*
from mytable t
cross appy (values
('e1',e1reason),('e2',e2reason),('e3',e3,e3reason)
) x(code,response,reason)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。