Suppose there was a SQL table like this:
Label Name | Last name | Address Line 1 | Address line 2
-----------+-----------+----------------+-------------------
John | Smith | 100 Any St | Anytown, WI, 50000
Jane | Smith | 100 Any St | Anytown, WI, 50000
Bill | Doe | 200 Any St | Anytown, WI, 50000
Sam | Rodgers | 200 Any St | Anytown, WI, 50000
Lisa | Thompson | 300 Any St | Anytown, WI, 50000
Walter | Thompson | 300 Any St | Anytown, WI, 50000
Chuck | Thompson | 300 Any St | Anytown, WI, 50000
What SQL query would produce a table like this?:
Label Name | Last name | Address Line 1 | Address line 2
------------------------+-----------+----------------+-------------------
John and Jane | Smith | 100 Any St | Anytown, WI, 50000
Bill | Doe | 200 Any St | Anytown, WI, 50000
Sam | Rodgers | 200 Any St | Anytown, WI, 50000
Lisa, Walter, and Chuck | Thompson | 300 Any St | Anytown, WI, 50000
You should do these kinds of things in the presentation layer, rather than in the DBMS.
But if you really want to do this at the DBMS level, and your DBMS is MS SQL Server (as your comment seems to indicate), you can do the following:
First, create a function that concatenates related
[Label Name]values (using ‘,’ and ‘and’ as separators):Then use the function like this:
Result: