
            
                -- addit with
                with
                    
                dr_list(DrugRequest_id) as (
                    select
                        dr2.DrugRequest_id
                    from
                        v_DrugRequest dr

                        left join v_DrugRequest dr2  on

                            dr2.DrugRequestPeriod_id = dr.DrugRequestPeriod_id and
                            COALESCE(dr2.PersonRegisterType_id, 0) = COALESCE(dr.PersonRegisterType_id, 0) and

                            COALESCE(dr2.DrugRequestKind_id, 0) = COALESCE(dr.DrugRequestKind_id, 0) and

                            COALESCE(dr2.DrugGroup_id, 0) = COALESCE(dr.DrugGroup_id, 0) and

                            COALESCE(dr2.DrugRequest_Version, 0) = COALESCE(dr.DrugRequest_Version, 0) and

                            dr2.DrugRequestCategory_id = dr.DrugRequestCategory_id
                    	
                    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 = dr2.Lpu_id
                        limit 1
                    ) arl On true
                
                    where
                        dr.DrugRequest_id = '2866'
                         and (dr2.Lpu_id = dr.Lpu_id or arl.AccessRightsName_id is null)
                )
            , curDT as (select dbo.tzGetDate() as curDate), curDTY as (select date_part('year',dbo.tzGetDate()) as curDate), curD as (select cast(to_char(dbo.tzGetDate(), 'YYYYMMDD') as timestamp) as curDate)
                -- end addit with
            
            select
                count(drpo.DrugRequestPersonOrder_id) as cnt
            from
                v_DrugRequestPersonOrder drpo
                left join v_PersonState ps  on ps.Person_id = drpo.Person_id
                left join v_DrugRequest dr  on dr.DrugRequest_id = drpo.DrugRequest_id
                left join v_DrugRequestPeriod drp  on drp.DrugRequestPeriod_id = dr.DrugRequestPeriod_id
                
                LEFT JOIN LATERAL (

                    select
                        ii_drpo.DrugRequestPersonOrder_id
                    from
                        v_DrugRequestPersonOrder ii_drpo

                        inner join dr_list ii_dl on ii_dl.DrugRequest_id = ii_drpo.DrugRequest_id
                    where
                        ii_drpo.Person_id = drpo.Person_id and
                        (
                            ii_drpo.DrugComplexMnn_id is not null or
                            ii_drpo.Tradenames_id is not null
                        )
                    limit 1
                ) drpo_wd ON true
            
            
                where
                    -- where
                    drpo.DrugRequest_id = '2866' and drpo.DrugComplexMnn_id is null and drpo.Tradenames_id is null and drpo_wd.DrugRequestPersonOrder_id is null
                    -- end where
            
        
