Merge specific row in whole table
Merge specific row in whole table
I'm working on a web app, considering how to merge specific row in whole table.
Here is the example
Original data:
Name Date
-------------------
Jason Jul 2017
Tom Jun 2018
Andy Jun 2018
Mary Jun 2018
Alex Feb 2018
David Aug 2018
I'd like to make the same result into one big cell,like what we can do in Excel
Name Date
------------------
Jason Jul 2017
Tom
Andy Jun 2018
Mary
Alex Feb 2018
David Aug 2018
Is this possible in SQL Server? Or I need to do this change in the java or css layer
you need to do this in your front end application where the result is display. Not in SQL Server
– Squirrel
Aug 21 at 2:38
1 Answer
1
The exact output you want is something which is better handled in your presentation layer, e.g. using something like PHP, Java, etc. Perhaps the closest we might get to your output in SQL Server would be to do a group concatenation of the names by date, something like this:
SELECT
t1.Date,
STUFF((
SELECT ',' + t2.Name
FROM yourTable t2
WHERE t1.Date = t2.Date
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS Names
FROM (SELECT DISTINCT Date FROM yourTable) t1;
Demo
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
SQL tables have rows and columns. All rows have the same set of columns. Your result is not a table in this sense. Hence, you cannot do exactly what you want in SQL.
– Gordon Linoff
Aug 21 at 1:35