I have a history table that contains the records of each customer with start_date and end_date columns that indicate validity period of each rows. The table looks like this:
| 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 |
I decided to use "expand on" function to produce a column that would show "monthly" records (end of each month records). Desired output should look like this, where if end_date is null it should expand the record up until the current date but my expand syntax does not work correctly:
| 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
I have below sql but it excludes the last record which is end_of month "2021/9/30". If I set the ANCHOR to "MONTH_BEGIN", The record "2021/9/30" will appear but it will exclude the record 2021/7/30 in return.
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
This seems to match your requested result:
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