clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2335751 fiddles created (27466 in the last week).

create table users (id int primary key, weeks_to_live int not null);
 hidden batch(es)


insert into users values (1, 3), (2, 2);
2 rows affected
 hidden batch(es)


create table calendar (id int primary key, user_id int not null references users);
 hidden batch(es)


insert into calendar values (1, 1), (2, 2);
2 rows affected
 hidden batch(es)


create table calendar_field ( id serial primary key, text text, rating int, calendar_id int not null references calendar, week_number int not null );
 hidden batch(es)


insert into calendar_field (text, rating, calendar_id, week_number) select '', 0, c.id, g.wn from calendar c join users u on u.id = c.user_id cross join generate_series(0, u.weeks_to_live) as g(wn);
7 rows affected
 hidden batch(es)


select * from calendar_field;
id text rating calendar_id week_number
1 0 1 0
2 0 1 1
3 0 1 2
4 0 1 3
5 0 2 0
6 0 2 1
7 0 2 2
 hidden batch(es)