27,579
社区成员
发帖
与我相关
我的任务
分享
if not object_id('tb') is null
drop table tb
Go
Create table tb([分类] nvarchar(2),[数量] int)
Insert tb
select N'A类',5 union all
select N'A类',5 union all
select N'B类',10 union all
select N'C类',6 union all
select N'C类',8 union all
select N'D类',2 union all
select N'E类',7
Go
select sum(case when 分类=N'A类' then 1 else 0 end )A类,
sum(case when 分类=N'B类' then 1 else 0 end )B类,
sum(case when 分类=N'C类' then 1 else 0 end )C类,
sum(case when 分类 in(N'A类',N'B类',N'C类') then 数量 else 0 end )合计
from tb
/*
A类 B类 C类 合计
----------- ----------- ----------- -----------
2 1 2 34
(1 個資料列受到影響)
*/
select sum(case when 分类='A类' then 1 else 0 end ) [A类],
sum(case when 分类='B类' then 1 else 0 end ) [B类],
sum(case when 分类='B类' then 1 else 0 end ) [B类],
sum(case when 分类 in ('A类','B类','C类') then 数量 else 0 end ) [合计]
from tb
select sum(case when 分类='A类' then 1 else 0 end )A类,
sum(case when 分类='B类' then 1 else 0 end )B类,
sum(case when 分类='C类' then 1 else 0 end )C类,
sum(case when 分類 in('A类','B类','C类') then 数量 else 0 end )
from tb