0

我做了一张桌子

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 分钟的间隔分组?

4

1 回答 1

1

为刻钟添加一个表达式:

(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截断除法结果的小数部分。

于 2021-07-16T03:35:43.747 回答