Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with a as (select contract_id from contracts where contact_id=106 and start_date <= CURRENT_DATE and end_date >= CURRENT_DATE),
- b as (select schedule_id from scheduled_contracts where contract_id in (select * from a)),
- c as (select schedule_id from schedules s where s.schedule_id in (select * from b)
- and s.start_date <= CURRENT_DATE and s.end_date >= CURRENT_DATE),
- d as (select schedule_id, start_date from schedules s where s.schedule_id in (select * from b)
- and s.start_date > CURRENT_DATE),
- e as (select schedule_id, count(*) as num_sessions from sessions where schedule_id
- in (select * from c) group by schedule_id order by num_sessions desc limit 1),
- f as (select s.schedule_id, d.start_date, count(*) as num_sessions from sessions s inner join d on s.schedule_id = d.schedule_id where s.schedule_id
- in (select schedule_id from d) group by s.schedule_id, d.start_date order by d.start_date asc, num_sessions desc limit 1),
- g as (select schedule_id as holder,
- case
- when (select count(*) from e) = 0 then
- case
- when (select count(*) from f) = 0 then null
- else (select schedule_id from f)
- end
- else (select schedule_id from e)
- end from b),
- h as (select distinct schedule_id from g),
- i as (select term_program_location_id from term_program_location_schedules tpls where tpls.schedule_id in (select * from h)),
- j as (select tpl.term_id, tpl.program_region_id, tpl.team_program_id from term_program_location tpl where tpl.term_program_location_id in (select * from i))
- select tpl.program_region_id, tpl.term_id, tp.program_id from j tpl, team_program tp where tp.team_program_id = tpl.team_program_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement