我有一个events
表,其中包含两列eventkey
(唯一,主键)和createtime
,它将事件的创建时间存储为自1月1日以来的毫秒数1970年在NUMBER
列。
我想创建一个“直方图”或频率分布,显示过去一周每小时创建的事件数量。
这是使用width_bucket()
函数在Oracle中编写此类查询的最佳方法吗?是否可以使用其他Oracle分析函数之一导出落入每个存储桶的行数,而不是使用width_bucket
来确定每行所属的存储桶编号并对其执行count(*)
?
-- 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
答案 0 :(得分:11)
如果您的createtime
是日期列,那么这将是微不足道的:
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', 'YYYYMMDD') + createtime / 86400000),
'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
)
答案 1 :(得分:3)
我不熟悉Oracle的日期函数,但我非常肯定有一种编写Postgres语句的方法:
select date_trunc('hour', stamp), count(*)
from your_data
group by date_trunc('hour', stamp)
order by date_trunc('hour', stamp)
答案 2 :(得分:1)
与Adam完全相同的响应,但我更喜欢将period_start保留为时间字段,以便在需要时更容易进一步过滤:
with
events as
(
select rownum eventkey, round(dbms_random.value(1305504000000, 1306108800000)) createtime
from dual
connect by level <= 1000
)
select
trunc(timestamp '1970-01-01 00:00:00' + numtodsinterval(createtime/1000, 'second'), 'HH') period_start,
count(*) numevents
from
events
where
createtime between 1305504000000 and 1306108800000
group by
trunc(timestamp '1970-01-01 00:00:00' + numtodsinterval(createtime/1000, 'second'), 'HH')
order by
period_start
答案 3 :(得分:0)
使用oracle提供的函数“ WIDTH_BUCKET”来累积连续或精细离散的数据。以下示例显示了一种创建具有5个存储桶的直方图并收集“ COLUMN_VALUE”(从510到520)的方法(因此,每个存储桶都将获得范围2的值)。 WIDTH_BUCKET将创建其他id = 0和num_buckets + 1个存储桶,以分别用于低于最小值和最大值的值。
SELECT "BUCKET_ID", count(*),
CASE
WHEN "BUCKET_ID"=0 THEN -1/0F
ELSE 510+(520-510)/5*("BUCKET_ID"-1)
END "BUCKET_MIN",
CASE
WHEN "BUCKET_ID"=5+1 THEN 1/0F
ELSE 510+(520-510)/5*("BUCKET_ID")
END "BUCKET_MAX"
FROM
(
SELECT "COLUMN_VALUE",
WIDTH_BUCKET("COLUMN_VALUE", 510, 520, 5) "BUCKET_ID"
FROM "MY_TABLE"
)
group by "BUCKET_ID"
ORDER BY "BUCKET_ID";
样本输出
BUCKET_ID COUNT(*) BUCKET_MIN BUCKET_MAX
---------- ---------- ---------- ----------
0 45 -Inf 5.1E+002
1 220 5.1E+002 5.12E+002
2 189 5.12E+002 5.14E+002
3 43 5.14E+002 5.16E+002
4 3 5.16E+002 5.18E+002
在我的表格中,没有518-520,因此未显示ID = 5的存储桶。另一方面,值小于min(510),因此存在一个id == 0的存储桶,将-inf收集到510个值。