【问题标题】:Date Ranges into Monthly Breakdown日期范围到月度细分
【发布时间】:2022-12-18 14:10:16
【问题描述】:

您好,我有一个 SQL 问题,我正在尝试获取特定日期范围内每个人的月末记录。本质上,如果开始日期和结束日期在每个月的最后一天之内,我希望使用某种月末记录来跟踪历史记录(多年的数据)。所以数据目前看起来像这样(为简单起见,仅使用 2022)。

Name StartDate EndDate
John Smith 2022-01-15 2022-04-10
Jane Doe 2022-01-18 2022-03-05
Rob Johnson 2022-03-07 2022-07-18

而我正在寻找的是这样的东西

Name StartDate EndDate EndMonth
John Smith 2022-01-15 2022-04-10 2022-01-31
Jane Doe 2022-01-18 2022-03-05 2022-01-31
John Smith 2022-01-15 2022-04-10 2022-02-28
Jane Doe 2022-01-18 2022-03-05 2022-02-28
John Smith 2022-01-15 2022-04-10 2022-03-31
Rob Johnson 2022-03-07 2022-07-18 2022-03-31
Rob Johnson 2022-03-07 2022-07-18 2022-04-30
Rob Johnson 2022-03-07 2022-07-18 2022-05-31

ETC...

我尝试将 Records 表与我拥有的日历表连接起来,该表具有几年前每天的月末数据,但无法弄清楚。日历表看起来像这样......

Date EndMonth
2022-01-01 2022-01-31
2022-01-02 2022-01-31
.....

【问题讨论】:

    标签: sql date teradata


    【解决方案1】:

    JOINing 两个表应该给出期望的结果。


    你没有提到初始表的表名, 所以我将其称为 person 表。

    您的calendar 表是一个好的开始。 我认为您不需要 Date 列。 只需一个 EndMonth 列就足够了。


    根据日历加入人, 其中 EndMonth 在 StartDate 和 EndDate 之间。

    你完成了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多