分区间统计

--x<=30,30<x<=60,x>60
declare @t table(id int primary key,col decimal(10,2))
insert into @t select 1,26
union all select 2,88
union all select 3,4
union all select 4,76
union all select 5,58
union all select 6,18
union all select 7,84

--第一种方法
select des,cou = count(cou),num = cast(count(cou) as decimal(10,2))/b.cout  from(
select
cou = case when a.col <= 30 then 1
when a.col > 30 and a.col <= 60 then 2
else 3 end,
des = case when a.col <= 30 then 'col<=30'
when a.col > 30 and a.col <= 60 then '30<col<=60'
else 'col>60' end
 from @t a ) a cross join (select cout = count(1) from @t ) b  group by des,b.cout


--第二种方法
select a.des,cou=count(1),num = cast(count(1) as decimal(10,2))/c.cout from
(select sid = 1,leftNum = null,rightNum = 30,des = 'col<=30'
 union all select sid = 2,leftNum = 30,rightNum =60,des = '30<col<=60'
 union all select sid = 3,leftNum = 60,rightNum = null,des = 'col>60'
) a left join @t b on (b.col <= a.rightNum or a.rightNum is null) and (b.col > a.leftNum or a.leftNum is null)
cross join (select cout = count(1) from @t ) c
group by a.des,c.cout

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值