
            with mv as (
                
            select
                cast('9990010252' as bigint) as Lpu_id
        
            ),
            curDT as (select dbo.tzGetDate() as curDate),
            curD as (select cast(dbo.tzGetDate() AS date) as curDate)
            select * from (
                select distinct
                     cast(MSF.MedStaffFact_id as varchar) || '_' as "MedStaffFactKey_id"
                    ,'main' as "listType"
                    ,MSF.MedStaffFact_id as "MedStaffFact_id"
                    ,MSF.MedPersonal_id as "MedPersonal_id"
                    ,COALESCE(LTRIM(RTRIM(MSF.Person_Snils)), '') as "Person_Snils"
                    ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_TabCode)), '') as "MedPersonal_TabCode"
                    ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_Code)), '') as "MedPersonal_DloCode"
                    ,LTRIM(RTRIM(MSF.Person_FIO)) as "MedPersonal_Fio"
                    ,MSF.Person_SurName || ' ' || COALESCE(SUBSTRING(MSF.Person_FirName,1,1) || '.', '') || COALESCE(SUBSTRING(MSF.Person_SecName,1,1) || '.', '') as "MedPersonal_Fin"
                    ,'[' || LTRIM(RTRIM(LS.LpuSection_Code)) || '. ' || LTRIM(RTRIM(LS.LpuSection_Name)) || ']' as "LpuSection_Name"
                    ,COALESCE(LSP.LpuSectionProfile_Code, '') as "LpuSectionProfile_Code"
                    ,COALESCE(LSP.LpuSectionProfile_SysNick, '') as "LpuSectionProfile_SysNick"
                    ,COALESCE(LSP.LpuSectionProfile_Name, '') as "LpuSectionProfile_Name"
                    ,MSF.LpuSectionProfile_id as "LpuSectionProfile_msfid"
                    ,COALESCE(cast(LUT.LpuUnitType_Code as varchar), cast(LUTm.LpuUnitType_Code as varchar), '') as "LpuUnitType_Code"
                    ,COALESCE(LUT.LpuUnitType_SysNick,LUTm.LpuUnitType_SysNick, '') as "LpuUnitType_SysNick"
                    ,MSF.Lpu_id as "Lpu_id"
                    ,COALESCE(LU.LpuBuilding_id,MSF.LpuBuilding_id) as "LpuBuilding_id"
                    ,COALESCE(LB.LpuBuildingType_id,LBmsf.LpuBuildingType_id) as "LpuBuildingType_id"
                    ,COALESCE(LU.LpuUnit_id, LUm.LpuUnit_id) as "LpuUnit_id"
                    ,LU.LpuUnitSet_id as "LpuUnitSet_id"
                    ,LS.LpuSection_id as "LpuSection_id"
                    ,LS.LpuSection_pid as "LpuSection_pid"
                    ,LS.LpuSectionAge_id as "LpuSectionAge_id"
                    ,to_char(COALESCE(LS.LpuSection_disDate, L.Lpu_endDate), 'DD.MM.YYYY') as "LpuSection_disDate"
                    ,to_char(LS.LpuSection_setDate, 'DD.MM.YYYY') as "LpuSection_setDate"
                    ,to_char(MSF.WorkData_begDate, 'DD.MM.YYYY') as "WorkData_begDate"
                    ,to_char(MSF.WorkData_endDate, 'DD.MM.YYYY') as "WorkData_endDate"
                    ,PK.id as "PostKind_id"
                    ,p.id as "PostMed_id"
                    ,p.code as "PostMed_Code"
                    ,p.name as "PostMed_Name"
                    ,p.frmpEntry_id as "frmpEntry_id"
                    ,MSO.MedSpecOms_id as "MedSpecOms_id"
                    ,MSO.MedSpecOms_Code as "MedSpecOms_Code"
                    ,FMS.MedSpec_id as "FedMedSpec_id"
                    ,FMS.MedSpec_Code as "FedMedSpec_Code"
                    ,FMSP.MedSpec_Code as "FedMedSpecParent_Code"
                    ,COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) as "MedStaffFactCache_IsDisableInDoc"
                    ,cast(MSF.MedStaffFact_Stavka as varchar) as "MedStaffFact_Stavka"
                    ,case when p.PrimaryHealthCare = 1 then 2 else 1 end as "Post_IsPrimaryHealthCare"
                    ,MSRF.LpuRegion_ids as "LpuRegion_List"
                    ,MSRF2.LpuRegion_mids as "LpuRegion_MainList"
                    ,MSFC.MedStaffFactCache_IsHomeVisit as "MedStaffFactCache_IsHomeVisit"
                    ,MPost.MedPost_pid as "MedPost_pid"
                    ,nsiMPP.MedPersonalPost_Code as "MedPersonalPost_Code"
                    ,MPost.MedPost_id as "Post_id"
                    ,MPost.MedPost_Code as "MedPost_Code"
                    ,MPost.MedPost_Name as "MedPost_Name"
                	,L.Org_id as "Org_id"
                	,frmns.FRMRNomenSpeciality_Code as "MedPersonal_Speciality"
                ,frmns.FRMRNomenSpeciality_Name as "MedPersonal_Speciality_Name"
                     ,lp_dlo.LpuPeriodDLO_Code  as "LpuPeriodDLO_Code" ,to_char(lp_dlo.LpuPeriodDLO_begDate, 'DD.MM.YYYY') as "LpuPeriodDLO_begDate" ,to_char(lp_dlo.LpuPeriodDLO_endDate, 'DD.MM.YYYY') as "LpuPeriodDLO_endDate"
                ,to_char(MSF.WorkData_dlobegDate, 'DD.MM.YYYY') as "WorkData_dloBegDate"

                ,to_char(MSF.WorkData_dloendDate, 'DD.MM.YYYY') as "WorkData_dloEndDate"

            ,null as "LpuRegion_DatesList",MW.IsSentToFRMR as "IsSentToFRMR", L.Lpu_IsFRMO as "Lpu_IsFRMO"
                from v_MedStaffFact MSF
                    left join v_Lpu L  on L.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_LpuUnit LU  on LU.LpuUnit_id = LS.LpuUnit_id
                    LEFT join v_LpuUnit LUm  on LUm.LpuUnit_id = MSF.LpuUnit_id
                    LEFT join v_LpuUnitType LUT  on LUT.LpuUnitType_id = LU.LpuUnitType_id
                    LEFT join v_LpuUnitType LUTm  on LUTm.LpuUnitType_id = LUm.LpuUnitType_id
                    LEFT join v_LpuBuilding LB  on LB.LpuBuilding_id = LU.LpuBuilding_id
                    LEFT join v_LpuBuilding LBmsf  on LBmsf.LpuBuilding_id = MSF.LpuBuilding_id
                    left join v_MedSpecOms MSO  on MSO.MedSpecOms_id = MSF.MedSpecOms_id
                    left join fed.v_MedSpec FMS  on FMS.MedSpec_id = MSO.MedSpec_id
                    left join fed.v_MedSpec FMSP  on FMSP.MedSpec_id = FMS.MedSpec_pid
                    left join persis.PostKind PK  on PK.id = MSF.PostKind_id
                    left join persis.Post p  on p.id = MSF.Post_id
                    left join persis.FRMPPost fp on fp.id = p.frmpEntry_id
                    left join nsi.MedPersonalPost nsiMPP on nsiMPP.MedPersonalPost_id = fp.Frmr_id
                    left join persis.Speciality spec on spec.id = p.Speciality_id
                	left join persis.FRMRNomenSpeciality frmns on frmns.FRMRNomenSpeciality_id = spec.FRMRNomenSpeciality_id
                    LEFT JOIN LATERAL(
                        select
                            string_agg(cast(MSR.LpuRegion_id as varchar), ',') as LpuRegion_ids
                        from
                            v_MedStaffRegion MSR
                        where
                            MSR.MedStaffFact_id = MSF.MedStaffFact_id
                    ) MSRF on true
                    LEFT JOIN LATERAL(
                        select
                            string_agg(cast(MSR2.LpuRegion_id as varchar), ',') as LpuRegion_mids
                        from
                            v_MedStaffRegion MSR2
                        where
                            MSR2.MedStaffFact_id = MSF.MedStaffFact_id
                            and COALESCE(MSR2.MedStaffRegion_isMain,1) = 2
                    ) MSRF2 on true
                    left join v_MedStaffFactCache MSFC  on MSFC.MedStaffFact_id = MSF.MedStaffFact_id and MSFC.Server_id = MSF.Server_id
                    LEFT JOIN nsi.MedPost MPost ON MPost.MedPost_id = p.MedPost_id
                    
                LEFT JOIN LATERAL (
                    select
                        i_lpd.LpuPeriodDLO_Code,
                        i_lpd.LpuPeriodDLO_begDate,
                        i_lpd.LpuPeriodDLO_endDate
                    from
                        v_LpuPeriodDLO i_lpd
                    where
                        i_lpd.Lpu_id = ls.Lpu_id and
                        (
                            i_lpd.LpuUnit_id is null or
                            i_lpd.LpuUnit_id = ls.LpuUnit_id
                        ) and
                        i_lpd.LpuPeriodDLO_begDate <= CAST('2022-05-11 00:00:00' as date) and
                        (
                            i_lpd.LpuPeriodDLO_endDate is null or
                            i_lpd.LpuPeriodDLO_endDate >= CAST('2022-05-11 00:00:00' as date)
                        )
                    order by
                        LpuPeriodDLO_Code asc, i_lpd.LpuUnit_id desc
                    limit 1
                ) lp_dlo ON true
             left join persis.MedWorker MW on MW.Person_id = MSF.Person_id 
                where (1 = 1) and MSF.Lpu_id in (9990010252,13101210,13101098) or MSF.Lpu_id = (select Lpu_id from mv)
                    and MSF.WorkData_begDate is not null
                    and COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) = 1
                union all
                select distinct
                     cast(MSF.MedStaffFact_id as varchar) || '_' || cast(WGLS.WorkGraphLpuSection_id as varchar) as "MedStaffFactKey_id"
                    ,'main' as "listType"
                    ,MSF.MedStaffFact_id as "MedStaffFact_id"
                    ,MSF.MedPersonal_id as "MedPersonal_id"
                    ,COALESCE(LTRIM(RTRIM(MSF.Person_Snils)), '') as "Person_Snils"
                    ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_TabCode)), '') as "MedPersonal_TabCode"
                    ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_Code)), '') as "MedPersonal_DloCode"
                    ,LTRIM(RTRIM(MSF.Person_FIO)) as "MedPersonal_Fio"
                    ,MSF.Person_SurName || ' ' || COALESCE(SUBSTRING(MSF.Person_FirName,1,1) || '.', '') || COALESCE(SUBSTRING(MSF.Person_SecName,1,1) || '.', '') as "MedPersonal_Fin"
                    ,'[' || LTRIM(RTRIM(LS.LpuSection_Code)) || '. ' || LTRIM(RTRIM(LS.LpuSection_Name)) || ']' as "LpuSection_Name"
                    ,COALESCE(LSP.LpuSectionProfile_Code, '') as "LpuSectionProfile_Code"
                    ,COALESCE(LSP.LpuSectionProfile_SysNick, '') as "LpuSectionProfile_SysNick"
                    ,COALESCE(LSP.LpuSectionProfile_Name, '') as "LpuSectionProfile_Name"
                    ,MSF.LpuSectionProfile_id as "LpuSectionProfile_msfid"
                    ,COALESCE(cast(LUT.LpuUnitType_Code as varchar), '') as "LpuUnitType_Code"
                    ,COALESCE(LUT.LpuUnitType_SysNick, '') as "LpuUnitType_SysNick"
                    ,MSF.Lpu_id as "Lpu_id"
                    ,COALESCE(LU.LpuBuilding_id,MSF.LpuBuilding_id) as "LpuBuilding_id"
                    ,COALESCE(LB.LpuBuildingType_id,LBmsf.LpuBuildingType_id) as "LpuBuildingType_id"
                    ,LU.LpuUnit_id as "LpuUnit_id"
                    ,LU.LpuUnitSet_id as "LpuUnitSet_id"
                    ,LS.LpuSection_id as "LpuSection_id"
                    ,LS.LpuSection_pid as "LpuSection_pid"
                    ,LS.LpuSectionAge_id as "LpuSectionAge_id"
                    ,to_char(COALESCE(LS.LpuSection_disDate, L.Lpu_endDate), 'DD.MM.YYYY') as "LpuSection_disDate"
                    ,to_char(LS.LpuSection_setDate, 'DD.MM.YYYY') as "LpuSection_setDate"
                    ,to_char(WG.WorkGraph_begDT, 'DD.MM.YYYY') as "WorkData_begDate"
                    ,to_char(WG.WorkGraph_endDT, 'DD.MM.YYYY') as "WorkData_endDate"
                    ,PK.id as "PostKind_id"
                    ,p.id as "PostMed_id"
                    ,p.code as "PostMed_Code"
                    ,p.name as "PostMed_Name"
                    ,p.frmpEntry_id as "frmpEntry_id"
                    ,MSO.MedSpecOms_id as "MedSpecOms_id"
                    ,MSO.MedSpecOms_Code as "MedSpecOms_Code"
                    ,FMS.MedSpec_id as "FedMedSpec_id"
                    ,FMS.MedSpec_Code as "FedMedSpec_Code"
                    ,FMSP.MedSpec_Code as "FedMedSpecParent_Code"
                    ,COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) as "MedStaffFactCache_IsDisableInDoc"
                    ,cast(MSF.MedStaffFact_Stavka as varchar) as "MedStaffFact_Stavka"
                    ,case when p.PrimaryHealthCare = 1 then 2 else 1 end as "Post_IsPrimaryHealthCare"
                    ,MSRF.LpuRegion_ids as "LpuRegion_List"
                    ,MSRF2.LpuRegion_mids as "LpuRegion_MainList"
                    ,MSFC.MedStaffFactCache_IsHomeVisit as "MedStaffFactCache_IsHomeVisit"
                    ,MPost.MedPost_pid as "MedPost_pid"
                    ,nsiMPP.MedPersonalPost_Code as "MedPersonalPost_Code"
                    ,MPost.MedPost_id as "Post_id"
                    ,MPost.MedPost_Code as "MedPost_Code"
                    ,MPost.MedPost_Name as "MedPost_Name"
                	,L.Org_id as "Org_id"
                	,frmns.FRMRNomenSpeciality_Code as "MedPersonal_Speciality"
                ,frmns.FRMRNomenSpeciality_Name as "MedPersonal_Speciality_Name"
                     ,lp_dlo.LpuPeriodDLO_Code  as "LpuPeriodDLO_Code" ,to_char(lp_dlo.LpuPeriodDLO_begDate, 'DD.MM.YYYY') as "LpuPeriodDLO_begDate" ,to_char(lp_dlo.LpuPeriodDLO_endDate, 'DD.MM.YYYY') as "LpuPeriodDLO_endDate"
                ,to_char(MSF.WorkData_dlobegDate, 'DD.MM.YYYY') as "WorkData_dloBegDate"

                ,to_char(MSF.WorkData_dloendDate, 'DD.MM.YYYY') as "WorkData_dloEndDate"

            ,null as "LpuRegion_DatesList",MW.IsSentToFRMR as "IsSentToFRMR", L.Lpu_IsFRMO as "Lpu_IsFRMO"
                from v_MedStaffFact MSF
                    inner join v_WorkGraph WG on (
                        WG.MedStaffFact_id = msf.MedStaffFact_id and
                        (
                            CAST(WG.WorkGraph_begDT as date) <= (select curDate from curD)
                            and CAST(WG.WorkGraph_endDT as date) >= (select curDate from curD)
                        )
                    )
                    left join v_Lpu L  on L.Lpu_id = MSF.Lpu_id
                    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_LpuUnit LU  on LU.LpuUnit_id = LS.LpuUnit_id
                    left join v_LpuUnitType LUT  on LUT.LpuUnitType_id = LU.LpuUnitType_id
                    LEFT join v_LpuBuilding LB  on LB.LpuBuilding_id = LU.LpuBuilding_id
                    LEFT join v_LpuBuilding LBmsf  on LBmsf.LpuBuilding_id = MSF.LpuBuilding_id
                    left join v_MedSpecOms MSO  on MSO.MedSpecOms_id = MSF.MedSpecOms_id
                    left join fed.v_MedSpec FMS  on FMS.MedSpec_id = MSO.MedSpec_id
                    left join fed.v_MedSpec FMSP  on FMSP.MedSpec_id = FMS.MedSpec_pid
                    left join persis.PostKind PK  on PK.id = MSF.PostKind_id
                    left join persis.Post p  on p.id = MSF.Post_id
                    left join persis.FRMPPost fp on fp.id = p.frmpEntry_id
                    left join nsi.MedPersonalPost nsiMPP on nsiMPP.MedPersonalPost_id = fp.Frmr_id
                    left join persis.Speciality spec on spec.id = p.Speciality_id
                	left join persis.FRMRNomenSpeciality frmns on frmns.FRMRNomenSpeciality_id = spec.FRMRNomenSpeciality_id
                    LEFT JOIN LATERAL(
                        select
                            string_agg(cast(MSR.LpuRegion_id as varchar), ',') as LpuRegion_ids
                        from
                            v_MedStaffRegion MSR
                        where
                            MSR.MedStaffFact_id = MSF.MedStaffFact_id
                    ) MSRF on true
                    LEFT JOIN LATERAL(
                        select
                            string_agg(cast(MSR2.LpuRegion_id as varchar), ',') as LpuRegion_mids
                        from
                            v_MedStaffRegion MSR2
                        where
                            MSR2.MedStaffFact_id = MSF.MedStaffFact_id
                            and COALESCE(MSR2.MedStaffRegion_isMain,1) = 2
                    ) MSRF2 on true
                    left join v_MedStaffFactCache MSFC  on MSFC.MedStaffFact_id = MSF.MedStaffFact_id and MSFC.Server_id = MSF.Server_id
                    LEFT JOIN nsi.MedPost MPost ON MPost.MedPost_id = p.MedPost_id
                    
                LEFT JOIN LATERAL (
                    select
                        i_lpd.LpuPeriodDLO_Code,
                        i_lpd.LpuPeriodDLO_begDate,
                        i_lpd.LpuPeriodDLO_endDate
                    from
                        v_LpuPeriodDLO i_lpd
                    where
                        i_lpd.Lpu_id = ls.Lpu_id and
                        (
                            i_lpd.LpuUnit_id is null or
                            i_lpd.LpuUnit_id = ls.LpuUnit_id
                        ) and
                        i_lpd.LpuPeriodDLO_begDate <= CAST('2022-05-11 00:00:00' as date) and
                        (
                            i_lpd.LpuPeriodDLO_endDate is null or
                            i_lpd.LpuPeriodDLO_endDate >= CAST('2022-05-11 00:00:00' as date)
                        )
                    order by
                        LpuPeriodDLO_Code asc, i_lpd.LpuUnit_id desc
                    limit 1
                ) lp_dlo ON true
             left join persis.MedWorker MW on MW.Person_id = MSF.Person_id 
                where (1 = 1) and MSF.Lpu_id in (9990010252,13101210,13101098) or MSF.Lpu_id = (select Lpu_id from mv)
                    and MSF.WorkData_begDate is not null
                    and COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) = 1
        
                union all
                    
            -- сторонние специалисты (пока по ним нет даты окончания договора, поэтому если появится - то просто добавить в последний join)
            select distinct
                 cast(MSF.MedStaffFact_id as varchar) || '_' as "MedStaffFactKey_id"
                ,'dispcontract' as "listType"
                ,MSF.MedStaffFact_id as "MedStaffFact_id"
                ,MSF.MedPersonal_id as "MedPersonal_id"
                ,COALESCE(LTRIM(RTRIM(MSF.Person_Snils)), '') as "Person_Snils"
                ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_TabCode)), '') as "MedPersonal_TabCode"
                ,COALESCE(LTRIM(RTRIM(MSF.MedPersonal_Code)), '') as "MedPersonal_DloCode"
                ,LTRIM(RTRIM(MSF.Person_FIO)) as "MedPersonal_Fio"
                ,MSF.Person_SurName || ' ' || COALESCE(SUBSTRING(MSF.Person_FirName,1,1) || '.', '') || COALESCE(SUBSTRING(MSF.Person_SecName,1,1) || '.', '') as "MedPersonal_Fin"
                ,'[' || LTRIM(RTRIM(LS.LpuSection_Code)) || '. ' || LTRIM(RTRIM(LS.LpuSection_Name)) || ']' as "LpuSection_Name"
                ,COALESCE(LSP.LpuSectionProfile_Code, '') as "LpuSectionProfile_Code"
                ,COALESCE(LSP.LpuSectionProfile_SysNick, '') as "LpuSectionProfile_SysNick"
                ,COALESCE(LSP.LpuSectionProfile_Name, '') as "LpuSectionProfile_Name"
                ,MSF.LpuSectionProfile_id as "LpuSectionProfile_msfid"
                ,COALESCE(cast(LUT.LpuUnitType_Code as varchar), '') as "LpuUnitType_Code"
                ,COALESCE(LUT.LpuUnitType_SysNick, '') as "LpuUnitType_SysNick"
                ,MSF.Lpu_id as "Lpu_id"
                ,COALESCE(LU.LpuBuilding_id,MSF.LpuBuilding_id) as "LpuBuilding_id"
                ,COALESCE(LB.LpuBuildingType_id,LBmsf.LpuBuildingType_id) as "LpuBuildingType_id"
                ,LU.LpuUnit_id as "LpuUnit_id"
                ,LU.LpuUnitSet_id as "LpuUnitSet_id"
                ,LS.LpuSection_id as "LpuSection_id"
                ,LS.LpuSection_pid as "LpuSection_pid"
                ,LS.LpuSectionAge_id as "LpuSectionAge_id"
                ,to_char(COALESCE(LS.LpuSection_disDate, L.Lpu_endDate), 'DD.MM.YYYY') as "LpuSection_disDate"
                ,to_char(LS.LpuSection_setDate, 'DD.MM.YYYY') as "LpuSection_setDate"
                ,to_char(MSF.WorkData_begDate, 'DD.MM.YYYY') as "WorkData_begDate"
                ,to_char(MSF.WorkData_endDate, 'DD.MM.YYYY') as "WorkData_endDate"
                ,PK.id as "PostKind_id"
                ,p.id as "PostMed_id"
                ,p.code as "PostMed_Code"
                ,p.name as "PostMed_Name"
                ,p.frmpEntry_id as "frmpEntry_id"
                ,MSO.MedSpecOms_id as "MedSpecOms_id"
                ,MSO.MedSpecOms_Code as "MedSpecOms_Code"
                ,FMS.MedSpec_id as "FedMedSpec_id"
                ,FMS.MedSpec_Code as "FedMedSpec_Code"
                ,FMSP.MedSpec_Code as "FedMedSpecParent_Code"
                ,COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) as "MedStaffFactCache_IsDisableInDoc"
                ,cast(MSF.MedStaffFact_Stavka as varchar) as "MedStaffFact_Stavka"
                ,case when p.PrimaryHealthCare = 1 then 2 else 1 end as "Post_IsPrimaryHealthCare"
                ,MSRF.LpuRegion_ids as "LpuRegion_List"
                ,MSRF2.LpuRegion_mids as "LpuRegion_MainList"
                ,MSFC.MedStaffFactCache_IsHomeVisit as "MedStaffFactCache_IsHomeVisit"
                ,MPost.MedPost_pid as "MedPost_pid"
                ,nsiMPP.MedPersonalPost_Code as "MedPersonalPost_Code"
                ,MPost.MedPost_id as "Post_id"
            ,MPost.MedPost_Code as "MedPost_Code"
            ,MPost.MedPost_Name as "MedPost_Name"
                ,L.Org_id as "Org_id"
                ,frmns.FRMRNomenSpeciality_Code as "MedPersonal_Speciality"
                ,frmns.FRMRNomenSpeciality_Name as "MedPersonal_Speciality_Name"
                 ,lp_dlo.LpuPeriodDLO_Code  as "LpuPeriodDLO_Code" ,to_char(lp_dlo.LpuPeriodDLO_begDate, 'DD.MM.YYYY') as "LpuPeriodDLO_begDate" ,to_char(lp_dlo.LpuPeriodDLO_endDate, 'DD.MM.YYYY') as "LpuPeriodDLO_endDate"
                ,to_char(MSF.WorkData_dlobegDate, 'DD.MM.YYYY') as "WorkData_dloBegDate"

                ,to_char(MSF.WorkData_dloendDate, 'DD.MM.YYYY') as "WorkData_dloEndDate"

            ,null as "LpuRegion_DatesList",MW.IsSentToFRMR as "IsSentToFRMR", L.Lpu_IsFRMO as "Lpu_IsFRMO"
            from v_MedStaffFact MSF
                inner join LpuDispContract LDC  on LDC.Lpu_id = (select Lpu_id from mv)
                inner join v_LpuSection LS  on LS.LpuSection_id = MSF.LpuSection_id
                    and LS.LpuSection_setDate is not null
                inner join v_LpuSectionProfile LSP  on LSP.LpuSectionProfile_id = LS.LpuSectionProfile_id
                inner join v_LpuUnit LU  on LU.LpuUnit_id = LS.LpuUnit_id
                inner join v_LpuBuilding LB  on LU.LpuBuilding_id = LB.LpuBuilding_id
                inner join v_LpuUnitType LUT  on LUT.LpuUnitType_id = LU.LpuUnitType_id
                left join v_Lpu L  on L.Lpu_id = MSF.Lpu_id
                LEFT join v_LpuBuilding LBmsf  on LBmsf.LpuBuilding_id = MSF.LpuBuilding_id
                left join v_MedSpecOms MSO  on MSO.MedSpecOms_id = MSF.MedSpecOms_id
                left join fed.v_MedSpec FMS  on FMS.MedSpec_id = MSO.MedSpec_id
                left join fed.v_MedSpec FMSP  on FMSP.MedSpec_id = FMS.MedSpec_pid
                left join persis.PostKind PK  on PK.id = MSF.PostKind_id
                left join persis.Post p  on p.id = MSF.Post_id
                left join persis.FRMPPost fp on fp.id = p.frmpEntry_id
                left join nsi.MedPersonalPost nsiMPP on nsiMPP.MedPersonalPost_id = fp.Frmr_id
                left join persis.Speciality spec on spec.id = p.Speciality_id
                left join persis.FRMRNomenSpeciality frmns on frmns.FRMRNomenSpeciality_id = spec.FRMRNomenSpeciality_id
                LEFT JOIN LATERAL(
                    select
                        string_agg(cast(MSR.LpuRegion_id as varchar), ',') as LpuRegion_ids
                    from
                        v_MedStaffRegion MSR
                    where
                        MSR.MedStaffFact_id = MSF.MedStaffFact_id
                ) MSRF on true
                LEFT JOIN LATERAL(
                    select
                        string_agg(cast(MSR2.LpuRegion_id as varchar), ',') as LpuRegion_mids
                    from
                        v_MedStaffRegion MSR2
                    where
                        MSR2.MedStaffFact_id = MSF.MedStaffFact_id
                        and COALESCE(MSR2.MedStaffRegion_isMain,1) = 2
                ) MSRF2 on true
                left join v_MedStaffFactCache MSFC  on MSFC.MedStaffFact_id = MSF.MedStaffFact_id and MSFC.Server_id = MSF.Server_id
                LEFT JOIN nsi.MedPost MPost ON MPost.MedPost_id = p.MedPost_id
                
                LEFT JOIN LATERAL (
                    select
                        i_lpd.LpuPeriodDLO_Code,
                        i_lpd.LpuPeriodDLO_begDate,
                        i_lpd.LpuPeriodDLO_endDate
                    from
                        v_LpuPeriodDLO i_lpd
                    where
                        i_lpd.Lpu_id = ls.Lpu_id and
                        (
                            i_lpd.LpuUnit_id is null or
                            i_lpd.LpuUnit_id = ls.LpuUnit_id
                        ) and
                        i_lpd.LpuPeriodDLO_begDate <= CAST('2022-05-11 00:00:00' as date) and
                        (
                            i_lpd.LpuPeriodDLO_endDate is null or
                            i_lpd.LpuPeriodDLO_endDate >= CAST('2022-05-11 00:00:00' as date)
                        )
                    order by
                        LpuPeriodDLO_Code asc, i_lpd.LpuUnit_id desc
                    limit 1
                ) lp_dlo ON true
             left join persis.MedWorker MW on MW.Person_id = MSF.Person_id 
            where (1 = 1)
                
                and MSF.Lpu_id = LDC.Lpu_oid
                and MSF.Lpu_id != (select Lpu_id from mv)
                and (MSF.LpuSection_id = LDC.LpuSection_id or LDC.LpuSection_id is null)
                and LDC.LpuSectionProfile_id in (
                    select LpuSectionProfile_id from v_LpuSectionLpuSectionProfile  where LpuSection_id = LS.LpuSection_id
                    union all
                    select LS.LpuSectionProfile_id
                )
                and MSF.WorkData_begDate is not null
                and COALESCE(MSF.MedStaffFactCache_IsDisableInDoc, 1) = 1
             
        
            
                ) as MedPersonal
                order by
                     MedPersonal."MedPersonal_Fio"
                    ,MedPersonal."LpuSection_Name"
            
