[英]Oracle LISTAGG DISTINCT with multiple conditions
For Oracle12, I have the table shown below对于 Oracle12,我有如下所示的表格
ID ![]() |
CONTACTCODE![]() |
CONTACTPERSON![]() |
---|---|---|
1 ![]() |
X ![]() |
CLIENT![]() |
1 ![]() |
X ![]() |
CLIENT![]() |
1 ![]() |
X ![]() |
WIFE![]() |
1 ![]() |
Y![]() |
CLIENT![]() |
1 ![]() |
Z ![]() |
WIFE![]() |
1 ![]() |
Z ![]() |
CLIENT![]() |
1 ![]() |
Z ![]() |
HUSBAND![]() |
1 ![]() |
W ![]() |
CLIENT![]() |
2 ![]() |
Y![]() |
CHILD![]() |
2 ![]() |
Y![]() |
WIFE![]() |
2 ![]() |
Y![]() |
CLIENT![]() |
3 ![]() |
Y![]() |
HUSBAND![]() |
3 ![]() |
W ![]() |
CLIENT![]() |
And I want the result like:我想要这样的结果:
Expected table:预期表:
ID ![]() |
MAIN![]() |
REF![]() |
---|---|---|
1 ![]() |
X,Y, Z ![]() |
X,Z ![]() |
2 ![]() |
Y![]() |
Y![]() |
3 ![]() |
0 ![]() |
Y![]() |
I used row_number partition by ID, CONTACTCODE and then LISTAGG where row_number = 1 but I had many problems.我使用按 ID、CONTACTCODE 和 LISTAGG 的 row_number 分区,其中 row_number = 1 但我遇到了很多问题。
You can use the distinct
, case..when
and LISTAGG
as follows:您可以使用
distinct
、 case..when
和LISTAGG
,如下所示:
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
You can use row_number()
and conditional aggregation:您可以使用
row_number()
和条件聚合:
select id,
(case when sum(case when contactperson = 'Client' then 1 else 0 end) > 0
then listagg(case when seqnum = 1 and contactcode in ('X', 'Y', 'Z')
then contactcode
end, '') within group (order by contactcode)
end),
listagg(case when seqnum = 1 and contactcode in ('X', 'Y', 'Z')
then contactcode
end, '') within group (order by contactcode)
from (select t.*,
row_number() over (partition by id, contactcode order by id) as seqnum
from t
) t
group by id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.