1

I would like to concatenate the follow columns X, Y and Z, grouping by ProductToday and separating the values with comma " , " from my TSQL table.

enter image description here

As a result I would like to have it: enter image description here

Which query should I use to to it ?

P.S: some values from columns X,Y,Z are also Null.

ysth
  • 96,171
  • 6
  • 121
  • 214
Yan
  • 105
  • 7
  • 1
    Does this answer your question? [How to use GROUP BY to concatenate strings in MySQL?](https://stackoverflow.com/questions/149772/how-to-use-group-by-to-concatenate-strings-in-mysql) – NajiMakhoul Jan 24 '22 at 15:18
  • I'm guessing since this says TSQL that this is sql-server, not mysql; retagging – ysth Jan 24 '22 at 18:28

1 Answers1

1

You can use STRING_AGG() in sql server and GROUP_CONCAT in MySQL

For SQL Server:

SELECT 
    STRING_AGG(Z,',')
FROM 
    yourTable
GROUP BY ProductToday

For MySQL:

SELECT GROUP_CONCAT(Z SEPARATOR ',') FROM yourTable GROUP BY ProductToday;
Bilal Bin Zia
  • 596
  • 3
  • 12