
                
                
                SELECT 
                
            cast(COALESCE(EPLDD13.EvnPLDispDop13_id, 0) as varchar) || '_' || cast(COALESCE(PS.Person_id, 0) as varchar) as "id",
            EPLDD13.EvnPLDispDop13_id as "EvnPLDispDop13_id",
            PS.Person_id as "Person_id",
            PS.Server_id as "Server_id",
            EPLDD13.PersonEvn_id as "PersonEvn_id",
            EPLDD13.PayType_id as "PayType_id",
            case when EPLDD13.EvnPLDispDop13_IsEndStage = 2  then 'Да' else 'Нет' end as "EvnPLDispDop13_IsEndStage",
            case when EPLDD13.EvnPLDispDop13_isMobile = 2  then 'Да' else 'Нет' end as "EvnPLDispDop13_IsMobile",
            case when DopDispSecond.EvnPLDispDop13_IsEndStage = 2  then 'Да' else 'Нет' end as "EvnPLDispDop13Second_IsEndStage",
            to_char(EPLDD13.EvnPLDispDop13_consDT, 'DD.MM.YYYY') as "EvnPLDispDop13_setDate",
            to_char(EPLDD13.EvnPLDispDop13_disDate, 'DD.MM.YYYY') as "EvnPLDispDop13_disDate",
            HK.HealthKind_Name as "EvnPLDispDop13_HealthKind_Name",
            case when EPLDD13.EvnPLDispDop13_IsEndStage = 2 and EPLDD13.EvnPLDispDop13_IsTwoStage = 2 then to_char(EPLDD13.EvnPLDispDop13_disDate, 'DD.MM.YYYY') else null end as "EvnPLDispDop13Second_napDate",
            HK_SEC.HealthKind_Name as "EvnPLDispDop13Second_HealthKind_Name",
            case when DDICData.DopDispInfoConsent_IsAgree = 1 then to_char(EPLDD13.EvnPLDispDop13_consDT, 'DD.MM.YYYY') else null end as "EvnPLDispDop13_rejDate",
            case
                when DopDispSecond.EvnPLDispDop13_id is null then 0
                when DopDispSecond.Lpu_id = '60' then 0
                when DopDispSecond.Lpu_id = '60' and EPLDD13AL.EvnPLDispDop13_id is not null then 0
                else 4
            end as "AccessType_Code",
            DopDispSecond.EvnPLDispDop13_id as "EvnPLDispDop13Second_id",
            to_char(DOCOSMDT.EvnUslugaDispDop_disDate, 'DD.MM.YYYY') as "VopOsm_EvnUslugaDispDop_disDate", -- дата осмотра врача-терапевта на первом этапе
            COALESCE(DopDispSecond.EvnPLDispDop13_IsTransit, 1) as "EvnPLDispDop13Second_IsTransit",
            to_char(DopDispSecond.EvnPLDispDop13_consDT, 'DD.MM.YYYY') as "EvnPLDispDop13Second_setDate",
            to_char(DopDispSecond.EvnPLDispDop13_disDate, 'DD.MM.YYYY') as "EvnPLDispDop13Second_disDate",
            case when DDICDataSecond.DopDispInfoConsent_IsAgree = 1 then to_char(DopDispSecond.EvnPLDispDop13_consDT, 'DD.MM.YYYY') else null end as "EvnPLDispDop13Second_rejDate",
            to_char(ecp.EvnCostPrint_setDT, 'DD.MM.YYYY') as "EvnCostPrint_setDT",
            EPLDD13.EvnPLDispDop13_IsSigned as "EvnPLDispDop13_IsSigned",
            case when ecp.EvnCostPrint_IsNoPrint = 2 then 'Отказ от справки' when ecp.EvnCostPrint_IsNoPrint = 1 then 'Справка выдана' else '' end as "EvnCostPrint_IsNoPrintText"
        
            ,COALESCE(UAdd.Address_Nick, UAdd.Address_Address) as "ua_name"
            ,COALESCE(PAdd.Address_Nick, PAdd.Address_Address) as "pa_name"
        
                ,RTRIM(PS.Person_SurName) as "Person_Surname"
                ,RTRIM(PS.Person_FirName) as "Person_Firname"
                ,RTRIM(PS.Person_SecName) as "Person_Secname"
                ,to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday"
            
                
                FROM
                
                v_PersonState PS
            left join v_PersonCard PC on PC.Person_id = PS.Person_id
                inner join v_EvnPLDispDop13 EPLDD13 on PS.Person_id = EPLDD13.Person_id and COALESCE(EPLDD13.DispClass_id,1) = 1 and date_part('year', EPLDD13.EvnPLDispDop13_consDT) = 2023  and COALESCE(EPLDD13.EvnPLDispDop13_IsTwoStage, 1) = '2'  
                left join v_EvnPLDispDop13 DopDispSecond on (DopDispSecond.EvnPLDispDop13_fid = EPLDD13.EvnPLDispDop13_id) 
            
            left join v_EvnCostPrint ecp on ecp.Evn_id = DopDispSecond.EvnPLDispDop13_id
            left join v_HealthKind HK on HK.HealthKind_id = EPLDD13.HealthKind_id
            left join v_Address UAdd on UAdd.Address_id = PS.UAddress_id
            left join v_Address PAdd on PAdd.Address_id = PS.PAddress_id
            LEFT JOIN LATERAL (
                select
                    DDIC.DopDispInfoConsent_IsAgree
                from
                    v_DopDispInfoConsent DDIC
                    left join v_SurveyTypeLink STL on STL.SurveyTypeLink_id = DDIC.SurveyTypeLink_id
                    left join v_SurveyType ST on ST.SurveyType_id = STL.SurveyType_id
                where
                    DDIC.EvnPLDisp_id = EPLDD13.EvnPLDispDop13_id
                    and ST.SurveyType_Code = 1
                    
                limit 1
            ) DDICData on true
            LEFT JOIN LATERAL ( -- дата осмотра врачём-терапевтом из первого этапа
                select
                    COALESCE(EUDD.EvnUslugaDispDop_disDate, EUDD.EvnUslugaDispDop_didDate) as EvnUslugaDispDop_disDate
                from v_EvnUslugaDispDop EUDD
                    left join v_EvnVizitDispDop EVDD on EVDD.EvnVizitDispDop_id = EUDD.EvnUslugaDispDop_pid
                    left join v_UslugaComplex UC on UC.UslugaComplex_id = EUDD.UslugaComplex_id
                    left join v_DopDispInfoConsent DDIC on EVDD.DopDispInfoConsent_id = DDIC.DopDispInfoConsent_id
                    left join v_SurveyTypeLink STL on STL.SurveyTypeLink_id = DDIC.SurveyTypeLink_id
                    left join v_SurveyType ST on ST.SurveyType_id = STL.SurveyType_id
                where
                    EVDD.EvnVizitDispDop_pid = EPLDD13.EvnPLDispDop13_id
                    and ST.SurveyType_Code = 19
                    and COALESCE(EUDD.EvnUslugaDispDop_IsVizitCode, 1) = 1
                limit 1
            ) DOCOSMDT on true
            left join v_HealthKind HK_SEC on HK_SEC.HealthKind_id = DopDispSecond.HealthKind_id
            LEFT JOIN LATERAL (
                select
                    DDIC.DopDispInfoConsent_IsAgree
                from
                    v_DopDispInfoConsent DDIC
                    left join v_SurveyTypeLink STL on STL.SurveyTypeLink_id = DDIC.SurveyTypeLink_id
                    left join v_SurveyType ST on ST.SurveyType_id = STL.SurveyType_id
                where
                    DDIC.EvnPLDisp_id = DopDispSecond.EvnPLDispDop13_id
                    and ST.SurveyType_Code = 48
                limit 1
            ) DDICDataSecond on true
            LEFT JOIN LATERAL (
                select EvnPLDispDop13_id, Lpu_id
                from v_EvnPLDispDop13
                where Person_id = PS.Person_id
                    and date_part('year', EvnPLDispDop13_setDate) = 2023
                    and Lpu_id != '60'
                    and COALESCE(DispClass_id,1) = 1
                limit 1
            ) EPLDD13AL on true
            left join v_Lpu lpu on lpu.Lpu_id = COALESCE(EPLDD13.Lpu_id, EPLDD13AL.Lpu_id)
        
                 WHERE 
                (1=1) and PC.Lpu_id = '60' and PC.LpuAttachType_id = '1' and case when DopDispSecond.EvnPLDispDop13_id is not null then DopDispSecond.pmUser_updID else EPLDD13.pmUser_updID end = '376062320067' and cast(case when DopDispSecond.EvnPLDispDop13_id is not null then DopDispSecond.EvnPLDispDop13_insDT else EPLDD13.EvnPLDispDop13_insDT end as date) >= cast('2023-06-01' as date) and cast(case when DopDispSecond.EvnPLDispDop13_id is not null then DopDispSecond.EvnPLDispDop13_insDT else EPLDD13.EvnPLDispDop13_insDT end as date) <= cast('2023-06-30' as date)
                
                
                        ORDER BY
                            
                
            PS.Person_SurName,
            PS.Person_FirName,
            PS.Person_SecName
        
                
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
            
            
