Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select r.id
- ,b.from_dt
- ,b.to_dt
- , proom.name||', койка №'||b.number||coalesce(', '||bprof.name,'')||', '||dep.name,
- bprof.id as bed_profile, bprof.name as bed_profile_name
- FROM md_bed_resource AS r INNER JOIN md_bed AS b ON r.bed_id = b.id join md_room room on b.room_id = room.id
- join pim_room proom on room.id = proom.id
- left join md_bed_profile bprof on b.bed_profile_id=bprof.id
- join pim_department dep on proom.department_id = dep.id
- join pim_organization org on dep.org_id = org.id where upper(proom.name||', койка №'||b.number||coalesce(', '||bprof.name,'')||', '||dep.name) like '%'||upper('')||'%'
- and org.id = 123 and (dep.id = 1293)
- and (
- to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy hh24:mi:ss')::timestamp is null and 13264557::int4 is null or
- to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp is null
- or
- not exists
- (
- select 1 from sr_res_group_relationship rgrel where rgrel.resource_id = r.id and
- exists(select 1 from mc_step s where s.res_group_id = rgrel.group_id) and
- (not
- (
- (
- 13264557::int4 is not null and
- (select max(edatetime) from sr_res_group_relationship where group_id = 13264557 and
- role_id in (select id from sr_res_role where resource_kind_id = 4)) > rgrel.edatetime
- or
- to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy hh24:mi:ss')::timestamp > rgrel.edatetime
- ) or
- to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp < rgrel.bdatetime
- )
- or to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp > rgrel.bdatetime and rgrel.edatetime is null
- )
- )
- )
- /*
- and (
- (b.from_dt is null and b.to_dt >= coalesce(to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy'), to_date('26.03.2017','dd.mm.yyyy')))
- or (b.to_dt is null and b.from_dt <= coalesce(to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy'), to_date('26.03.2017','dd.mm.yyyy')))
- or (b.to_dt is null and b.from_dt is null)
- or (b.from_dt <= coalesce(to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy'), to_date('26.03.2017','dd.mm.yyyy'))
- and b.to_dt >= coalesce(to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy'), to_date('26.03.2017 ','dd.mm.yyyy')))
- )
- */
- ;
- select r.id
- ,b.from_dt
- ,b.to_dt
- , proom.name||', койка №'||b.number||coalesce(', '||bprof.name,'')||', '||dep.name,
- bprof.id as bed_profile, bprof.name as bed_profile_name
- FROM md_bed_resource AS r INNER JOIN md_bed AS b ON r.bed_id = b.id join md_room room on b.room_id = room.id
- join pim_room proom on room.id = proom.id
- left join md_bed_profile bprof on b.bed_profile_id=bprof.id
- join pim_department dep on proom.department_id = dep.id
- join pim_organization org on dep.org_id = org.id where upper(proom.name||', койка №'||b.number||coalesce(', '||bprof.name,'')||', '||dep.name) like '%'||upper('')||'%'
- and org.id = 123 and (dep.id = 1293)
- and (
- to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy hh24:mi:ss')::timestamp is null and null::int4 is null or
- to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp is null
- or
- not exists
- (
- select 1 from sr_res_group_relationship rgrel where rgrel.resource_id = r.id and
- exists(select 1 from mc_step s where s.res_group_id = rgrel.group_id) and
- (not
- (
- (
- null::int4 is not null and
- (select max(edatetime) from sr_res_group_relationship where group_id = 13264557 and
- role_id in (select id from sr_res_role where resource_kind_id = 4)) > rgrel.edatetime
- or
- to_timestamp('29.10.2016 18:50', 'dd.MM.yyyy hh24:mi:ss')::timestamp > rgrel.edatetime
- ) or
- to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp < rgrel.bdatetime
- )
- or to_timestamp('08.11.2016 13:44', 'dd.MM.yyyy hh24:mi:ss')::timestamp > rgrel.bdatetime and rgrel.edatetime is null
- )
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement