sql – 在Oracle中创建直方图/频率分布的最佳方法?

前端之家收集整理的这篇文章主要介绍了sql – 在Oracle中创建直方图/频率分布的最佳方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个事件表,其中包含两列eventkey(唯一,主键)和createtime,它将事件的创建时间存储为NUMBER列中自1970年1月1日以来的毫秒数.

我想创建一个“直方图”或频率分布,显示过去一周每小时创建了多少事件.

这是使用width_bucket()函数在Oracle中编写此类查询的最佳方法吗?是否可以使用其他Oracle分析函数之一导出落入每个存储桶的行数,而不是使用width_bucket来确定每行所属的存储桶编号并对其进行计数(*)?

-- 1305504000000 = 5/16/2011 12:00am GMT
-- 1306108800000 = 5/23/2011 12:00am GMT
select 
timestamp '1970-01-01 00:00:00' + numtodsinterval((1305504000000/1000 + (bucket * 60 * 60)),'second') period_start,numevents
from (
  select bucket,count(*) as events from (
    select eventkey,createtime,width_bucket(createtime,1305504000000,1306108800000,24 * 7) bucket
    from events 
    where createtime between 1305504000000 and 1306108800000
  ) group by bucket
) 
order by period_start

解决方法

如果您的创建时间是日期列,那么这将是微不足道的:
SELECT TO_CHAR(CREATE_TIME,'DAY:HH24'),COUNT(*) 
  FROM EVENTS
 GROUP BY TO_CHAR(CREATE_TIME,'DAY:HH24');

实际上,转换createtime列并不太难:

select TO_CHAR( 
         TO_DATE('19700101','YYYYMMDD') + createtime / 86400000),'DAY:HH24') AS BUCKET,COUNT(*)
   FROM EVENTS
  WHERE createtime between 1305504000000 and 1306108800000
 group by TO_CHAR( 
         TO_DATE('19700101','DAY:HH24') 
 order by 1

或者,如果您正在寻找fencepost值(例如,从第一个十分位数(0-10%)到下一个十分位数(11-20%),您可以执行以下操作:

select min(createtime) over (partition by decile) as decile_start,max(createtime) over (partition by decile) as decile_end,decile
  from (select createtime,ntile (10) over (order by createtime asc) as decile
          from events
         where createtime between 1305504000000 and 1306108800000
       )

猜你在找的MsSQL相关文章