with cte_nick as (
select coalesce(org_isaccess,1) as org_isaccess, lower(replace(org_nick,'"','')) as nick from v_org where org_enddate is null
)
,cte_name as (
select coalesce(org_isaccess,1) as org_isaccess, lower(replace(org_name,'"','')) as name from v_org where org_enddate is null
)
, cte as (
select v_org.*, smo.orgsmo_id, smo.orgsmo_f002smocod
from v_org 
left join v_orgsmo smo on smo.org_id = v_org.org_id
where 
(lower(replace(org_nick,'"','')) in (select nick from cte_nick group by nick having count(*)>1 and max(org_isaccess)=2)
or lower(replace(org_name,'"','')) in (select name from cte_name group by name having count(*)>1 and max(org_isaccess)=2)
) 
and org_enddate is null
limit 10
)
select cte.*,
(select count(1) as cnt 
	from v_Evn e
	inner join v_lpu l on l.lpu_id = e.lpu_id and l.org_id = cte.org_id
) as Evn_Count, 
(select count(1) as cnt 
	from v_EvnStick es
	inner join v_lpu l on l.lpu_id = es.lpu_id and l.org_id = cte.org_id
) as EvnStick_Count, 
(select count(1) as cnt 
	from v_Polis p
	where p.orgsmo_id = cte.orgsmo_id
) as Polis_Count
from cte
order by org_nick
