我正在尝试找到与PostgreSQL查询中的WINDOW函数中的PARTITION BY子句中的当前行进行比较的方法.
想象一下,我在这5个元素的以下查询中有一个短列表(在实际情况中,我有数千甚至数百万行).我试图获取每一行,下一个不同元素(事件列)的id,以及前一个不同元素的id.
WITH events AS(
SELECT 1 as id, 12 as event, '2014-03-19 08:00:00'::timestamp as date
UNION SELECT 2 as id, 12 as event, '2014-03-19 08:30:00'::timestamp as date
UNION SELECT 3 as id, 13 as event, '2014-03-19 09:00:00'::timestamp as date
UNION SELECT 4 as id, 13 as event, '2014-03-19 09:30:00'::timestamp as date
UNION SELECT 5 as id, 12 as event, '2014-03-19 10:00:00'::timestamp as date
)
SELECT lag(id) over w as previous_different, event
, lead(id) over w as next_different
FROM …Run Code Online (Sandbox Code Playgroud) 我每天都在努力寻找#活跃用户.
用户在连续 4 周每周发出超过 10个请求时处于活动状态.
即.2014年10月31日,如果用户每周总共发出超过10个请求,则用户处于活动状态:
我有一张桌子requests:
CREATE TABLE requests (
id text PRIMARY KEY, -- id of the request
amount bigint, -- sum of requests made by accounts_id to recipient_id,
-- aggregated on a daily basis based on "date"
accounts_id text, -- id of the user
recipient_id text, -- id of the recipient
date timestamp -- date that the request was made in YYYY-MM-DD
);
Run Code Online (Sandbox Code Playgroud)
样本值:
INSERT …Run Code Online (Sandbox Code Playgroud)