如何显示 "each" 个月的最新记录,仅给出每条记录的日期范围
How to show the latest record for "each" month given only a date range of each records
我有一个历史记录 table,其中包含每个客户的记录,其中 start_date 和 end_date 列指示每行的有效期。 table 看起来像这样:
| ID | Name | Code |start_date (Timestamp) |end_date (Timestamp) |
|:--- |:----:|:-----:|----------------------:|--------------------:|
|123 | John | 100 |2021/1/6 8:00:00 |2021/1/31 8:00:00 |
|123 | John | 101 |2021/1/31 8:00:00 |2021/2/15 8:00:00 |
|123 | John | 102 |2021/2/15 8:00:00 |2021/3/15 8:00:00 |
|123 | John | 103 |2021/3/15 8:00:00 |2021/6/15 9:00:00 |
|123 | John | 105 |2021/6/15 9:00:00 |2021/6/15 9:15:00 |
|123 | John | 106 |2021/6/15 9:15:00 |2021/6/15 10:00:00 |
|123 | John | 107 |2021/6/15 10:00:00 |2021/7/15 15:00:00 |
|123 | John | 108 |2021/7/15 15:00:00 |null |
我决定使用“扩展”功能来生成一个显示“每月”记录(每个月末记录)的列。所需的输出应如下所示,如果 end_date 为空,则应将记录扩展到当前日期,但我的扩展语法无法正常工作:
| ID | Name | Code |start_date (Timestamp) |end_date (Timestamp) |end_of_month
|---- |------|-------|-----------------------|---------------------|------------
|123 | John | 101 |2021/1/31 8:00 |2021/2/15 8:00:00 | 2021/1/31
|123 | John | 102 |2021/2/15 8:00 |2021/3/15 8:00:00 | 2021/2/28
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/3/31
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/4/30
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/5/31
|123 | John | 107 |2021/6/15 10:00 |2021/7/15 15:00:00 | 2021/6/30
|123 | John | 108 |2021/7/15 15:00 |? | 2021/7/30
|123 | John | 108 |2021/7/15 15:00 |? | 2021/8/31
|123 | John | 108 |2021/7/15 15:00 |? | 2021/9/30
我有以下 sql 但它不包括最后一条记录,即 end_of 月“2021/9/30”。
如果我将 ANCHOR 设置为“MONTH_BEGIN”,则会出现记录“2021/9/30”,但它会排除 return.
中的记录 2021/7/30
select a.id, a.name, a.code, a.start_date, a.end_date, last_day(BEGIN(bg2))
as end_of_month from (select id, name, code, start_date, end_date,
period(start_date,COALESCE(MIN(start_date) OVER (PARTITION BY name ORDER BY start_date
ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING), CURRENT_TIMESTAMP)) as bg from CHESS.HST) as a
expand on bg as bg2 by ANCHOR MONTH_END
这似乎符合您要求的结果:
select a.id, a.name, a.code, a.start_date, a.end_date,
last_day(begin(bg2)) as end_of_month
from
(
select id, name, code, start_date, end_date,
period(start_date,COALESCE(end_date
,add_months(CURRENT_TIMESTAMP(0), 1)
)
) as bg
from HST
) as a
expand on bg as bg2
by ANCHOR MONTH_END
我有一个历史记录 table,其中包含每个客户的记录,其中 start_date 和 end_date 列指示每行的有效期。 table 看起来像这样:
| ID | Name | Code |start_date (Timestamp) |end_date (Timestamp) |
|:--- |:----:|:-----:|----------------------:|--------------------:|
|123 | John | 100 |2021/1/6 8:00:00 |2021/1/31 8:00:00 |
|123 | John | 101 |2021/1/31 8:00:00 |2021/2/15 8:00:00 |
|123 | John | 102 |2021/2/15 8:00:00 |2021/3/15 8:00:00 |
|123 | John | 103 |2021/3/15 8:00:00 |2021/6/15 9:00:00 |
|123 | John | 105 |2021/6/15 9:00:00 |2021/6/15 9:15:00 |
|123 | John | 106 |2021/6/15 9:15:00 |2021/6/15 10:00:00 |
|123 | John | 107 |2021/6/15 10:00:00 |2021/7/15 15:00:00 |
|123 | John | 108 |2021/7/15 15:00:00 |null |
我决定使用“扩展”功能来生成一个显示“每月”记录(每个月末记录)的列。所需的输出应如下所示,如果 end_date 为空,则应将记录扩展到当前日期,但我的扩展语法无法正常工作:
| ID | Name | Code |start_date (Timestamp) |end_date (Timestamp) |end_of_month
|---- |------|-------|-----------------------|---------------------|------------
|123 | John | 101 |2021/1/31 8:00 |2021/2/15 8:00:00 | 2021/1/31
|123 | John | 102 |2021/2/15 8:00 |2021/3/15 8:00:00 | 2021/2/28
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/3/31
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/4/30
|123 | John | 103 |2021/3/15 8:00 |2021/6/15 9:00:00 | 2021/5/31
|123 | John | 107 |2021/6/15 10:00 |2021/7/15 15:00:00 | 2021/6/30
|123 | John | 108 |2021/7/15 15:00 |? | 2021/7/30
|123 | John | 108 |2021/7/15 15:00 |? | 2021/8/31
|123 | John | 108 |2021/7/15 15:00 |? | 2021/9/30
我有以下 sql 但它不包括最后一条记录,即 end_of 月“2021/9/30”。 如果我将 ANCHOR 设置为“MONTH_BEGIN”,则会出现记录“2021/9/30”,但它会排除 return.
中的记录 2021/7/30select a.id, a.name, a.code, a.start_date, a.end_date, last_day(BEGIN(bg2))
as end_of_month from (select id, name, code, start_date, end_date,
period(start_date,COALESCE(MIN(start_date) OVER (PARTITION BY name ORDER BY start_date
ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING), CURRENT_TIMESTAMP)) as bg from CHESS.HST) as a
expand on bg as bg2 by ANCHOR MONTH_END
这似乎符合您要求的结果:
select a.id, a.name, a.code, a.start_date, a.end_date,
last_day(begin(bg2)) as end_of_month
from
(
select id, name, code, start_date, end_date,
period(start_date,COALESCE(end_date
,add_months(CURRENT_TIMESTAMP(0), 1)
)
) as bg
from HST
) as a
expand on bg as bg2
by ANCHOR MONTH_END