-- TOP SL, FIXDATE 26.06.2023, JIRA PROMEDWEB-158376
            with curDT as (select localtimestamp as curDate)
            
                select
                    msf.MedStaffFact_id as "MedStaffFact_id",
                    msf.LpuSection_id as "LpuSection_id",
                    msf.MedPersonal_id as "MedPersonal_id",
                    coalesce(ls.LpuSection_FullName, '') as "LpuSection_Name",
                    coalesce(ls.LpuSection_Name, '') as "LpuSection_Nick",
                    coalesce(ps.PostMed_Name, vp.name,'') as "PostMed_Name",
                    ps.PostMed_Code as "PostMed_Code",
                    ps.PostMed_id as "PostMed_id",
                    lb.LpuBuilding_id as "LpuBuilding_id",
                    coalesce(lb.LpuBuilding_Name, '') as "LpuBuilding_Name",
                    lu.LpuUnit_id as "LpuUnit_id",
                    lu.LpuUnitSet_id as "LpuUnitSet_id",
                    coalesce(lu.LpuUnit_Name, '') as "LpuUnit_Name",
                    case when lut.LpuUnitType_SysNick in ('polka','ccenter','traumcenter','fap')
                        then case when exists(select from v_TimeTableGraf_lite tt where msf.MedStaffFact_id = tt.MedStaffFact_id and tt.TimetableGraf_Time is not null)
                                then 'true'
                                else 'false'
                            end
                        when lut.LpuUnitType_SysNick = 'parka' then case when (select count(*) from v_TimetablePar tt where msf.LpuSection_id = tt.LpuSection_id) > 0
                                then 'true'
                                else 'false'
                            end
                        when lut.LpuUnitType_SysNick in ('stac', 'hstac', 'pstac', 'dstac') then
                            case when (select count(*) from v_TimetableStac_lite tt where msf.LpuSection_id = tt.LpuSection_id) > 0
                                then 'true'
                                else 'false'
                            end
                        else 'false'
                    end as "Timetable_isExists",
                    lut.LpuUnitType_SysNick as "LpuUnitType_SysNick",
                    lu.LpuUnitType_id as "LpuUnitType_id",
                    lsp.LpuSectionProfile_SysNick as "LpuSectionProfile_SysNick",
                    lsp.LpuSectionProfile_Code as "LpuSectionProfile_Code",
                    lsp.LpuSectionProfile_id as "LpuSectionProfile_id",
                    ls.LpuSectionAge_id as "LpuSectionAge_id",
                    null::bigint as "MedService_id",
                    null as "MedService_Nick",
                    null as "MedService_Name",
                    null::bigint as "MedServiceType_id",
                    null::bigint as "MedService_IsExternal",
                    null::bigint as "AuthorizationIS_id",
                    null::bigint as "MedService_IsDisplayByFilter",
                    null as "MedServiceType_SysNick",
                    msf.Person_FIO as "MedPersonal_FIO",
                    Lpu.Org_id as "Org_id",
                    Lpu.Lpu_id as "Lpu_id",
                    Lpu.Lpu_Nick as "Org_Nick",
                    Lpu.Lpu_Nick as "Lpu_Nick",
                    null::bigint as "MedStaffFactLink_id",
                    null as "MedStaffFactLink_begDT",
                    null as "MedStaffFactLink_endDT",
                    msf.MedStaffFactCache_IsDisableInDoc as "MedStaffFactCache_IsDisableInDoc",
                    eq.ElectronicQueueInfo_id as "ElectronicQueueInfo_id",
                    eq.ElectronicService_id as "ElectronicService_id",
                    eq.MedServiceElectronicQueue_id as "MedServiceElectronicQueue_id",
                    eq.MedServiceMedPersonal_id as "MedServiceMedPersonal_id",
                    eq.ElectronicService_Num as "ElectronicService_Num",
                    eq.ElectronicQueueInfo_CallTimeSec as "ElectronicQueueInfo_CallTimeSec",
                    eq.ElectronicQueueInfo_PersCallDelTimeMin as "ElectronicQueueInfo_PersCallDelTimeMin",
                    eq.ElectronicQueueInfo_InterruptTimeMin as "ElectronicQueueInfo_InterruptTimeMin",
                    eq.ElectronicQueueInfo_CallCount as "ElectronicQueueInfo_CallCount",
                    eq.ElectronicService_isShownET as "ElectronicService_isShownET",
                    eq.ElectronicQueueInfo_IsAutoReg as "ElectronicQueueInfo_IsAutoReg",
                    eq.ElectronicQueueInfo_QueueTimeMin as "ElectronicQueueInfo_QueueTimeMin",
                    (
                        select
                            string_agg(coalesce(CAST(etl.ElectronicTreatment_id as VARCHAR),''), ',')
                        from v_ElectronicTreatmentLink etl
                            inner join v_ElectronicQueueInfo eqio on eqio.ElectronicQueueInfo_id = etl.ElectronicQueueInfo_id
                            inner join v_ElectronicService eso on eso.ElectronicQueueInfo_id = eqio.ElectronicQueueInfo_id
                        where eso.ElectronicService_id = eq.ElectronicService_id
                    ) as "ElectronicTreatment_ids",
                    eboard.ElectronicScoreboard_id as "ElectronicScoreboard_id",
                    eboard.ElectronicScoreboard_IPaddress as "ElectronicScoreboard_IPaddress",
                    eboard.ElectronicScoreboard_Port as "ElectronicScoreboard_Port",
                    null::bigint as "SmpUnitType_Code",
                    null::bigint as "SmpUnitParam_IsKTPrint",
                    null::bigint as "Storage_id",
                    null::bigint as "Storage_pid",
                    ps.FRMPPostMed_id as "FRMPPostMed_id"
                    
                    ,msf.MedicalCareKind_id as "MedicalCareKind_id"
                    ,msf.PostKind_id as "PostKind_id"
                    ,null::varchar as "MSF_LpuSection_Name"
                
                from
                    v_MedStaffFact msf
                    left join v_Lpu lpu on lpu.Lpu_id = msf.Lpu_id
                    left join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
                    left join v_LpuSectionProfile lsp on lsp.LpuSectionProfile_id = ls.LpuSectionProfile_id
                    left join v_LpuBuilding lb on lb.LpuBuilding_id = ls.LpuBuilding_id
                    left join v_LpuUnit lu on lu.LpuUnit_id = ls.LpuUnit_id
                    left join v_LpuUnitType lut on lut.LpuUnitType_id = lu.LpuUnitType_id
                    left join persis.v_Post vp on vp.id = msf.Post_id
                    left join v_PostMed ps on ps.PostMed_id = msf.Post_id
                    left join lateral (
                        select
                            eqi.ElectronicQueueInfo_id,
                            mseq.ElectronicService_id,
                            mseq.MedServiceElectronicQueue_id,
                            mseq.MedServiceMedPersonal_id,
                            es.ElectronicService_Num,
                            eqi.ElectronicQueueInfo_CallTimeSec,
                            eqi.ElectronicQueueInfo_PersCallDelTimeMin,
                            coalesce(eqi.ElectronicQueueInfo_InterruptTimeMin, 0) as ElectronicQueueInfo_InterruptTimeMin,
                            eqi.ElectronicQueueInfo_CallCount,
                            eqi.ElectronicQueueInfo_QueueTimeMin,
                            case when eqi.ElectronicQueueInfo_IsAutoReg = 2 then 1 else null end as ElectronicQueueInfo_IsAutoReg,
                            case when es.ElectronicService_isShownET = 2 then 1 else null end as ElectronicService_isShownET
                        from
                            v_MedServiceElectronicQueue mseq
                            left join v_ElectronicService es on es.ElectronicService_id = mseq.ElectronicService_id
                            left join v_ElectronicQueueInfo eqi on eqi.ElectronicQueueInfo_id = es.ElectronicQueueInfo_id
                            left join v_ElectronicScoreboardQueueLink esql on esql.ElectronicQueueInfo_id = eqi.ElectronicQueueInfo_id
                        where mseq.MedStaffFact_id = msf.MedStaffFact_id
                		  and eqi.ElectronicQueueInfo_IsOff = 1
                        limit 1
                    ) as eq on true
                    left join lateral (
                        select
                            ebd.ElectronicScoreboard_id,
                            ebd.ElectronicScoreboard_IPaddress,
                            ebd.ElectronicScoreboard_Port
                        from
                        	v_ElectronicScoreboard ebd
                            left join v_ElectronicScoreboardQueueLink esql on esql.ElectronicService_id = eq.ElectronicService_id
                        where ebd.ElectronicScoreboard_id = esql.ElectronicScoreboard_id
                          and ebd.ElectronicScoreboard_IsLED = 2
                        limit 1
                    ) as eboard on true
                
                where msf.MedPersonal_id = '180101000004491'
                  and msf.MedStaffFact_Stavka > 0
                  and lpu.Org_id = 180101000000049 
                  
                    and msf.WorkData_begDate::date <= (select curDate from curDT)
                    and (msf.WorkData_endDate::date >= (select curDate from curDT) or msf.WorkData_endDate is null)
                  and msf.MedStaffFact_id = '180101000124234' and ls.LpuSection_id = '180101000000630'
            
             union all 
                select distinct
                    msf.MedStaffFact_id as "MedStaffFact_id",
                    ls.LpuSection_id as "LpuSection_id",
                    msf.MedPersonal_id as "MedPersonal_id",
                    coalesce(ls.LpuSection_FullName, '') as "LpuSection_Name",
                    coalesce(ls.LpuSection_Name, '') as "LpuSection_Nick",
                    coalesce(ps.PostMed_Name, '') as "PostMed_Name",
                    ps.PostMed_Code as "PostMed_Code",
                    ps.PostMed_id as "PostMed_id",
                    lb.LpuBuilding_id as "LpuBuilding_id",
                    coalesce(lb.LpuBuilding_Name, '') as "LpuBuilding_Name",
                    lu.LpuUnit_id as "LpuUnit_id",
                    lu.LpuUnitSet_id as "LpuUnitSet_id",
                    coalesce(lu.LpuUnit_Name, '') as "LpuUnit_Name",
                    case
                        when lut.LpuUnitType_SysNick in ('polka','ccenter','traumcenter','fap') then
                            case when exists(select from v_TimeTableGraf_lite tt where msf.MedStaffFact_id = tt.MedStaffFact_id and tt.TimetableGraf_Time is not null) then 'true' else 'false' end
                        when lut.LpuUnitType_SysNick = 'parka' then
                            case when (select count(*) from v_TimetablePar tt where msf.LpuSection_id = tt.LpuSection_id) > 0 then 'true' else 'false' end
                        when lut.LpuUnitType_SysNick in ('stac','hstac','pstac','dstac') then
                            case when (select count(*) from v_TimetableStac_lite tt where msf.LpuSection_id = tt.LpuSection_id) > 0 then 'true' else 'false' end
                        else 'false'
                    end as "Timetable_isExists",
                    lut.LpuUnitType_SysNick as "LpuUnitType_SysNick",
                    lu.LpuUnitType_id as "LpuUnitType_id",
                    lsp.LpuSectionProfile_SysNick as "LpuSectionProfile_SysNick",
                    lsp.LpuSectionProfile_Code as "LpuSectionProfile_Code",
                    lsp.LpuSectionProfile_id as "LpuSectionProfile_id",
                    ls.LpuSectionAge_id as "LpuSectionAge_id",
                    null::bigint as "MedService_id",
                    null as "MedService_Nick",
                    null as "MedService_Name",
                    null::bigint as "MedServiceType_id",
                    null::bigint as "MedService_IsExternal",
                    null::bigint as "AuthorizationIS_id",
                    null::bigint as "MedService_IsDisplayByFilter",
                    null as "MedServiceType_SysNick",
                    msf.Person_FIO as "MedPersonal_FIO",
                    Lpu.Org_id as "Org_id",
                    Lpu.Lpu_id as "Lpu_id",
                    Lpu.Lpu_Nick as "Org_Nick",
                    Lpu.Lpu_Nick as "Lpu_Nick",
                    null::bigint as "MedStaffFactLink_id",
                    null as "MedStaffFactLink_begDT",
                    null as "MedStaffFactLink_endDT",
                    msf.MedStaffFactCache_IsDisableInDoc as "MedStaffFactCache_IsDisableInDoc",
                    null::bigint as "ElectronicQueueInfo_id",
                    null::bigint as "ElectronicService_id",
                    null::bigint as "MedServiceElectronicQueue_id",
                    null::bigint as "MedServiceMedPersonal_id",
                    null::bigint as "ElectronicService_Num",
                    null::bigint as "ElectronicQueueInfo_CallTimeSec",
                    null::bigint as "ElectronicQueueInfo_PersCallDelTimeMin",
                    null::bigint as "ElectronicQueueInfo_InterruptTimeMin",
                    null::bigint as "ElectronicQueueInfo_CallCount",
                    null::bigint as "ElectronicService_isShownET",
                    null::bigint as "ElectronicQueueInfo_IsAutoReg",
                    null::bigint as "ElectronicQueueInfo_QueueTimeMin",
                    null as "ElectronicTreatment_ids",
                    null::bigint as "ElectronicScoreboard_id",
                    null as "ElectronicScoreboard_IPaddress",
                    null::bigint as "ElectronicScoreboard_Port",
                    null::bigint as "SmpUnitType_Code",
                    null::bigint as "SmpUnitParam_IsKTPrint",
                    null::bigint as "Storage_id",
                    null::bigint as "Storage_pid",
                    ps.FRMPPostMed_id as "FRMPPostMed_id"
                    
                    ,msf.MedicalCareKind_id as "MedicalCareKind_id"
                    ,msf.PostKind_id as "PostKind_id"
                    ,null::varchar as "MSF_LpuSection_Name"
                
                from
                    v_MedStaffFact msf
                    left join v_Lpu lpu on lpu.Lpu_id = msf.Lpu_id
                    inner join lateral (
                        select
                            WG.WorkGraph_id
                        from v_WorkGraph WG
                        where
                            WG.MedStaffFact_id = msf.MedStaffFact_id
                            and WG.WorkGraph_begDT <= (select curDate from curDT)
                            and WG.WorkGraph_endDT >= (select curDate from curDT)
                    ) as WG on true
                    left join v_WorkGraphLpuSection WGLS on WGLS.WorkGraph_id = WG.WorkGraph_id
                    left join v_LpuSection ls on ls.LpuSection_id = WGLS.LpuSection_id
                    left join v_LpuSectionProfile lsp on lsp.LpuSectionProfile_id = ls.LpuSectionProfile_id
                    left join v_LpuBuilding lb on lb.LpuBuilding_id = ls.LpuBuilding_id
                    left join v_LpuUnit lu on lu.LpuUnit_id = ls.LpuUnit_id
                    left join v_LpuUnitType lut on lut.LpuUnitType_id = lu.LpuUnitType_id
                    left join v_PostMed ps on ps.PostMed_id = msf.Post_id
                where msf.MedPersonal_id = '180101000004491'
                  and msf.Lpu_id = '180101000000050'
                   and msf.MedStaffFact_id = '180101000124234' and ls.LpuSection_id = '180101000000630'
             union all 
                select
                    msf.MedStaffFact_id as "MedStaffFact_id",
                    msf.LpuSection_id as "LpuSection_id",
                    msf.MedPersonal_id as "MedPersonal_id",
                    coalesce(ls.LpuSection_FullName, '') as "LpuSection_Name",
                    coalesce(ls.LpuSection_Name, '') as "LpuSection_Nick",
                    coalesce(ps.PostMed_Name, '') as "PostMed_Name",
                    ps.PostMed_Code as "PostMed_Code",
                    ps.PostMed_id as "PostMed_id",
                    lb.LpuBuilding_id as "LpuBuilding_id",
                    coalesce(lb.LpuBuilding_Name, '') as "LpuBuilding_Name",
                    lu.LpuUnit_id as "LpuUnit_id",
                    lu.LpuUnitSet_id as "LpuUnitSet_id",
                    coalesce(lu.LpuUnit_Name, '') as "LpuUnit_Name",
                    case when exists(select from v_TimeTableGraf_lite tt where msf.MedStaffFact_id = tt.MedStaffFact_id and tt.TimetableGraf_Time is not null)
                        then 'true'
                        else 'false'
                    end as "Timetable_isExists",
                    lut.LpuUnitType_SysNick as "LpuUnitType_SysNick",
                    lu.LpuUnitType_id as "LpuUnitType_id",
                    lsp.LpuSectionProfile_SysNick as "LpuSectionProfile_SysNick",
                    lsp.LpuSectionProfile_Code as "LpuSectionProfile_Code",
                    lsp.LpuSectionProfile_id as "LpuSectionProfile_id",
                    ls.LpuSectionAge_id as "LpuSectionAge_id",
                    null::bigint as "MedService_id",
                    null as "MedService_Nick",
                    null as "MedService_Name",
                    null::bigint as "MedServiceType_id",
                    null::bigint as "MedService_IsExternal",
                    null::bigint as "AuthorizationIS_id",
                    null::bigint as "MedService_IsDisplayByFilter",
                    null as "MedServiceType_SysNick",
                    msf.Person_FIO as "MedPersonal_FIO",
                    Lpu.Org_id as "Org_id",
                    Lpu.Lpu_id as "Lpu_id",
                    Lpu.Lpu_Nick as "Org_Nick",
                    Lpu.Lpu_Nick as "Lpu_Nick",
                    msfl.MedStaffFactLink_id as "MedStaffFactLink_id",
                    to_char(msfl.MedStaffFactLink_begDT, 'DD.MM.YYYY') as "MedStaffFactLink_begDT",
                    to_char(msfl.MedStaffFactLink_endDT, 'DD.MM.YYYY') as "MedStaffFactLink_endDT",
                    msf.MedStaffFactCache_IsDisableInDoc as "MedStaffFactCache_IsDisableInDoc",
                    null::bigint as "ElectronicQueueInfo_id",
                    null::bigint as "ElectronicService_id",
                    null::bigint as "MedServiceElectronicQueue_id",
                    null::bigint as "MedServiceMedPersonal_id",
                    null as "ElectronicService_Num",
                    null as "ElectronicQueueInfo_CallTimeSec",
                    null as "ElectronicQueueInfo_PersCallDelTimeMin",
                    null as "ElectronicQueueInfo_InterruptTimeMin",
                    null as "ElectronicQueueInfo_CallCount",
                    null as "ElectronicService_isShownET",
                    null as "ElectronicQueueInfo_IsAutoReg",
                    null as "ElectronicQueueInfo_QueueTimeMin",
                    null as "ElectronicTreatment_ids",
                    null::bigint as "ElectronicScoreboard_id",
                    null as "ElectronicScoreboard_IPaddress",
                    null as "ElectronicScoreboard_Port",
                    null::bigint as "SmpUnitType_Code",
                    null::bigint as "SmpUnitParam_IsKTPrint",
                    null::bigint as "Storage_id",
                    null::bigint as "Storage_pid",
                    ps.FRMPPostMed_id as "FRMPPostMed_id"
                    
                    ,msf.MedicalCareKind_id as "MedicalCareKind_id"
                    ,msf.PostKind_id as "PostKind_id"
                    ,null::varchar as "MSF_LpuSection_Name"
                
                from
                    v_MedStaffFactLink msfl
                    inner join v_MedStaffFact msf on msf.MedStaffFact_id = msfl.MedStaffFact_id
                    inner join v_MedStaffFact mmsf on mmsf.MedStaffFact_id = msfl.MedStaffFact_sid
                    left join v_Lpu lpu on lpu.Lpu_id = msf.Lpu_id
                    left join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
                    left join v_LpuSectionProfile lsp on lsp.LpuSectionProfile_id = ls.LpuSectionProfile_id
                    left join v_LpuBuilding lb on lb.LpuBuilding_id = ls.LpuBuilding_id
                    left join v_LpuUnit lu on lu.LpuUnit_id = ls.LpuUnit_id
                    left join v_LpuUnitType lut on lut.LpuUnitType_id = lu.LpuUnitType_id
                    left join v_PostMed ps on ps.PostMed_id = msf.Post_id
                where
                    mmsf.MedPersonal_id = '180101000004491'
                    and msf.Lpu_id = '180101000000050'
                    and msf.MedStaffFact_Stavka > 0
                    and lut.LpuUnitType_SysNick in ('polka','ccenter','traumcenter','fap')
                    
                    and msf.WorkData_begDate::date <= (select curDate from curDT)
                    and (msf.WorkData_endDate::date >= (select curDate from curDT) or msf.WorkData_endDate is null)
                  and msf.MedStaffFact_id = '180101000124234' and ls.LpuSection_id = '180101000000630'
             union all 
                 select
                    msf.MedStaffFact_id as "MedStaffFact_id",
                    msf.LpuSection_id as "LpuSection_id",
                    msf.MedPersonal_id as "MedPersonal_id",
                    coalesce(ls.LpuSection_FullName, '') as "LpuSection_Name",
                    coalesce(ls.LpuSection_Name, '') as "LpuSection_Nick",
                    coalesce(ps.PostMed_Name, vp.name,'') as "PostMed_Name",
                    ps.PostMed_Code as "PostMed_Code",
                    ps.PostMed_id as "PostMed_id",
                    lb.LpuBuilding_id as "LpuBuilding_id",
                    coalesce(lb.LpuBuilding_Name, '') as "LpuBuilding_Name",
                    lu.LpuUnit_id as "LpuUnit_id",
                    lu.LpuUnitSet_id as "LpuUnitSet_id",
                    coalesce(lu.LpuUnit_Name, '') as "LpuUnit_Name",
                    case when lut.LpuUnitType_SysNick in ('polka','ccenter','traumcenter','fap')
                        then case when exists(select from v_TimeTableGraf_lite tt where msf.MedStaffFact_id = tt.MedStaffFact_id and tt.TimetableGraf_Time is not null)
                                then 'true'
                                else 'false'
                            end
                        when lut.LpuUnitType_SysNick = 'parka' then case when (select count(*) from v_TimetablePar tt where msf.LpuSection_id = tt.LpuSection_id) > 0
                                then 'true'
                                else 'false'
                            end
                        when lut.LpuUnitType_SysNick in ('stac', 'hstac', 'pstac', 'dstac') then
                            case when (select count(*) from v_TimetableStac_lite tt where msf.LpuSection_id = tt.LpuSection_id) > 0
                                then 'true'
                                else 'false'
                            end
                        else 'false'
                    end as "Timetable_isExists",
                    lut.LpuUnitType_SysNick as "LpuUnitType_SysNick",
                    lu.LpuUnitType_id as "LpuUnitType_id",
                    lsp.LpuSectionProfile_SysNick as "LpuSectionProfile_SysNick",
                    lsp.LpuSectionProfile_Code as "LpuSectionProfile_Code",
                    lsp.LpuSectionProfile_id as "LpuSectionProfile_id",
                    ls.LpuSectionAge_id as "LpuSectionAge_id",
                    null::bigint as "MedService_id",
                    null as "MedService_Nick",
                    null as "MedService_Name",
                    null::bigint as "MedServiceType_id",
                    null::bigint as "MedService_IsExternal",
                    null::bigint as "AuthorizationIS_id",
                    null::bigint as "MedService_IsDisplayByFilter",
                    null as "MedServiceType_SysNick",
                    msf.Person_FIO as "MedPersonal_FIO",
                    Lpu.Org_id as "Org_id",
                    Lpu.Lpu_id as "Lpu_id",
                    Lpu.Lpu_Nick as "Org_Nick",
                    Lpu.Lpu_Nick as "Lpu_Nick",
                    null::bigint as "MedStaffFactLink_id",
                    null as "MedStaffFactLink_begDT",
                    null as "MedStaffFactLink_endDT",
                    msf.MedStaffFactCache_IsDisableInDoc as "MedStaffFactCache_IsDisableInDoc",
                    eq.ElectronicQueueInfo_id as "ElectronicQueueInfo_id",
                    eq.ElectronicService_id as "ElectronicService_id",
                    eq.MedServiceElectronicQueue_id as "MedServiceElectronicQueue_id",
                    eq.MedServiceMedPersonal_id as "MedServiceMedPersonal_id",
                    eq.ElectronicService_Num as "ElectronicService_Num",
                    eq.ElectronicQueueInfo_CallTimeSec as "ElectronicQueueInfo_CallTimeSec",
                    eq.ElectronicQueueInfo_PersCallDelTimeMin as "ElectronicQueueInfo_PersCallDelTimeMin",
                    eq.ElectronicQueueInfo_InterruptTimeMin as "ElectronicQueueInfo_InterruptTimeMin",
                    eq.ElectronicQueueInfo_CallCount as "ElectronicQueueInfo_CallCount",
                    eq.ElectronicService_isShownET as "ElectronicService_isShownET",
                    eq.ElectronicQueueInfo_IsAutoReg as "ElectronicQueueInfo_IsAutoReg",
                    eq.ElectronicQueueInfo_QueueTimeMin as "ElectronicQueueInfo_QueueTimeMin",
                    (
                        select
                            string_agg(coalesce(CAST(etl.ElectronicTreatment_id as VARCHAR),''), ',')
                        from v_ElectronicTreatmentLink etl
                            inner join v_ElectronicQueueInfo eqio on eqio.ElectronicQueueInfo_id = etl.ElectronicQueueInfo_id
                            inner join v_ElectronicService eso on eso.ElectronicQueueInfo_id = eqio.ElectronicQueueInfo_id
                        where eso.ElectronicService_id = eq.ElectronicService_id
                    ) as "ElectronicTreatment_ids",
                    eboard.ElectronicScoreboard_id as "ElectronicScoreboard_id",
                    eboard.ElectronicScoreboard_IPaddress as "ElectronicScoreboard_IPaddress",
                    eboard.ElectronicScoreboard_Port as "ElectronicScoreboard_Port",
                    null::bigint as "SmpUnitType_Code",
                    null::bigint as "SmpUnitParam_IsKTPrint",
                    null::bigint as "Storage_id",
                    null::bigint as "Storage_pid",
                    ps.FRMPPostMed_id as "FRMPPostMed_id"
                    
                    ,msf.MedicalCareKind_id as "MedicalCareKind_id"
                    ,msf.PostKind_id as "PostKind_id"
                    ,null::varchar as "MSF_LpuSection_Name"
                
                from
                    v_MedStaffFact msf
                    left join v_Lpu lpu on lpu.Lpu_id = msf.Lpu_id
                    left join v_MedStaffFactReplace MSFR on MSF.MedStaffFact_id = MSFR.MedStaffFact_id
                    left join v_LpuSection ls on ls.LpuSection_id = msf.LpuSection_id
                    left join v_LpuSectionProfile lsp on lsp.LpuSectionProfile_id = ls.LpuSectionProfile_id
                    left join v_LpuBuilding lb on lb.LpuBuilding_id = ls.LpuBuilding_id
                    left join v_LpuUnit lu on lu.LpuUnit_id = ls.LpuUnit_id
                    left join v_LpuUnitType lut on lut.LpuUnitType_id = lu.LpuUnitType_id
                    left join persis.v_Post vp on vp.id = msf.Post_id
                    left join v_PostMed ps on ps.PostMed_id = msf.Post_id
                    left join lateral (
                        select
                            eqi.ElectronicQueueInfo_id,
                            mseq.ElectronicService_id,
                            mseq.MedServiceElectronicQueue_id,
                            mseq.MedServiceMedPersonal_id,
                            es.ElectronicService_Num,
                            eqi.ElectronicQueueInfo_CallTimeSec,
                            eqi.ElectronicQueueInfo_PersCallDelTimeMin,
                            coalesce(eqi.ElectronicQueueInfo_InterruptTimeMin, 0) as ElectronicQueueInfo_InterruptTimeMin,
                            eqi.ElectronicQueueInfo_CallCount,
                            eqi.ElectronicQueueInfo_QueueTimeMin,
                            case when eqi.ElectronicQueueInfo_IsAutoReg = 2 then 1 else null end as ElectronicQueueInfo_IsAutoReg,
                            case when es.ElectronicService_isShownET = 2 then 1 else null end as ElectronicService_isShownET
                        from
                            v_MedServiceElectronicQueue mseq
                            left join v_ElectronicService es on es.ElectronicService_id = mseq.ElectronicService_id
                            left join v_ElectronicQueueInfo eqi on eqi.ElectronicQueueInfo_id = es.ElectronicQueueInfo_id
                            left join v_ElectronicScoreboardQueueLink esql on esql.ElectronicQueueInfo_id = eqi.ElectronicQueueInfo_id
                        where mseq.MedStaffFact_id = msf.MedStaffFact_id
                		  and eqi.ElectronicQueueInfo_IsOff = 1
                        limit 1
                    ) as eq on true
                    left join lateral (
                        select
                            ebd.ElectronicScoreboard_id,
                            ebd.ElectronicScoreboard_IPaddress,
                            ebd.ElectronicScoreboard_Port
                        from
                        	v_ElectronicScoreboard ebd
                            left join v_ElectronicScoreboardQueueLink esql on esql.ElectronicService_id = eq.ElectronicService_id
                        where ebd.ElectronicScoreboard_id = esql.ElectronicScoreboard_id
                          and ebd.ElectronicScoreboard_IsLED = 2
                        limit 1
                    ) as eboard on true
                    where
                     msf.Lpu_id = '180101000000050'
                    and MSFR.MedStaffFactReplace_BegDate <= (select curDate from curDT)
                    and MSFR.MedStaffFactReplace_EndDate >= (select curDate from curDT)
            
