Home > database >  Concatenate values & count rows in Oracle
Concatenate values & count rows in Oracle

Time:10-27

I have a table like this

enter image description here

Trying to get the output as below

enter image description here

The below SQL will do the concatenation of ID2 values for the same ID1. How do I get the count of ID1 based on the number of combinations of ID1,ID2 also?

SELECT
ID1,
LISTAGG(ID2, ', ')
WITHIN GROUP (ORDER BY ID2) "ID2Values"
FROM table_name

CodePudding user response:

SELECT ID1,
     LISTAGG(ID2, ', ') WITHIN GROUP (ORDER BY ID2) "ID2Values",
     count(*)
FROM table_name
group by ID1
  • Related