在Server 2008中,我试图使用周期列将下面的表格式“枢轴”为宽格式(在实际数据中有5个不同的句点)。
我已经搜索过了,但还没有找到解决办法。我已经提到了https://www.tangrainc.com/blog/2009/01/pivoting-on-multiple-columns/#comment-504,但不能将逻辑转换为>2个值列--这是我所需要的。
有什么想法吗?你可能已经猜到我不是SQL专家。使用Server 2008。
谢谢,克里斯
ps。第一个S/O岗位!
试图从一张扁平的桌子上获得:
摆到一张大桌子上:
发布于 2018-05-29 14:23:40
您可以使用条件聚合:
select Cat, Dept,
sum(case when Period = 'LW' then New else 0 end) as [Net LW],
sum(case when Period = 'LY' then New else 0 end) as [Net LY],
sum(case when Period = 'LW' then Gross else 0 end) as [Gross LW],
sum(case when Period = 'LY' then Gross else 0 end) as [Gross LY],
sum(case when Period = 'LW' then Profit else 0 end) as [Profit LW],
sum(case when Period = 'LY' then Profit else 0 end) as [Profit LY],
sum(case when Period = 'LW' then Units else 0 end) as [Units LW],
sum(case when Period = 'LY' then Units else 0 end) as [Units LY]
from table t
group by Cat, Dept;
https://stackoverflow.com/questions/50586715
复制相似问题