2020-12-21 SQL按照步长分类统计

日常取数过程中,时常会遇到,按照某个字段的值,取分布情况。
比如按照使用时间,0-2h,2-4h,4-6h.....的占比
此时如果用case...when 的话需要重复写很多,可以用floor()函数,取整。

round() 遵循四舍五入把原值转化为指定小数位数,round(1.45,0) = 1;round(1.55,0)=2
floor()向下舍入为指定小数位数 如:floor(1.45,0)= 1;floor(1.55,0) = 1
ceiling()向上舍入为指定小数位数 如: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)

你可能感兴趣的:(2020-12-21 SQL按照步长分类统计)