天天看點

Sql sever 行轉列函數——pivot

實作效果:

Sql sever 行轉列函數——pivot

腳本:

--行轉列
with vw_a as(
 select convert(date,日期,111) as 日期
,case when 增值部門  like '%魯班%' then '魯班' else '資訊流' end as type
,sum(昨日總消耗) 消耗
  from 金華增值總部資料 
 where (日期 >= '2019-03-01') or (日期 >= '2018-12-01' and 日期 < '2018-12-21')
 group by convert(date,日期,111),case when 增值部門  like '%魯班%' then '魯班' else '資訊流' end
 )
 select * from vw_a
 pivot(sum(消耗) for type in([魯班],[資訊流])) t