Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys I have a small issue in SQL I have a table shown below

each name has at least 2 emails shown in the table I want an output in which the name is in one row and all emails side by side
if any solution help out

Table
col1         |       col2
__________________________
abhi         | xyz@email
abhi         | abc@email
abhi         | rst@email
ragu         | str@email
ragu         | pqr@email

expected output:

col1         |     col2
abhi         |     xyz@email,abc@email,rst@email
ragu         |     str@email,pqr@email[enter image description here][1]


  [1]: https://i.stack.imgur.com/f29mF.png


What I have tried:

I Have No Idea How To Get It If Any Solution Help
Posted
Updated 29-Jun-21 9:05am

1 solution

You can use LISTAGG[^]

Something like
SQL
SELECT DISTINCT 
       col1,
       LISTAGG(col2, ',') 
          WITHIN GROUP (ORDER BY col2)
          OVER (PARTITION BY col1)
FROM Table
 
Share this answer
 
Comments
Maciej Los 29-Jun-21 16:39pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900