clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3364508 fiddles created (36297 in the last week).

create table calendar ( id serial primary key, cal_day date not null, business_day bool not null );
 hidden batch(es)


insert into calendar (cal_day, business_day) values ('20180101', false), ('20180102', true), ('20180103', false), ('20180104', true), ('20180105', false), ('20180106', true), ('20180107', false), ('20180108', true), ('20180109', false), ('20180110', true), ('20180111', false), ('20180112', true);
12 rows affected
 hidden batch(es)


select max(cal_day) as the_day from ( select cal_day from calendar where cal_day > '20180103' and business_day = true order by cal_day limit 4 ) bd;
the_day
2018-01-10
 hidden batch(es)


select cal_day from (select cal_day, row_number() over (order by cal_day) rn from calendar where cal_day > '20180103' and business_day = true limit 4) bd where rn = 4;
cal_day
2018-01-10
 hidden batch(es)


create or replace function add_business_day(from_date date, num_days int) returns date as $fbd$ select max(cal_day) as the_day from (select cal_day from calendar where cal_day > $1 and business_day = true order by cal_day limit $2) bd; $fbd$ language sql;
 hidden batch(es)


create or replace function add_business_day2(from_date date, num_days int) returns date as $fbd$ select cal_day from (select cal_day, row_number() over (order by cal_day) rn from calendar where cal_day > $1 and business_day = true limit $2) bd where rn = $2; $fbd$ language sql;
 hidden batch(es)


select add_business_day('20180103', 4);
add_business_day
2018-01-10
 hidden batch(es)


select add_business_day2('20180103', 4)
add_business_day2
2018-01-10
 hidden batch(es)