0

I need your help the resolve the following problem

     column1    Column2 
          1       a
          1       b
          2       c
          3       d
          4       e

I want to contact the elements of col2 for the same value of col1 and return the result as a variable in a single column.

Result : |1|a,b|

Thanks for your help.

Rida
  • 1
  • 2

2 Answers2

0

use concat function

 select concat(case when t.column1 is not null then t.column1 else end,
 case when t.column2 is not null then t.column2 else end) as col1 from   your_table as t
inner join
(
select column1 from your_table
group by column1
having count(*)>1
) as T1
t.column1=T1.column1
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Fill tbename and column1,column2 according your need

      SELECT  distinct     CAT.column1 AS [column1],
               STUFF((    SELECT ',' + SUB.column2 AS [text()]

                     FROM tbename SUB
                     WHERE
                     SUB.column1 = CAT.column1
                     FOR XML PATH('')
                       ), 1, 1, '' )

        AS [column2]
  FROM  tbename  CAT

for oracle below code 11g+

select column1, listagg(column2,',') within group( order by column1 ) 
  from tbename group by gr
sandeep rawat
  • 4,797
  • 1
  • 18
  • 36