Code:
SELECT temp_id
, a_id
, SUM(emp_cnt) AS emp_cnt
FROM (
/* insert your query here */
select a.temp_id,
a.id as a_id,
b.id as b_id,
(select count(*) cnt from EMP where id = b.id) as EMP_CNT
from DEPT a, DEPT b
where a.domain = 'X'
and b.domain <> 'X'
and a.temp_id = b.temp_id
AND b.temp_id in (select temp_id from DEPT group by temp_id having count(*) > 2)
) AS derivedtable
GROUP
BY temp_id
, a_id
