勤学如春起之苗不见其增日有所长,辍学如磨刀之石不见其损日有所亏

第一行转换成第二行格式

select b.FactoryName,
isnull(M1,0) M1,isnull(M2,0) M2,isnull(M3,0) M3,isnull(M4,0) M4,
isnull(M5,0) M5,isnull(M6,0) M6,isnull(M7,0) M7,isnull(M8,0) M8,
isnull(M9,0) M9,isnull(M10,0) M10,isnull(M11,0) M11,isnull(M11,0) M12,
b.Month1,b.Month2,b.Month3,b.Month4,
b.Month5,b.Month6,b.Month7,b.Month8,
b.Month9,b.Month10,b.Month11,b.Month12
from(
select FactoryName,
sum(case datediff(m,getdate(),DateId+'01') when 0 then number else 0 end) M1,
sum(case datediff(m,getdate(),DateId+'01') when 1 then number else 0 end) M2,
sum(case datediff(m,getdate(),DateId+'01') when 2 then number else 0 end) M3,
sum(case datediff(m,getdate(),DateId+'01') when 3 then number else 0 end) M4,
sum(case datediff(m,getdate(),DateId+'01') when 4 then number else 0 end) M5,
sum(case datediff(m,getdate(),DateId+'01') when 5 then number else 0 end) M6,
sum(case datediff(m,getdate(),DateId+'01') when 6 then number else 0 end) M7,
sum(case datediff(m,getdate(),DateId+'01') when 7 then number else 0 end) M8,
sum(case datediff(m,getdate(),DateId+'01') when 8 then number else 0 end) M9,
sum(case datediff(m,getdate(),DateId+'01') when 9 then number else 0 end) M10,
sum(case datediff(m,getdate(),DateId+'01') when 10 then number else 0 end) M11,
sum(case datediff(m,getdate(),DateId+'01') when 11 then number else 0 end) M12
from cmdi_MonitorBaseData_Detail_Sum_Bar
group by FactoryName
)a
full join(
select aa.*,bb.FactoryName from(
select convert(varchar(7),getdate(),120) Month1,
convert(varchar(7),dateadd(m,1,getdate()),120) Month2,
convert(varchar(7),dateadd(m,2,getdate()),120) Month3,
convert(varchar(7),dateadd(m,3,getdate()),120) Month4,
convert(varchar(7),dateadd(m,4,getdate()),120) Month5,
convert(varchar(7),dateadd(m,5,getdate()),120) Month6,
convert(varchar(7),dateadd(m,6,getdate()),120) Month7,
convert(varchar(7),dateadd(m,7,getdate()),120) Month8,
convert(varchar(7),dateadd(m,8,getdate()),120) Month9,
convert(varchar(7),dateadd(m,9,getdate()),120) Month10,
convert(varchar(7),dateadd(m,10,getdate()),120) Month11,
convert(varchar(7),dateadd(m,11,getdate()),120) Month12
)aa
cross join(
select '达创' FactoryName
union all
select 'H3C' FactoryName
union all
select '飞旭' FactoryName
union all
select '统合' FactoryName
union all
select '信华' FactoryName
union all
select '东信' FactoryName
union all
select '杭州贝赢' FactoryName
)bb
)b on a.FactoryName=b.FactoryName

 

 

posted on 2017-12-14 16:41  TheClound  阅读(330)  评论(0编辑  收藏  举报