create table A#(
tName Nvarchar(10),
typt nvarchar(10),
number int
)
insert into A# values('甲','销售',200)
insert into A# values('乙','采集',30)
insert into A# values('乙','采集',50)
insert into A# values('乙','销售',100)
select * from a#
select sum(number) from a# group by tName,typt
select distinct tname,sum(number)as 销售 ,
case
when typt='采集'
then sum(number) end as '采集'
from a# where typt='销售' group by tName,typt
select distinct tname,
sum(
case
when typt='销售'
then number else 0
end
)as '销售',
sum(
case
when typt='采集'
then number else 0
end
) as '采集'
from
(
select * from a#
)as TempTable
group by tname
求平均值
select tname,
sum(
case
when typt='采集' then number else 0
end
) as 采集
,
sum(
case
when typt='销售' then number else 0
end
) as 销售
from
(
select tname,typt, avg(number) as number from a# group by tName,typt
) as temptable group by tname