Mysql區(qū)間分組查詢的實(shí)現(xiàn)方式
Mysql區(qū)間分組查詢
場(chǎng)景
一張用戶表(user),有用戶id(id)、余額(balance)等字段,要求展示 余額在某個(gè)區(qū)間內(nèi)的人數(shù)
? 區(qū)間有0-1萬(wàn),1-10萬(wàn),10-50萬(wàn),50-100萬(wàn),100萬(wàn)+,
下面是模擬數(shù)據(jù):
用戶id?? ??? ?余額 1?? ??? ??? ?100?? ? 2?? ??? ??? ?200?? ? 3?? ??? ??? ?3223 4?? ??? ??? ?100001 5?? ??? ??? ?100025 6?? ??? ??? ?512123 7?? ??? ??? ?565656 8?? ??? ??? ?10000001
統(tǒng)計(jì)結(jié)果應(yīng)該如下所示:
余額 人數(shù)
0-1萬(wàn) 1
1-10萬(wàn) 2
10-50萬(wàn) 1
50-100萬(wàn) 2
100萬(wàn)+ 1
第一想法
select? ?? ?count(if(balance between 0 and 10000, id , null ) ) as "0-1萬(wàn)", ?? ?count(if(balance between 10001 and 100000, id , null ) ) as "1-10萬(wàn)", ?? ?count(if(balance between 100001 and 500000, id , null ) ) as "10-50萬(wàn)", ?? ?count(if(balance between 500001 and 1000000, id , null ) ) as "50-100萬(wàn)", ?? ?count(if(balance > 1000000, id , null ) ) as "100萬(wàn)+" from user ;
這樣可以查出來(lái)每個(gè)范圍對(duì)應(yīng)的人數(shù),但是不盡人意,而且寫(xiě)的很麻煩…
一番百度之后
select interval(balance,0,10000,100000,500000,1000000) as i ,count(*)? from user group by i; select elt(interval(balance,0,10000,100000,500000,1000000),"0-1萬(wàn)","1-10萬(wàn)","10-50萬(wàn)","50-100萬(wàn)","100萬(wàn)+") as region ,count(*)? from user group by region;
利用了mysql提供的interval和elt函數(shù)實(shí)現(xiàn)了效果
interval
interval(N,N1,N2,N3) ,比較列表中的N值,該函數(shù)如果N<N1返回0,如果N<N2返回1,如果N<N3返回2 等等。
elt
elt(n,str1,str2,str3,…) 如果n=1,則返回str1,如果n=2,則返回str2,依次類推
兩個(gè)函數(shù)結(jié)合,再加上group,實(shí)現(xiàn)了這種范圍分組的效果
另一種解決辦法
由于使用的是類似mysql語(yǔ)句查詢的一個(gè)分析數(shù)據(jù)庫(kù),它不支持elt函數(shù)和interval函數(shù)(抄mysql沒(méi)有抄全…)
實(shí)現(xiàn)這種范圍分組的場(chǎng)景,可以通過(guò)創(chuàng)建中間表的形式實(shí)現(xiàn)。然后通過(guò)用戶表去join
創(chuàng)建如下一個(gè)中間表:有下限、上限和區(qū)間名三個(gè)字段
lower?? ??? ?upper?? ??? ?region 0?? ??? ??? ?10000?? ??? ?0-1萬(wàn) 10001?? ??? ?100000?? ??? ?1-10萬(wàn) 100001?? ??? ?500000?? ??? ?10-50萬(wàn) 500001?? ??? ?1000000?? ??? ?50-100萬(wàn) 1000000?? ??? ?2000000000?? ?100萬(wàn)+
用戶表就可以通過(guò)余額字段去join這個(gè)表
select region,count(*) from user? left join tmp on user.balance between tmp.lower and tmp.upper group by region?
就可以實(shí)現(xiàn)范圍分組的效果
相比之前兩種,感覺(jué)這個(gè)想法很有趣(同事教的)。
按區(qū)間分組查詢、獲取各區(qū)間的總數(shù)
數(shù)據(jù)表如下

需求
tick_count是次數(shù)、user_account是用戶標(biāo)識(shí),user_account可能重復(fù),統(tǒng)計(jì)0次,1-3次、4-6次、7-9次、10-12次、13次以上,這幾個(gè)區(qū)間各有多少個(gè)用戶數(shù)
select case
when tc.stick_count = 0 then
'0'
when tc.stick_count > 0 and tc.stick_count <= 3 then
'1to3'
when tc.stick_count > 3 and tc.stick_count<= 6 then
'4to6'
when tc.stick_count > 6 and tc.stick_count <= 9 then
'7to9'
when tc.stick_count > 9 and tc.stick_count <= 12 then
'10to12'
when tc.stick_count > 13 then
'more13'
end stickLevel,
COUNT(DISTINCT user_account) total
from t_stick_detail_hourly tc
group by case
when tc.stick_count = 0 then
'0'
when tc.stick_count > 0 and tc.stick_count <= 3 then
'1to3'
when tc.stick_count > 3 and tc.stick_count<= 6 then
'4to6'
when tc.stick_count > 6 and tc.stick_count <= 9 then
'7to9'
when tc.stick_count > 9 and tc.stick_count <= 12 then
'10to12'
when tc.stick_count > 13 then
'more13'
end運(yùn)行結(jié)果

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mysql數(shù)據(jù)庫(kù)鎖定機(jī)制詳細(xì)介紹
這篇文章主要介紹了Mysql數(shù)據(jù)庫(kù)鎖定機(jī)制詳細(xì)介紹,本文用大量?jī)?nèi)容講解了Mysql中的鎖定機(jī)制,例如MySQL鎖定機(jī)制簡(jiǎn)介、合理利用鎖機(jī)制優(yōu)化MySQL等內(nèi)容,需要的朋友可以參考下2014-12-12
在?CentOS?7?下如何使用?Ansible?Playbook?實(shí)現(xiàn)?MySQL?8.0.34?的
要在?CentOS?7?下使用?Ansible?Playbook?實(shí)現(xiàn)?MySQL?8.0.34?的二進(jìn)制安裝,需要先下載?MySQL?8.0.34?的二進(jìn)制包,并將其上傳至目標(biāo)服務(wù)器,對(duì)MySQL?8.0.34?二進(jìn)制安裝過(guò)程感興趣的朋友跟隨小編一起看看吧2024-03-03
Mysql8.4數(shù)據(jù)庫(kù)安裝新建用戶和庫(kù)表
MySQL是最常用的數(shù)據(jù)庫(kù),本文主要介紹了Mysql8.4數(shù)據(jù)庫(kù)安裝新建用戶和庫(kù)表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
解決數(shù)據(jù)庫(kù)有數(shù)據(jù)但查詢出來(lái)的值為Null問(wèn)題
這篇文章主要介紹了解決數(shù)據(jù)庫(kù)有數(shù)據(jù)但查詢出來(lái)的值為Null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

