0

How can I get the result specified below?

I tried using the LISTAGG function, but it does not eliminate duplicates.

Is there a way to eliminate duplicates at the database itself instead of fetching the values into the service and then deduping them?

Dataset:

col0   col1
1      7
1      8
1      8
1      2
1      9
2      9
3      10
3      11
3      12

Desired result:

col0   col1
1      2,7,8,9
2      9
3      10,11,12

Attempted SQL (that does not eliminate duplicates):

SELECT 
  col0, 
  SUM(col2 + col3) AS mycount
  LISTAGG(col1, ',') within GROUP (ORDER BY col1) 
FROM mytable 
WHERE 
  _time >= TIMESTAMP '2024-12-03 00:00:00' AND
  _time <  TIMESTAMP '2024-12-03 01:00:00'
GROUP BY col0
ORDER BY mycount DESC
LIMIT 10
;
1
  • add the DISTINCT keyword to the LISTAGG function Commented Dec 4, 2024 at 23:13

1 Answer 1

1

You need to use DISTINCT within LISTAGG

Fiddle

SELECT col0,
       LISTAGG(DISTINCT col1, ',') WITHIN GROUP (ORDER BY col1) AS col1
FROM test
GROUP BY col0;

Output

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.