MySQL按區間分組查詢統計報表

2021-09-02 10:37:37 字數 1482 閱讀 1062

表結構如下:

列名中文名usertripid

自增id

eventtime

記錄時間, 資料格式 yyyy-mm-dd hh:mm:ss

h小時時間

m分鐘時間

userid

使用者id

部分資料內容如下, 正常情況下資料量在35w左右:

查詢語句:

select

d.eventtime,

h, d.m,

elt(

interval (d.m, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 59),

'0~5分鐘',

'5~10分鐘',

'10~15分鐘',

'15~20分鐘',

'20~25分鐘',

'30~35分鐘',

'35~40分鐘',

'40~45分鐘',

'45~50分鐘',

'50~55分鐘',

'55~59分鐘'

) as yb_level,

count(distinct d.userid) as cnt

from

t_utrip d

where

d.eventtime >= '2015-4-20'

and d.eventtime < '2015-4-21'

group by

h, elt(

interval (d.m, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 59),

'0~5分鐘',

'5~10分鐘',

'10~15分鐘',

'15~20分鐘',

'20~25分鐘',

'30~35分鐘',

'35~40分鐘',

'40~45分鐘',

'45~50分鐘',

'50~55分鐘',

'55~59分鐘'

)order by

d.eventtime, d.h, d.m

sql語句分析:按小時(h列)分組

將按分鐘區間分組, 通過interval(n, n1,n2,n3,...)函式將60分鐘分割為: 0~4, 5~9, 10~14, 15~19, ... 55~59; 函式

interval()將返回對應的下標值, 起始值為1(檢視interval函式詳情)

使用elt(n, n1,n2,n3,...)函式將對應區間轉換為指定值, 即: 

'0~5分鐘', '5~10分鐘',  '10~15分鐘', ... (檢視elt函式詳情)

mysql按日期分組統計的查詢

最近寫的乙個使用者資料統計相關介面,需要用到按照每天進行分組統計。select date format create time,y m d sum user id from orders where order state 2 group by date format create time,y m...

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...

mysql區間查詢 MySQL區間分組查詢

假設a表為會員資訊表,需要統計男性會員年齡各階段的出現的人數 create table a id int 11 unsigned not null auto increment,name varchar 255 not null default comment 會員名稱 tinyint 1 unsi...