日常取數(shù)過(guò)程中,時(shí)常會(huì)遇到沧竟,按照某個(gè)字段的值铸敏,取分布情況。
比如按照使用時(shí)間悟泵,0-2h杈笔,2-4h,4-6h.....的占比
此時(shí)如果用case...when 的話需要重復(fù)寫很多糕非,可以用floor()函數(shù)蒙具,取整。
round() 遵循四舍五入把原值轉(zhuǎn)化為指定小數(shù)位數(shù)朽肥,round(1.45,0) = 1;round(1.55,0)=2
floor()向下舍入為指定小數(shù)位數(shù) 如:floor(1.45,0)= 1;floor(1.55,0) = 1
ceiling()向上舍入為指定小數(shù)位數(shù) 如:ceiling(1.45,0) = 2;ceiling(1.55,0)=2
select floor(time_h/2), count(imei) as uv
from (SELECT imei,sum (cast (duration as bigint)/10800000) as time_h--h/day
from tabename
where event_date between '' and ''
group by imei) a
group by floor(time_h/2)