相关疑难解决方法(0)

PostgreSQL窗口函数:通过比较分区

我正在尝试找到与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)

sql postgresql window-functions postgresql-performance

4
推荐指数
1
解决办法
6937
查看次数

每天查询计数,包括多周的日期限制

我每天都在努力寻找#活跃用户.

用户在连续 4 每周发出超过 10个请求时处于活动状态.

即.2014年10月31日,如果用户每周总共发出超过10个请求,则用户处于活动状态:

  1. 2014年10月24日至10月30日AND
  2. 2014年10月17日至10月23日AND
  3. 2014年10月10日至10月16日AND
  4. 2014年10月3日至10月9日

我有一张桌子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)

sql postgresql date aggregate-functions postgresql-9.3

3
推荐指数
1
解决办法
1301
查看次数