Postgres:无论星期六和星期日,每个开发者的最长连胜

时间:2014-01-22 21:59:11

标签: postgresql

我从上一篇关于Postgres: Defining the longest streak (in days) per developer的帖子中得到了我需要的信息。

然而,现在我想知道每个开发者最长的连胜,无论周六或周日。例如,Bob从周四18,周五19,周一22和周二23工作,因此Bob连胜4天。

我知道我可以使用DOW窗口功能,它给我0星期日,1星期一,依此类推。但是

我不知道如何在Gordon Linoff提出的最后解决方案中应用DOW功能。

你们有些人可以帮我解决这个问题吗?欢呼声,

1 个答案:

答案 0 :(得分:0)

WITH
  working_limits AS (
    SELECT
      MIN(mr_date) AS start_date,
      MAX(mr_date) AS end_date
    FROM
      xxx
  ),
  working_days AS (
    SELECT
      ROW_NUMBER() OVER () AS day_number,
      s.d::date AS date
    FROM
      GENERATE_SERIES((SELECT start_date FROM working_limits),
                      (SELECT end_date FROM working_limits),
                      '1 day') AS s(d)
    WHERE
      EXTRACT(dow FROM s.d) BETWEEN 1 AND 5),
  worked_days AS (
    SELECT
      ROW_NUMBER() OVER () AS day_number,
      developer,
      mr_date AS date
    FROM
      xxx
    ORDER BY
      developer,
      mr_date
  )
SELECT
  y.developer,
  MAX(y.days)
FROM (
  SELECT
    x.developer,
    COUNT(*) AS days
  FROM (
    SELECT
      wngd.date,
      wd.developer,
      wngd.day_number - wd.day_number AS delta
    FROM
      working_days wngd INNER JOIN worked_days wd
    ON
      wngd.date = wd.date) AS x
  GROUP BY
    x.developer,
    x.delta) AS y
GROUP BY
  y.developer;