请问大家 ,sqlserver里行转列 可以用pivot,但是多个指标值行转列,应该怎么写的哇?
方法不唯一,自行选择。
其一:多个子查询pivot后通过join合并。
其二:将多个指标字段合并成一个字段,然后pivot。
——————————————————————————————————
SELECT
product ,aa , bb
FROM
guest.tab t
UNPIVOT (
aa FOR bb IN(
c1, c2, c3, c4
)
) t
-- c1, c2, c3, c4 对应你要转的列 , aa 、bb 是给新增的两列取的名字
用group by做分组,然后用case when展示值,select 人员,case when 产品=A then 发货 end 产品a发货, case when 产品=b then 发货 end 产品b发货,case when 用量=A then end 产品a用量,case when 用量=b then 发货 end 产品b用量 from 表 group by 产品