【发布时间】:2018-01-03 21:25:55
【问题描述】:
我需要聚合一个窗口(在 PostgreSQL 中):
我有这张表“locationtimestamp”:
seq | location | timestamp
-------------------------------------
1 | home | 2018-01-02 18:00
2 | home | 2018-01-03 08:00
3 | work | 2018-01-03 09:00
4 | work | 2018-01-03 16:00
5 | home | 2018-01-03 17:00
6 | elsewhere | 2018-01-03 18:00
7 | elsewhere | 2018-01-03 20:00
8 | home | 2018-01-03 21:00
9 | home | 2018-01-03 22:00
10 | home | 2018-01-03 23:00
我想要这个结果:
location | min_seq | max_seq | min_timestamp | max_timestamp
-------------------------------------------------------------------
home | 1 | 2 | 2018-01-02 18:00 | 2018-01-03 08:00
work | 3 | 4 | 2018-01-03 09:00 | 2018-01-03 16:00
home | 5 | 5 | 2018-01-03 17:00 | 2018-01-03 17:00
elsewhere | 6 | 7 | 2018-01-03 18:00 | 2018-01-03 20:00
home | 8 | 10 | 2018-01-03 21:00 | 2018-01-03 23:00
我尝试使用窗口函数来实现这一点,但结果是 10 行(表中的行数)而不是所需的聚合 5。
到目前为止我的尝试(不工作):
SELECT
location,
MIN(seq) OVER w min_seq,
MAX(seq) OVER w max_seq,
MIN("timestamp") OVER w min_timestamp,
MAX("timestamp") OVER w max_timestamp
FROM locationtimestamp
WINDOW w AS (PARTITION BY location ORDER BY seq ASC)
ORDER BY seq
【问题讨论】:
-
@RadimBača 谢谢!请参阅我对 Gordon Linoff 回答的评论。这是唯一的方法吗?或者所有的差距和孤岛问题都以类似的方式解决了吗? ;-)
-
我相信您也可以使用其他窗口函数,例如
lag或lead,但是,这是我见过的最优雅的解决方案。
标签: sql postgresql aggregate-functions window-functions gaps-and-islands