
                 WITH cte AS (
                    select
                        DrugRequestPeriod_id,
                        PersonRegisterType_id,
                        DrugRequestKind_id,
                        DrugGroup_id,
                        DrugRequestCategory_id,
                        Lpu_id,
                        DrugRequest_Version
                    from
                        v_DrugRequest

                    where
                        DrugRequest_id = '500801000011059'
                ),
                dr_list(DrugRequest_id) as (
                    select
                        DrugRequest_id
                    from
                        v_DrugRequest dr

                        LEFT JOIN LATERAL (

                            select
                                i_aro.AccessRightsName_id
                            from
                                v_Lpu i_l

                                left join v_AccessRightsOrg i_aro  on i_aro.Org_id = i_l.Org_id

                            where
                                i_l.Lpu_id = dr.Lpu_id
                            limit 1
                        ) arl ON true
                    where
                        dr.DrugRequestPeriod_id = (SELECT DrugRequestPeriod_id FROM cte) and
                        COALESCE(dr.PersonRegisterType_id, 0) = COALESCE((SELECT PersonRegisterType_id FROM cte), 0) and

                        COALESCE(dr.DrugRequestKind_id, 0) = COALESCE((SELECT DrugRequestKind_id FROM cte), 0) and

                        COALESCE(dr.DrugGroup_id, 0) = COALESCE((SELECT DrugGroup_id FROM cte), 0) and

                        dr.DrugRequestCategory_id = (SELECT DrugRequestCategory_id FROM cte) and
                        COALESCE(dr.DrugRequest_Version, 0) = COALESCE((SELECT DrugRequest_Version FROM cte), 0) and

                        (
                        	dr.Lpu_id = (SELECT Lpu_id FROM cte) or
                        	arl.AccessRightsName_id is null
                        )
                )
                select
                    count(drpo.DrugRequestPersonOrder_id) as "cnt"
                from
                    v_DrugRequestPersonOrder drpo

                    LEFT JOIN LATERAL ( -- ищем записи по пациенту в других разнарядках в рамках заявочной кампании

                        select
                            i_drpo.DrugRequestPersonOrder_id
                        from
                            v_DrugRequestPersonOrder i_drpo

                            inner join dr_list i_dl on i_dl.DrugRequest_id = i_drpo.DrugRequest_id
                        where
                            i_drpo.Person_id = drpo.Person_id and
                            (
                                i_drpo.DrugComplexMnn_id is not null or
                                i_drpo.Tradenames_id is not null
                            )
                        limit 1
                    ) p ON true
                where
                    drpo.DrugRequest_id = '500801000011059' and
                    drpo.DrugComplexMnn_id is null and
                    drpo.Tradenames_id is null and
                    p.DrugRequestPersonOrder_id is null;
            
