分區間統計

2021-05-25 10:58:30 字數 1150 閱讀 8198

--x<=30,3060

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 '3060' 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 = '3060'

) 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

MySQL 常見的業務處理 進行分區間的統計

在日常工作中我們常常有這樣的需求 統計所有資料區間的資訊。案例 統計某個 系統中所有登入使用者消費金額大於1000元的,800到1000元的,以及800元以下的人數 使用者登入表 商品訂單表 條件判斷函式 函式作用case expr when v1 then r1 when v2 then r2.e...

mysql 區間分組統計

select elt interval c.money,0,1000,2000,3000,4000,5000 less 1000 1000to2000 2000to3000 3000to4000 4000to5000 as level,count c.id as cnt from select su...

oracle 自動分割槽,間隔分割槽。秒解

oracle間隔分割槽 最開始的分割槽是永久分割槽,隨著資料的增加自動建立新的分割槽 實現範圍分割槽的自動化 現有表建立間隔分割槽表的語法 create table 分割槽表名 partition by range 分割槽字段 interval numtoyminterval number,inte...