如何在Postgresql中评估数据?

时间:2016-12-12 17:35:43

标签: postgresql datetime select

我需要找到连续三个月发布三次或更多次的用户。我写了这个问题:

select count(id), owneruserid, extract(month from creationdate) as postmonth from posts
group by owneruserid, postmonth
having count(id) >=3
order by owneruserid, postmonth

我明白了:

count   owneruserid postmonth
36     -1            1
23     -1            2
45     -1            3
41     -1            4
18     -1            5
24     -1            6
31     -1            7
78     -1            8
83     -1            9
17     -1            10
88     -1            11
127    -1            12
3      6             11
3      7             12
4      8             1
8      8             12
4      12            4
3      12            5
3      22            2
4      22            4

(截短的)

哪个好。如何查询连续三次或更多,三个月或更长时间发布的用户?感谢。

1 个答案:

答案 0 :(得分:1)

这被称为群岛和差距问题,特别是它是具有日期范围的岛屿问题。你应该,

  1. 解决此问题。
  2. 将其标记为发送至dba.stackexchange.com
  3. 要解决这个问题,

    1. 如果前面的行与前一个mont不对应,则创建一个窗口为1的伪列
    2. 使用COUNT()
    3. 创建其中的群组
    4. 检查以确保该组的count(*)大于或等于三。
    5. 查询,

      SELECT l.id, creationdaterange, count(*)
      FROM (
      
        SELECT t.id,
          t.creationdate,
          count(range_reset) OVER (PARTITION BY t.id ORDER BY creationdate) AS creationdaterange
        FROM (
          SELECT id,
          creationdate,
          CASE
            WHEN date_trunc('month',creationdate::date)::date - interval '1 month' = date_trunc('month',lag(creationdate))::date OVER (PARTITION BY id ORDER BY creationdate)
            THEN 1
          END AS range_reset
          FROM post
          ORDER BY id, creationdate
        ) AS t;
      
      ) AS l
      GROUP BY t.id, creationdaterange
      HAVING count(*) >= 3;