Prior to Oracle 11.2 I was using a custom aggregate function to concatenate a column into a row. 11.2 Added the LISTAGG function, so I am trying to use that instead. My problem is that I need to eliminate duplicates in the results and don't seem to be able to do that. Here is an example.
CREATE TABLE ListAggTest AS (
SELECT rownum Num1, DECODE(rownum,1,'2',to_char(rownum)) Num2 FROM dual
CONNECT BY rownum<=6
);
SELECT * FROM ListAggTest;
NUM1 NUM2
---------- ---------------------
1 2
2 2 << Duplicate 2
3 3
4 4
5 5
6 6
What I want to see is this:
NUM1 NUM2S
---------- --------------------
1 2-3-4-5-6
2 2-3-4-5-6
3 2-3-4-5-6
4 2-3-4-5-6
5 2-3-4-5-6
6 2-3-4-5-6
Here is a listagg version that is close, but doesn't eliminate duplicates.
SELECT Num1, listagg(Num2,'-') WITHIN GROUP (ORDER BY NULL) OVER () Num2s
FROM ListAggTest;
I have a solution, but it's worse than continuing to use the custom aggregate function.
order by nullbeorder by Num2or am I getting confused? – Jack Douglas♦ Mar 8 '12 at 19:092-3-4-5-6necessarily. It did make a difference for my regex solution which I've fixed as it relied on dupes being adjacent. – Jack Douglas♦ Mar 8 '12 at 20:21