
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'ЕНП' as "Поле",
                'Не заполнено обязательное поле' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join dbo.v_PersonDispVizit pdv on pdv.PersonDisp_id=pd.PersonDisp_id
            where  pdv.PersonDispVizit_NextFactDate <= cast('2023-07-31' as timestamp)
            and	pdv.PersonDispVizit_NextFactDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and ps.Person_EdNum is null
        union
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'ЕНП' as "Поле",
                'Не заполнено обязательное поле' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join v_EvnVizitPL evpl on evpl.Person_id = pd.Person_id and evpl.Diag_id = pd.Diag_id and evpl.Lpu_id = pd.Lpu_id
            left join v_PersonDispVizit pdv on pdv.EvnVizitPL_id = evpl.EvnVizitPL_id
            where evpl.EvnVizitPL_setDate <= cast('2023-07-31' as timestamp)
            and	evpl.EvnVizitPL_setDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and pdv.EvnVizitPL_id is null
            and ps.Person_EdNum is null
        union
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'СНИЛС врача' as "Поле",
                'Не заполнено обязательное поле' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join dbo.v_PersonDispVizit pdv on pdv.PersonDisp_id=pd.PersonDisp_id
            left join dbo.v_EvnVizitPL evpl on evpl.EvnVizitPL_id=pdv.EvnVizitPL_id
            left join dbo.v_EvnVizit ev on ev.EvnVizit_id=evpl.EvnVizitPL_id
            left join lateral (
                SELECT MPC.Person_id
                FROM v_MedPersonalCache MPC
                WHERE MPC.MedPersonal_id = COALESCE(ev.MedPersonal_id,pd.MedPersonal_id)
                LIMIT 1
            ) medpers ON true
            left join v_PersonState medPS  on medPS.Person_id = medpers.Person_id
            where  pdv.PersonDispVizit_NextFactDate <= cast('2023-07-31' as timestamp)
            and	pdv.PersonDispVizit_NextFactDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and medPS.Person_Snils is null
            and medPS.Person_id is not null
        union
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'СНИЛС врача' as "Поле",
                'Не заполнено обязательное поле' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join v_EvnVizitPL evpl on evpl.Person_id = pd.Person_id and evpl.Diag_id = pd.Diag_id and evpl.Lpu_id = pd.Lpu_id
            left join v_PersonDispVizit pdv on pdv.EvnVizitPL_id = evpl.EvnVizitPL_id
            left join dbo.v_EvnVizit ev on ev.EvnVizit_id=evpl.EvnVizitPL_id
            left join lateral (
                SELECT MPC.Person_id
                FROM v_MedPersonalCache MPC
                WHERE MPC.MedPersonal_id = COALESCE(ev.MedPersonal_id,pd.MedPersonal_id)
                LIMIT 1
            ) medpers ON true
            left join v_PersonState medPS  on medPS.Person_id = medpers.Person_id
            where evpl.EvnVizitPL_setDate <= cast('2023-07-31' as timestamp)
            and	evpl.EvnVizitPL_setDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and pdv.EvnVizitPL_id is null
            and medPS.Person_Snils is null
            and medPS.Person_id is not null
        union
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'СНИЛС врача' as "Поле",
                'Отсутствует врач, ведущий ДН' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join dbo.v_PersonDispVizit pdv on pdv.PersonDisp_id=pd.PersonDisp_id
            left join dbo.v_EvnVizitPL evpl on evpl.EvnVizitPL_id=pdv.EvnVizitPL_id
            left join dbo.v_EvnVizit ev on ev.EvnVizit_id=evpl.EvnVizitPL_id
            left join lateral (
                SELECT PDH.MedPersonal_id
                FROM v_PersonDispHist PDH
                WHERE PDH.PersonDisp_id = pd.PersonDisp_id
                    and PDH.PersonDispHist_endDate is null
                LIMIT 1
            ) PDH on true
            left join lateral (
                SELECT MPC.Person_id
                FROM v_MedPersonalCache MPC
                WHERE MPC.MedPersonal_id = COALESCE(ev.MedPersonal_id, PDH.MedPersonal_id)
                LIMIT 1
            ) medpers ON true
            left join v_PersonState medPS  on medPS.Person_id = medpers.Person_id
            where pdv.PersonDispVizit_NextFactDate <= cast('2023-07-31' as timestamp)
            and	pdv.PersonDispVizit_NextFactDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and medPS.Person_id is null
        union
            select
                pd.Person_id as "Person_id",
                ps.Person_SurName ||' '|| ps.Person_FirName ||' '|| ps.Person_SecName as "ФИО",
                to_char(ps.Person_BirthDay, 'dd.mm.YYYY') as "Дата рождения",
                'СНИЛС врача' as "Поле",
                'Отсутствует врач, ведущий ДН' as "Ошибка"
            from v_PersonDisp pd
            inner join v_PersonState ps on ps.Person_id = pd.Person_id
            inner join v_EvnVizitPL evpl on evpl.Person_id = pd.Person_id and evpl.Diag_id = pd.Diag_id and evpl.Lpu_id = pd.Lpu_id
            left join v_PersonDispVizit pdv on pdv.EvnVizitPL_id = evpl.EvnVizitPL_id
            left join dbo.v_EvnVizit ev on ev.EvnVizit_id=evpl.EvnVizitPL_id
            left join lateral (
                SELECT PDH.MedPersonal_id
                FROM v_PersonDispHist PDH
                WHERE PDH.PersonDisp_id = pd.PersonDisp_id
                    and PDH.PersonDispHist_endDate is null
                LIMIT 1
            ) PDH on true
            left join lateral (
                SELECT MPC.Person_id
                FROM v_MedPersonalCache MPC
                WHERE MPC.MedPersonal_id = COALESCE(ev.MedPersonal_id, PDH.MedPersonal_id)
                LIMIT 1
            ) medpers ON true
            left join v_PersonState medPS  on medPS.Person_id = medpers.Person_id
            where evpl.EvnVizitPL_setDate <= cast('2023-07-31' as timestamp)
            and	evpl.EvnVizitPL_setDate >= cast('2023-01-01' as timestamp)
            and	pd.Lpu_id = '47'
            and pdv.EvnVizitPL_id is null
            and medPS.Person_id is null
        order by "Person_id"
        
