34,837
社区成员




总量 小于20的个数 小于30的个数 小于60的个数 小于120的个数 小于180的个数
------- ----------- -------------- -------------- -------------- --------------
1813 1488 1510 1538 1545 1545
declare @tb table(id int,value int)
insert into @tb
select 1,12 union all
select 2,32 union all
select 3,22 union all
select 4,45 union all
select 5,75
select * from @tb
select count(1) as 总量,
sum(case when value <20 then 1 else 0 end ) as 小于20的个数,
sum(case when value<30 then 1 else 0 end ) as 小于30的个数,
sum(case when value<60 then 1 else 0 end ) as 小于60的个数,
sum(case when value<120 then 1 else 0 end ) as 小于120的个数,
sum(case when value<180 then 1 else 0 end ) as 小于180的个数
from @tb
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,value int)
insert into [tb]
select 1,12 union all
select 2,32 union all
select 3,22 union all
select 4,45 union all
select 5,75 union all
select 6,175 union all
select 7,115 union all
select 8,85
select 总量=COUNT(*),
小于20的个数=SUM(case when value<20 then 1 else 0 end),
小于30的个数=SUM(case when value<30 then 1 else 0 end),
小于60的个数=SUM(case when value<60 then 1 else 0 end),
小于120的个数=SUM(case when value<120 then 1 else 0 end),
小于180的个数=SUM(case when value<180 then 1 else 0 end)
from tb
/*
总量 小于20的个数 小于30的个数 小于60的个数 小于120的个数 小于180的个数
8 1 2 4 7 8
*/
select
总量=count(*),
[小于20的个数]=sum(case when [value]<20 then 1 else 0 end),
[小于30的个数]=sum(case when [value]<30 then 1 else 0 end),
[小于60的个数]=sum(case when [value]<60 then 1 else 0 end),
[小于80的个数]=sum(case when [value]<80 then 1 else 0 end),
[小于120的个数]=sum(case when [value]<120 then 1 else 0 end)
from tb