1

I have a table where storing details

ID     NAME
1       A
2       A
1       A

I need the output like

ID     Name   Count
1,2     A      3

Please help to get the output like that in oracle select query

Sreejith A
  • 27
  • 7

1 Answers1

2

In Oracle, you can use listagg(), but it has no distinct option. So, use a subquery and two levels of aggregation:

select listagg(id, ',') within group (order by id) as id, name, sum(cnt)
from (select id, name, count(*) as cnt
      from t
      group by id, name
     ) x
group by name;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786