我有一个表,有三列。
id,时间和值
如何获得正确的ID,即每5分钟在Postgres中只有一个数据?
例如:
id time value
1 03:11 test
1 07:13 test
1 12:11 test
2 02:11 test
2 07:11 test
这是我的版本,但只能得到一个小时的时间...
select
distinct on("id")
"id",
time::time,
value
from (
select
"id",
time::time,
value,
dense_rank() over (
partition by "id"
order by
to_char(time,'HH')::int,
(
to_char(time,'MI')
)::int/5
)
from my_table
where time between '2018-07-03 00:00:00' and '2018-07-03 01:00:00' and "id" = 'XXXXX'
) as res
order by "id";
结果:
id time value
1 03:05 14
但是结果是有10行。.也许更多
答案 0 :(得分:1)
下面的查询将产生所需的结果。
with res(id,time,value,rnk) as ( select id, time, value, dense_rank() over ( partition by id order by to_char(time,'HH24'), ( to_char(time,'MI') )::int/5 ) from your_table ) select distinct on(id,rnk) id, to_char(time,'HH24:MI'), value from res order by id,rnk