The following SQL script generates 2022 year calendar with months, quarters, half-years and days count for each calendar interval:
with clndr as
(
select
gs_day::date
from
generate_series('2022-01-01'::timestamp, current_timestamp, '1 day') as gs_day
),
draft as
(
select
gs_day::date as gs_day
,count(*) over() as full_year_days_cnt
,date_trunc( 'month', gs_day)::date as mnth
,count(*) over(partition by to_char(gs_day::date , 'MM')) as full_month_days_cnt
,case
when to_char(gs_day::date , 'MM') in ('01', '02', '03') then 1
when to_char(gs_day::date , 'MM') in ('04', '05', '06') then 2
when to_char(gs_day::date , 'MM') in ('07', '08', '09') then 3
when to_char(gs_day::date , 'MM') in ('10', '11', '12') then 4
end as year_quarter
,case
when to_char(gs_day::date, 'MM') in ('01', '02', '03', '04', '05', '06') then 1
when to_char(gs_day::date, 'MM') in ('07', '08', '09', '10', '11', '12') then 2
else null
end as year_half
from
generate_series('2022-01-01'::timestamp, current_timestamp, '1 day') as gs_day
)
select
gs_day
,full_year_days_cnt
,mnth
,full_month_days_cnt
,year_quarter
,count(*) over(partition by year_quarter) as year_quarter_days_cnt
,year_half
,count(*) over(partition by year_half) as year_half_days_cnt
from
draft
Unfortunately, I have to use cte "draft" to count "year_quarter_days_cnt" and "year_half_days_cnt", because I didn't find any way to refer to "year_quarter" and "year_half" aliases in select statement of draft cte.
Is there any possibilities to avoid cte or joins to get the same result using count window function?