AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 287519
Accepted
James
James
Asked: 2021-03-23 21:21:07 +0800 CST2021-03-23 21:21:07 +0800 CST 2021-03-23 21:21:07 +0800 CST

基于开始/结束日期名册的活跃用户月度摘要

  • 772

我有一张表格,说明谁参与了一个项目,从开始日期到结束日期。我需要一些帮助来编写一个查询,该查询将自今年年初以来在每个月底返回“活跃”用户的数量。

DROP TABLE "public"."roster";
CREATE TABLE "public"."roster" ("id" int,"user_id" int,"project_id" int,"start_date" datetime,"end_date" datetime,"closed_date" datetime, PRIMARY KEY ("id"));

INSERT INTO "public"."roster" ("id", "user_id", "project_id", "start_date", "end_date", "closed_date") VALUES
(1, 1, 1, '2019-05-27 00:00:00', '2021-01-15 00:00:00', NULL);

INSERT INTO "public"."roster" ("id", "user_id", "project_id", "start_date", "end_date", "closed_date") VALUES
(2, 2, 2, '2020-05-27 00:00:00', '2021-02-01 00:00:00', '2021-02-05 00:00:00');

INSERT INTO "public"."roster" ("id", "user_id", "project_id", "start_date", "end_date", "closed_date") VALUES
(3, 3, 3, '2020-05-27 00:00:00', '2024-02-01 00:00:00', '2021-02-05 00:00:00');

INSERT INTO "public"."roster" ("id", "user_id", "project_id", "start_date", "end_date", "closed_date") VALUES
(4, 4, 4, '2020-05-27 00:00:00', '2021-03-05 00:00:00', NULL);
id  user_id project_id  start_date            end_date              closed_date
1   1       1           2019-05-27 00:00:00   2021-01-15 00:00:00   NULL
2   2       2           2020-05-27 00:00:00   2021-02-01 00:00:00   2020-02-05 00:00:00
3   3       3           2020-05-27 00:00:00   2024-02-01 00:00:00   2020-02-05 00:00:00
4   4       4           2020-05-27 00:00:00   2021-03-05 00:00:00   NULL

结果将显示每个月有多少不同的用户有一个活跃的项目(仅自今年年初以来)。

所以对于上面的数据集,我们可以看到所有 4 个项目在 2021 年 1 月月份都是“活跃的”,因为结束日期在未来。

2021-01-31 | 3 (4 projects were active in some way, during January)
2021-02-28 | 2 (3 projects were active in some way, during February)
2021-03-31 | 1 (1 project was active in some way, during March)

最后一点复杂性是,有时项目可以在 EndDate 之前关闭,我想排除任何 endDate 在未来但项目实际上已经关闭的用户。

例如,在上面的数据集中,第三个项目的结束日期为 2024 年 2 月,但该项目于 2021 年 2 月结束。所以从技术上讲,这个人在 2021 年 1 月和 2 月是活跃的,但不是 2021 年 3 月及以后。

ps 希望得到 Redshift 的答案

redshift
  • 1 1 个回答
  • 106 Views

1 个回答

  • Voted
  1. Best Answer
    Akina
    2021-03-23T22:15:06+08:002021-03-23T22:15:06+08:00

    ps 希望在 Postgres 中得到答案(我正在使用 Redshift)

    PostgreSQL 的解决方案:

    WITH cte AS ( SELECT '2021-01-01'::DATE AS month_start, '2021-01-31'::DATE AS month_end UNION ALL
                  SELECT '2021-02-01', '2021-02-28' UNION ALL
                  SELECT '2021-03-01', '2021-03-31' )
    
    SELECT cte.month_end, COUNT(*)
    FROM roster
    CROSS JOIN cte
    WHERE start_date <= month_end 
      AND LEAST(end_date, closed_date) >= month_start
    GROUP BY cte.month_end
    ORDER BY cte.month_end;
    

    或者

    WITH cte AS ( SELECT '2021-01-31'::DATE AS month_end UNION ALL
                  SELECT '2021-02-28' UNION ALL
                  SELECT '2021-03-31' )
    
    SELECT cte.month_end, COUNT(*)
    FROM roster
    CROSS JOIN cte
    WHERE start_date <= month_end 
      AND LEAST(end_date, closed_date) >= DATE_TRUNC('month', month_end)
    GROUP BY cte.month_end
    ORDER BY cte.month_end;
    

    https://dbfiddle.uk/?rdbms=postgres_12&fiddle=710260e9fa44a89cc9c2d536739f7c92

    用您需要的代码替换cte代码(在第一个变体中为感兴趣的月份生成月份的第一天和最后几天,或者在第二个变体中仅生成最后几天的月份)。例如,使用日期生成器,从YYYY-MM-01下个月开始表示感兴趣的月份,步骤为一个月,然后减去 - 一个月的第一天和最后一天的一天。

    • 2

相关问题

  • 查询多个连接的行

  • Redshift 中 varchar 长度的存储大小

  • 这两种 SQL 样式之间有什么区别?

  • 使用 Amazon Redshift 作为缓存时的最佳实践是什么

  • 存储、查询和更新 300M 行数据的最佳方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve