RosettaCodeData/Task/Five-weekends/DuckDB/five-weekends.duckdb

20 lines
742 B
Plaintext

create or replace function day_before(day) as (6+day) % 7;
create or replace function weekday_of_last_day_of_month(year, month) as (
if(month=12,
day_before( extract('dayofweek' from make_date(year+1, 1, 1) )),
day_before( extract('dayofweek' from make_date( year, month+1, 1 ) ) ))
);
# The only case where the month has 5 full weekends is when the last day
# of the month falls on a Sunday (0) and the month has 31 days.
#
create or replace function five_weekends(start, stop) as table (
select year, month
from range(start, stop) as t(year),
unnest([1,3,5,7,8,10,12]) as u(month) -- months with 31 days
where weekday_of_last_day_of_month(year, month) = 0
);
from five_weekends(1900, 2101) order by year, month;