
                
                
                SELECT 
                PDDPE.PersonDopDispPlanExport_id as "PersonDopDispPlanExport_id",
                PDDPE.PersonDopDispPlanExport_FileName as "PersonDopDispPlanExport_FileName",
                to_char (PDDPE.PersonDopDispPlanExport_expDate, 'dd.mm.yyyy') as "PersonDopDispPlanExport_expDate",
                PC.pcount as "PersonDopDispPlanExport_Count",
                to_char (PDDPE.PersonDopDispPlanExport_impDate, 'dd.mm.yyyy') as "PersonDopDispPlanExport_impDate",
                DCP.DispCheckPeriod_Name as "DispCheckPeriod_Name",
                OS.OrgSMO_Nick as "OrgSMO_Nick",
                PDDPE.PersonDopDispPlanExport_isUsed as "PersonDopDispPlanExport_isUsed",
                PDDPE.PersonDopDispPlanExport_DownloadLink as "PersonDopDispPlanExport_DownloadLink",
                PDDPE.PersonDopDispPlanExport_PackNum as "PersonDopDispPlanExport_PackNum",
                ERR."PlanObsDispExport_CountErr" as "PlanObsDispExport_CountErr"
                
                FROM
                
                v_PlanObsDisp POD
                left join v_PlanObsDispLink PODL on PODL.PlanObsDisp_id=POD.PlanObsDisp_id
                left join v_DispCheckPeriod DCP on DCP.DispCheckPeriod_id = POD.DispCheckPeriod_id
                inner join PersonDopDispPlanExportLink PDDPEL on PDDPEL.PlanObsDispLink_id=PODL.PlanObsDispLink_id
                left join v_PersonDopDispPlanExport PDDPE on PDDPE.PersonDopDispPlanExport_id = PDDPEL.PersonDopDispPlanExport_id

                left join lateral (
                    select
                        count(PEE.PlanObsDispLink_id) as "PlanObsDispExport_CountErr"
                    from
                        v_PlanObsDispErrorExport PEE
                        left join v_PlanObsDispLink PODL1 on PODL1.PlanObsDisp_id = POD.PlanObsDisp_id
                    where
                        PEE.PlanObsDispLink_id = PODL1.PlanObsDispLink_id
                ) ERR on true

                LEFT JOIN LATERAL (
                    select count(distinct PD.Person_id) pcount
                    from v_PlanObsDispLink PL
                    left join v_PersonDisp PD on PD.PersonDisp_id = PL.PersonDisp_id
                    left join v_PersonDopDispPlanExportLink EL on El.PlanObsDispLink_id = PL.PlanObsDispLink_id
                    where
                    EL.PersonDopDispPlanExport_id=PDDPE.PersonDopDispPlanExport_id
                ) PC ON TRUE
                LEFT JOIN LATERAL (
                    select
                        OS.OrgSMO_Nick
                    from v_PlanObsDispLink PODL
                    left join v_PersonDisp PD on PD.PersonDisp_id = PODL.PersonDisp_id
                    left join v_PersonState PS on PS.Person_id = PD.Person_id
                    left join v_Polis p on p.Polis_id = PS.Polis_id
                    left join v_OrgSMO OS on OS.OrgSMO_id = p.OrgSMO_id
                    where PODL.PlanObsDisp_id = POD.PlanObsDisp_id limit 1
                ) OS ON TRUE
                 WHERE 
                (1 = 1) and POD.PlanObsDisp_id = '450101000000617'
                
                
                        ORDER BY
                            
                PDDPE.PersonDopDispPlanExport_id
                
                OFFSET 0 ROWS
                FETCH NEXT 100 ROWS ONLY
                
