有没有办法在 DuckDB 中按 15 分钟的间隔进行分组?
Is there a way to group by intervals of 15 min in DuckDB?
我用
做了一个table
create table counter (
createdat TIMESTAMP,
tickets INT,
id VARCHAR
)
我想按 15 分钟的间隔对行进行分组,所以我尝试这样做:
SELECT
SUM(tickets) AS total,
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat)
from counter
where id = ?
group by
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat)
通过此查询,我只能按小时查询。但是我无法将其修改为分钟。
查询如何按 15 分钟的间隔分组?
为刻钟添加一个表达式:
(extract(minute from createdat) / 15)::integer
到您的专栏:
select
sum(tickets) AS total,
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat),
(extract(minute from createdat) / 15)::integer
from counter
where id = ?
group by
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat),
(extract(minute from createdat) / 15)::integer
转换为 integer
会截断除法结果的小数部分。
我用
做了一个tablecreate table counter (
createdat TIMESTAMP,
tickets INT,
id VARCHAR
)
我想按 15 分钟的间隔对行进行分组,所以我尝试这样做:
SELECT
SUM(tickets) AS total,
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat)
from counter
where id = ?
group by
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat)
通过此查询,我只能按小时查询。但是我无法将其修改为分钟。
查询如何按 15 分钟的间隔分组?
为刻钟添加一个表达式:
(extract(minute from createdat) / 15)::integer
到您的专栏:
select
sum(tickets) AS total,
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat),
(extract(minute from createdat) / 15)::integer
from counter
where id = ?
group by
extract(year from createdat),
extract(month from createdat),
extract(day from createdat),
extract(hour from createdat),
(extract(minute from createdat) / 15)::integer
转换为 integer
会截断除法结果的小数部分。