实现效果:
脚本:
--行转列
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