我想使用两个不同(但相似)的窗口函数来计算两个值
SUM
和COUNT
on is_active over user_id+item
,只到行的时间 - 负 1 小时。我的直觉是使用 ROWSUNBOUNDED PRECEDING
但这样我就不能随着时间的推移进行过滤
COUNT(1) OVER(PARTITION BY user_id, item ORDER BY req_time ROWS UNBOUNDED PRECEDING)
SUM(is_active) OVER(PARTITION BY user-id, item ORDER BY req_time ROWS UNBOUNDED PRECEDING)
但是,这没有考虑“1 小时前”间隔因素
考虑以下数据:
user_id | req_time | item | is_active |
--------+--------------------+-------------------+---
1 | 2011-01-01 12:00:00| 1 | 0 |
1 | 2011-01-01 12:30:00| 1 | 1 |
1 | 2011-01-01 15:00:00| 1 | 1 |
1 | 2011-01-01 16:00:00| 1 | 0 |
1 | 2011-01-01 16:00:00| 2 | 0 |
1 | 2011-01-01 16:20:00| 2 | 1 |
2 | 2011-02-02 11:00:00| 1 | 1 |
2 | 2011-02-02 13:00:00| 1 | 0 |
1 | 2011-02-02 16:20:00| 1 | 0 |
1 | 2011-02-02 16:30:00| 2 | 0 |
我希望得到以下结果:“值 1”是 SUM(is_active),“值 2”是 COUNT(1):
user_id | req_time | item | value 1 | value 2 |
--------+--------------------+-----------------+---------+
1 | 2011-01-01 12:00:00| 1 | 0 | 0 |
1 | 2011-01-01 12:30:00| 1 | 0 | 0 |
1 | 2011-01-01 15:00:00| 1 | 1 | 2 |
1 | 2011-01-01 16:00:00| 1 | 2 | 3 |
1 | 2011-01-01 16:00:00| 2 | 0 | 0 |
1 | 2011-01-01 16:20:00| 2 | 0 | 0 |
2 | 2011-02-02 11:00:00| 1 | 0 | 0 |
2 | 2011-02-02 13:00:00| 1 | 1 | 1 |
1 | 2011-02-02 16:20:00| 1 | 2 | 4 |
1 | 2011-02-02 16:30:00| 2 | 1 | 2 |
我正在使用基于 Postgresql 8.2.15 的 Greenplum 4.21
提前致谢!吉利比