
                
                
            with curDT as (select localtimestamp 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",
                PS.PersonEvn_id as "PersonEvn_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, localtimestamp) 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",
                D.Diag_Code as "Diag_Code",
                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",
                PP.PersonPregnancy_RiskFactors as "RiskFactor572N",
                to_char(PR.personregister_disdate, 'dd.mm.yyyy') as "DateExclFrReg",
                OutCause.PersonRegisterOutCause_Name as "CauseExclFrReg",
                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,
                        case QT.QuestionType_Code
                            when 362 then 1
                            when 358 then 2
                            when 359 then 3
                            when 363 then 4
                        end as sort
                    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 sort
                    limit 1
                ) LastScreenPeriod on true
                left join lateral(
                    select coalesce(
                        BSS.BirthSpecStac_OutcomPeriod,
                        LastScreenPeriod.Value + CEILING(datediff('day', LastScreen.PregnancyScreen_setDT, localtimestamp) / 7.0),
                        PP.PersonPregnancy_Period + CEILING(datediff('day', PP.PersonPregnancy_setDT, localtimestamp) / 7.0)
                    ) 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 ps.Person_SurName ilike 'МИНЕНКОВА' || '%' and (
            (PR.Lpu_iid in (550101000000044,550101000000202,550101000000265,13102197,550101000000298,550101000000190,550101000000009,13102167,550101000000256,13102085,13102084,550101000000466,550101000000135,550101000000136,550101000000299,13102164,13102095,13102102,13102103,13102088,13102081,13102074,13102075,13102077,13102131,13102079,13102137,13102060,13102161,13102159,13102073,13102080,13102057,13102062,13102120,13102145,13102149,13102071,13102150,13102151,13102128,13102066,13102067,13102068,13102135,13102070,13102124,13102163,13102056,13102156,13102157,13102078,13102176,13102147,13102162,13102160,13102178,13102125,13102058,13102054,13102059,13102146,13102093,13102168,13102116,13102122,13102136,13102097,13102104,13102138,13102174,13102076,13102082,13102166,13102087,13102169,13102089,13102091,13102090,13102065,13102154,13102105,13102140,13102106,13102121,13102123,13102132,13102118,13102155,13102177,13102173,13102127,13102141,13102130,13102152,13102133,13102201,13102107,13102108,13102142,13102109,13102110,13102063,13102064,13102119,13102099,13102094,13102139,13102158,13102112,13102126,13102175,13102170,13102114,13102165,13102134,13102143,13102153,13102129,13102098,13102115,13102055,13102072,13102144,13102061,13102086,13102096,13102111,13102083,550101000000499,13102101,13102113,550101000000137,550101000000220,550101000000437,550101000000489,550101000000071,550101000000127,550101000000128,550101000000126,550101000000108,550101000000491,550101000000430,550101000000389,550101000000390,550101000000041,550101000000248,13102196,550101000000064,550101000000293,550101000000291,550101000000411,550101000000417,550101000000004,13102100,13102092,550101000000181,550101000000309,13102209,550101000000388,550101000000490,13102203,550101000000008,550101000000233,550101000000325,550101000000034,550101000000035,550101000000036,550101000000037,550101000000184,550101000000278,550101000000141,550101000000289,550101000000082,550101000000083,550101000000084,550101000000085,550101000000086,550101000000249,550101000000268,550101000000286,550101000000003,550101000000311,13102148,550101000000227,550101000000448,550101000000449,550101000000450,550101000000451,550101000000180,550101000000196,550101000000214,550101000000203,550101000000284,550101000000030,550101000000468,550101000000398,550101000000063,550101000000236,550101000000138,550101000000099,550101000000324,550101000000093,550101000000013,550101000000305,550101000000306,550101000000119,550101000000354,550101000000374,550101000000405,550101000000031,550101000000173,550101000000244,550101000000320,13102207,550101000000223,550101000000285,550101000000195,550101000000080,550101000000169,550101000000170,13102204,13102205,13102206,13102208,550101000000313,550101000000212,13102171,550101000000224,550101000000501,550101000000498,550101000000500,550101000000504,550101000000140,550101000000310,550101000000219,13102117,550101000000287,550101000000252,13102198,550101000000239,550101000000148,13102202,13102172,13102200,13102187,13102193,13102194,13102195,13102188,13102189,13102190,13102191,13102192,550101000000186,550101000000070,550101000000344,550101000000022,550101000000023,550101000000024,550101000000245,550101000000273,550101000000243,550101000000434,550101000000206,550101000000199,550101000000059,550101000000188,550101000000198,550101000000275,550101000000261,550101000000392,550101000000391,550101000000072,13102183,13102182,13102185,13102184,13102186,13102181,13102180)
                or PR.Lpu_iid is null)
            ) and PR.PersonRegisterOutCause_id is null
                
                
                        ORDER BY
                            
                
                PR.PersonRegister_setDate desc,
                PR.PersonRegister_disDate desc
        
                
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
