
                
                
            with curDT as (select dbo.tzGetDate() as curDate)
            
                SELECT 
                PR.PersonRegister_id as "PersonRegister_id",
                PR.PersonRegister_Code as "PersonRegister_Code",
                to_char(PR.PersonRegister_setDate, 'dd.mm.yyyy') as "PersonRegister_setDate",
                OutCause.PersonRegisterOutCause_id as "PersonRegisterOutCause_id",
                OutCause.PersonRegisterOutCause_Code as "PersonRegisterOutCause_Code",
                OutCause.PersonRegisterOutCause_SysNick as "PersonRegisterOutCause_SysNick",
                OutCause.PersonRegisterOutCause_Name as "PersonRegisterOutCause_Name",
                PP.PersonPregnancy_id as "PersonPregnancy_id",
                PS.Server_id as "Server_id",
                PS.Person_id as "Person_id",
                coalesce(rtrim(PS.Person_SurName),'')
                    || coalesce(' '
                    || rtrim(PS.Person_FirName),'')
                    || coalesce(' '
                    || rtrim(PS.Person_SecName),''
                ) as "Person_Fio",
                to_char(PS.Person_BirthDay, 'dd.mm.yyyy') as "Person_BirthDay",
                dbo.Age2(PS.Person_BirthDay, dbo.tzGetDate()) as "Person_Age",
                coalesce(PAddress.Address_Nick, PAddress.Address_Address) as "Person_PAddress",
                coalesce(PP.PersonPregnancy_RiskDPP, 0) + coalesce(LastScreen.PregnancyScreen_RiskPerPat, 0) as "PersonPregnancy_ObRisk",
                RT.RiskType_id as "RiskType_id",
                RT.RiskType_Name as "RiskType_Name",
                PR.Lpu_iid as "Lpu_iid",
                L.Lpu_Nick as "Lpu_Nick",
                LpuAttach.Lpu_id as "LpuAttach_id",
                LpuAttach.Lpu_Nick as "LpuAttach_Nick",
                LpuHosp.Lpu_id as "LpuHosp_id",
                LpuHosp.Lpu_Nick as "LpuHosp_Nick",
                PR.MedPersonal_iid as "MedPersonal_iid",
                DM.DeathMotherType_id as "DeathMotherStatus",
                MP.MedPersonal_Fio as "MedPersonal_Fio",
                D.Diag_id as "Diag_id",
                D.Diag_FullName as "Diag_FullName",
                Result.PregnancyResult_id as "PregnancyResult_id",
                Result.PregnancyResult_Name as "PregnancyResult_Name",
                BSS.AbortMethod_id as "AbortMethod_id",
                Period.Value as "PersonPregnancy_Period",
                (case when PP.RiskType_did = 2 then dbo.GetPregnancyPRRisk(PR.PersonRegister_id,1) else '' end) as "PRRiskFactor",
                (case when PP.RiskType_did = 1 then 'низкий' when PP.RiskType_did = 2 then 'высокий' else '' end) as "RiskPR",
                (case when PP.RiskType_bid = 1 then 'низкий' when PP.RiskType_bid = 2 then 'средний' when PP.RiskType_bid = 3 then 'высокий' else 'низкий' end) as "Risk572N",
                RTglobal.RiskType_Name as "RiskGlobal",
                dbo.GetPregnancy1130NRisk(PR.PersonRegister_id,1,0) as "RiskFactor572N",
                case when exists(
                    select *
                    from v_PersonQuarantine PQ
                    where PQ.Person_id = PS.Person_id
                    and PQ.PersonQuarantine_endDT is null
                ) then 2 else 1 end as "PersonQuarantine_IsOn"
                ,
                Evn_PS.EvnPS_IsActive as "EvnPS_IsActive"
                ,Part.Partogram_id as "Partogram_id"
                ,ES.EvnSection_id as "EvnSection_id"
                
                FROM
                
                v_PersonRegister PR
                inner join v_PersonRegisterType PRT on PRT.PersonRegisterType_id = PR.PersonRegisterType_id
                    and PRT.PersonRegisterType_SysNick ilike 'pregnancy'
                left join v_PersonPregnancy PP on PP.PersonRegister_id = PR.PersonRegister_id
                left join v_PersonRegisterOutCause OutCause on OutCause.PersonRegisterOutCause_id = PR.PersonRegisterOutCause_id
                left join v_PersonState PS on PS.Person_id = PR.Person_id
                left join v_Address PAddress on PAddress.Address_id = PS.PAddress_id
                left join v_RiskType RT on RT.RiskType_id = PR.RiskType_id
                left join v_PersonCard PC on PC.Person_id = PS.Person_id and PC.LpuAttachType_id = 1
                left join v_Lpu_all LpuAttach on LpuAttach.Lpu_id = PC.Lpu_id
                left join v_Lpu_all L on L.Lpu_id = PR.Lpu_iid
                left join lateral (
                    select
                        MedPersonal_id,
                        Person_Fio as MedPersonal_Fio
                    from v_MedPersonal
                    where MedPersonal_id = PR.MedPersonal_iid
                        and Lpu_id = coalesce(L.Lpu_id, Lpu_id)
                    limit 1
                ) MP on true
                left join v_BirthCertificate BC on BC.PersonRegister_id = PR.PersonRegister_id
                left join v_EvnVizitPL EVPL on EVPL.EvnVizitPL_id = PP.Evn_id
                left join v_BirthSpecStac BSS on BSS.PersonRegister_id = PR.PersonRegister_id
                left join v_EvnSection ES on ES.EvnSection_id = BSS.EvnSection_id
                left join v_EvnVizitPL rEVPL on rEVPL.EvnVizitPL_id = BSS.Evn_id
                left join v_PregnancyResult Result on Result.PregnancyResult_id = coalesce(PR.PregnancyResult_id,BSS.PregnancyResult_id)
                left join lateral (
                    select Screen.*
                    from v_PregnancyScreen Screen
                    where Screen.PersonRegister_id = PR.PersonRegister_id
                    order by Screen.PregnancyScreen_setDT desc
                    limit 1
                ) LastScreen on true
                left join lateral (
                    select PQ.PregnancyQuestion_ValuesStr as Lpu_id
                    from v_PregnancyQuestion PQ
                        inner join v_QuestionType QT on QT.QuestionType_id = PQ.QuestionType_id
                    where PQ.PregnancyScreen_id = LastScreen.PregnancyScreen_id
                        and QT.QuestionType_Code = 406
                    limit 1
                ) LastScreenDir on true
                left join lateral (
                    select PQ.PregnancyQuestion_AnswerInt as Value
                    from v_PregnancyQuestion PQ
                        inner join v_QuestionType QT on QT.QuestionType_id = PQ.QuestionType_id
                    where PQ.PregnancyScreen_id = LastScreen.PregnancyScreen_id
                        and QT.QuestionType_Code in (358,359,362,363)
                    order by QT.QuestionType_Code
                    limit 1
                ) LastScreenPeriod on true
                left join lateral(
                    select coalesce(
                        BSS.BirthSpecStac_OutcomPeriod,
                        LastScreenPeriod.Value + datediff('week', LastScreen.PregnancyScreen_setDT, dbo.tzGetDate()),
                        PP.PersonPregnancy_Period + datediff('week', PP.PersonPregnancy_setDT, dbo.tzGetDate())
                    ) as Value
                    limit 1
                ) Period on true
                left join lateral (
                    select
                    EvnPS_id,
                    EvnPS_IsActive
                    from
                    v_EvnPS
                    where
                    Person_id = PR.Person_id
                    order by EvnPS_setDT desc LIMIT 1) Evn_PS on true
                left join lateral(
                    select
                        PersonRegister_id,
                        DeathMotherType_id
                    from
                        v_DeathMother
                    where PersonRegister_id = PR.PersonRegister_id
                    order by
                        DeathMother_id
                    desc
                    limit 1
                ) DM on true
                left join v_Lpu_all LpuHosp on LpuHosp.Lpu_id = coalesce(ES.Lpu_id, LastScreenDir.Lpu_id, BC.Lpu_id)
                left join v_Diag D on D.Diag_id = coalesce(ES.Diag_id,rEVPL.Diag_id,LastScreen.Diag_id,PP.Diag_id,EVPL.Diag_id)
                left join v_Partogram Part on Part.EvnSection_id = ES.EvnSection_id
                left join v_RiskType RTglobal on RTglobal.RiskType_id = case
                    when PP.PersonPregnancy_IsInQuarantine = 2 then 3
                    else dbo.GetPregnancyRiskGlobal(RT.RiskType_id, PP.RiskType_did, PP.RiskType_bid)::int
                    end
                
                 WHERE 
                (1=1)  and BSS.BirthSpecStac_id is null and PR.PregnancyResult_id is null and PR.PersonRegister_setDate <= (select curDate from curDT) and (PR.PersonRegister_disDate is null or PR.PersonRegister_disDate >= (select curDate from curDT))
                
                
                        ORDER BY
                            
                
                PR.PersonRegister_setDate desc,
                PR.PersonRegister_disDate desc
        
                
                OFFSET 3300 ROWS
                FETCH NEXT 100 ROWS ONLY
                
