111,126
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb') is not null drop table tb
create table tb(spec varchar(50),grade int)
insert into tb
select 'A',1 union all
select 'A',2 union all
select 'B',1 union all
select 'B',2 union all
select 'C',3 union all
select 'C',2
select spec ,
sum(case when grade=1 then 1 else 0 end)[合格],
sum(case when grade=2 then 1 else 0 end)[次品],
sum(case when grade=3 then 1 else 0 end)[废品]
from tb
group by spec
drop table tb
select spec, hege=sum(hege), cipin=sum(cipin), feipin=sum(feipin)
from
(
select
spec,
hege = case grade when 1 then 1 else 0 end,
cipin = case grade when 2 then 1 else 0 end,
feipin = case grade when 3 then 1 else 0 end
from 表
) 临时表
group by spec