You can use the distinct
, case..when
and LISTAGG
as follows:
select id,
coalesce(listagg(case when isclient = 'CLIENT' then contactcode end,',')
within group (order by contactcode), '0') as maincode,
coalesce(listagg(case when isclient = 'NOT CLIENT' then contactcode end,',')
within group (order by contactcode), '0') as ref
(select distinct id, contactcode,
case when contactperson = 'CLIENT' then 'CLIENT' else 'NOT CLIENT' end as isclient
from your_table t
where t.contactcode in ('X', 'Y', 'Z') ) t
group by id
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…