I want to generate Yearmonth (YYYYMM) for each quarters between two years and associate a rank number for each line.
Date_Begin | Date_End |
---|---|
2021 | 2023 |
I retrieve a yearmonthrank as below:
SELECT GENERATE_ARRAY(1,((Date_End-Date_Begin )*4)) as period_rank
...
UNNEST(period_rank) AS period_yearmonthrank
Which returns:
period_rank |
---|
1 |
2 |
... |
12 |
Now, I have no idea how I could generate this output for yearmonth part?
period_rank | yearmonth |
---|---|
1 | 202103 |
2 | 202106 |
3 | 202109 |
4 | 202112 |
5 | 202203 |
6 | 202206 |
7 | 202209 |
8 | 202212 |
9 | 202303 |
10 | 202306 |
11 | 202309 |
12 | 202312 |
Thank you for your time!
CodePudding user response:
Try this one:
SELECT RANK() OVER (ORDER BY y, q) AS period_rank,
FORMAT('%dd', y, q) AS yearmonth,
FROM UNNEST(GENERATE_ARRAY(2021, 2023)) y, UNNEST([3, 6, 9, 12]) q
;
output: