select 编号 = identity(int,1,1),* into test from
(
select 公司名称,
sum(case when adddate = 1 then addvalue else 0 end) as '一月份',
sum(case when adddate = 2 then addvalue else 0 end) as '二月份',
sum(case when adddate = 3 then addvalue else 0 end) as '三月份',
sum(case when adddate = 4 then addvalue else 0 end) as '四月份',
sum(case when adddate = 5 then addvalue else 0 end) as '五月份',
sum(case when adddate = 6 then addvalue else 0 end) as '六月份',
sum(case when adddate = 7 then addvalue else 0 end) as '七月份',
sum(case when adddate = 8 then addvalue else 0 end) as '八月份',
sum(case when adddate = 9 then addvalue else 0 end) as '九月份',
sum(case when adddate = 10 then addvalue else 0 end) as '十月份',
sum(case when adddate = 11 then addvalue else 0 end) as '十一月份',
sum(case when adddate = 12 then addvalue else 0 end) as '十二月份',
sum(addvalue) as 总计
from tb
group by 公司名称
) t
select id = identity(int,1,1),* into test from
(
select 公司名称,
sum(case when adddate = 1 then addvalue else 0 end) as '一月份',
sum(case when adddate = 2 then addvalue else 0 end) as '二月份',
sum(case when adddate = 3 then addvalue else 0 end) as '三月份',
sum(case when adddate = 4 then addvalue else 0 end) as '四月份',
sum(case when adddate = 5 then addvalue else 0 end) as '五月份',
sum(case when adddate = 6 then addvalue else 0 end) as '六月份',
sum(case when adddate = 7 then addvalue else 0 end) as '七月份',
sum(case when adddate = 8 then addvalue else 0 end) as '八月份',
sum(case when adddate = 9 then addvalue else 0 end) as '九月份',
sum(case when adddate = 10 then addvalue else 0 end) as '十月份',
sum(case when adddate = 11 then addvalue else 0 end) as '十一月份',
sum(case when adddate = 12 then addvalue else 0 end) as '十二月份'
from tb
group by 公司名称
) t
create table 表A
(
id char(3),
data int,
month int
)
insert into 表A(id,data,month) values('001',11,1)
insert into 表A(id,data,month) values('001',12,2)
insert into 表A(id,data,month) values('001',13,3)
insert into 表A(id,data,month) values('001',14,4)
insert into 表A(id,data,month) values('001',15,5)
insert into 表A(id,data,month) values('001',16,6)
insert into 表A(id,data,month) values('001',17,7)
insert into 表A(id,data,month) values('001',18,8)
insert into 表A(id,data,month) values('001',19,9)
insert into 表A(id,data,month) values('001',110,10)
insert into 表A(id,data,month) values('001',111,11)
insert into 表A(id,data,month) values('001',112,12)
insert into 表A(id,data,month) values('002',21,1)
insert into 表A(id,data,month) values('002',22,2)
insert into 表A(id,data,month) values('002',23,3)
insert into 表A(id,data,month) values('002',24,4)
insert into 表A(id,data,month) values('002',25,5)
insert into 表A(id,data,month) values('002',26,6)
insert into 表A(id,data,month) values('002',27,7)
insert into 表A(id,data,month) values('002',28,8)
insert into 表A(id,data,month) values('002',29,9)
insert into 表A(id,data,month) values('002',210,10)
insert into 表A(id,data,month) values('002',211,11)
insert into 表A(id,data,month) values('002',212,12)
go
SELECT id as '人员' ,
SUM(CASE month WHEN 1 THEN data ELSE 0 END) AS '1月' ,
SUM(CASE month WHEN 2 THEN data ELSE 0 END) AS '2月' ,
SUM(CASE month WHEN 3 THEN data ELSE 0 END) AS '3月' ,
SUM(CASE month WHEN 4 THEN data ELSE 0 END) AS '4月' ,
SUM(CASE month WHEN 5 THEN data ELSE 0 END) AS '5月' ,
SUM(CASE month WHEN 6 THEN data ELSE 0 END) AS '6月' ,
SUM(CASE month WHEN 7 THEN data ELSE 0 END) AS '7月' ,
SUM(CASE month WHEN 8 THEN data ELSE 0 END) AS '8月' ,
SUM(CASE month WHEN 9 THEN data ELSE 0 END) AS '9月' ,
SUM(CASE month WHEN 10 THEN data ELSE 0 END) AS '10月' ,
SUM(CASE month WHEN 11 THEN data ELSE 0 END) AS '11月' ,
SUM(CASE month WHEN 12 THEN data ELSE 0 END) AS '12月'
FROM 表A
GROUP BY ID
SELECT id as '人员' ,
SUM(CASE month WHEN 1 THEN data ELSE 0 END) AS '1月' ,
SUM(CASE month WHEN 2 THEN data ELSE 0 END) AS '2月' ,
SUM(CASE month WHEN 3 THEN data ELSE 0 END) AS '3月' ,
SUM(CASE month WHEN 4 THEN data ELSE 0 END) AS '4月' ,
SUM(CASE month WHEN 5 THEN data ELSE 0 END) AS '5月' ,
SUM(CASE month WHEN 6 THEN data ELSE 0 END) AS '6月' ,
SUM(CASE month WHEN 7 THEN data ELSE 0 END) AS '7月' ,
SUM(CASE month WHEN 8 THEN data ELSE 0 END) AS '8月' ,
SUM(CASE month WHEN 9 THEN data ELSE 0 END) AS '9月' ,
SUM(CASE month WHEN 10 THEN data ELSE 0 END) AS '10月' ,
SUM(CASE month WHEN 11 THEN data ELSE 0 END) AS '11月' ,
SUM(CASE month WHEN 12 THEN data ELSE 0 END) AS '12月' ,
SUM(data) as '合计'
FROM 表A
GROUP BY ID
Create View V_TEST
As
Select
compName As 公司名称,
SUM(Case addDate When 1 Then addValue Else 0 End) As 一月份,
SUM(Case addDate When 2 Then addValue Else 0 End) As 二月份,
SUM(Case addDate When 3 Then addValue Else 0 End) As 三月份,
SUM(Case addDate When 4 Then addValue Else 0 End) As 四月份,
SUM(Case addDate When 5 Then addValue Else 0 End) As 五月份,
SUM(Case addDate When 6 Then addValue Else 0 End) As 六月份,
SUM(Case addDate When 7 Then addValue Else 0 End) As 七月份,
SUM(Case addDate When 8 Then addValue Else 0 End) As 八月份,
SUM(Case addDate When 9 Then addValue Else 0 End) As 九月份,
SUM(Case addDate When 10 Then addValue Else 0 End) As 十月份,
SUM(Case addDate When 11 Then addValue Else 0 End) As 十一月份,
SUM(Case addDate When 12 Then addValue Else 0 End) As 十二月份
From
表
Group By
compName
GO
select 公司名称,
sum(case when adddate = 1 then addvalue else 0 end) as '一月份',
sum(case when adddate = 2 then addvalue else 0 end) as '二月份',
sum(case when adddate = 3 then addvalue else 0 end) as '三月份',
sum(case when adddate = 4 then addvalue else 0 end) as '四月份',
sum(case when adddate = 5 then addvalue else 0 end) as '五月份',
sum(case when adddate = 6 then addvalue else 0 end) as '六月份',
sum(case when adddate = 7 then addvalue else 0 end) as '七月份',
sum(case when adddate = 8 then addvalue else 0 end) as '八月份',
sum(case when adddate = 9 then addvalue else 0 end) as '九月份',
sum(case when adddate = 10 then addvalue else 0 end) as '十月份',
sum(case when adddate = 11 then addvalue else 0 end) as '十一月份',
sum(case when adddate = 12 then addvalue else 0 end) as '十二月份'
from tb
group by 公司名称