1
 empno     emailID
--------------------
1           A@gm.com
2           B@gm.com
3           C@gm.com
2           BB@gm.com
1           AA@gm.com
1           AAA@gm.com
3           CC@gm.com

Write a query so that result will be :--

 empno     emailID
--------------------
1           A@gm.com,AA@gm.com,AAA@gm.com
2           B@gm.com,BB@gm.com
3           C@gm.com,CC@gm.com
David M
  • 71,481
  • 13
  • 158
  • 186
Rosh
  • 11
  • 1

1 Answers1

1

What you are looking for is called GROUP_CONCAT in MySQL and looks like this:

SELECT empno, GROUP_CONCAT(emailID)
FROM yourtable
GROUP BY empno

Unfortunately GROUP_CONCAT does not exist in SQL Server but you can use FOR XML PATH or one of the other workarounds posted as answers to this question.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452