I have the following data
create table company (com_cd varchar2(100), act_id number(10));
insert into company values ('IBM',100);
insert into company values ('IBM',200);
insert into company values ('IBM',300);
insert into company values ('HP',50);
insert into company values ('HP',85);
Then I ran this query
select COM_CD,
REGEXP_SUBSTR(DUP_ACT_ID,'[^,]+',1,1) AS DUP_ACT_1,
REGEXP_SUBSTR(DUP_ACT_ID,'[^,]+',1,2) AS DUP_ACT_2,
REGEXP_SUBSTR(DUP_ACT_ID,'[^,]+',1,3) AS DUP_ACT_3
FROM (
SELECT COM_CD, LISTAGG(ACT_ID,',') DUP_ACT_ID
FROM COMPANY
GROUP BY COM_CD HAVING COUNT(*) > 1
);
COM_CD DUP_ACT_1 DUP_ACT_2 DUP_ACT_3
HP 50 85
IBM 100 200 300
It gave me the correct output but I won't know in a table with thousands of values whether there would be 1 or 10 duplicate ACT_ID. So I am looking for a solution where I don't use regexp_substr and instead get the output somehow based on number of values in the DUP_ACT_ID column. Any help is appreciated.
If you don't have to have a single row per com_cd, you could use something like the following:
You didn't mention what should happen if we see the same act_id multiple times within a com_cd, I assumed you would only want to see it once.
If you need a new column for each dup_act_id, you could pivot the above query:
But in that case you'd still have to add a new section to the in() clause for each additional dup_act_id.