Combine Multiple Rows into One Cell Using SQL

62 Views Asked by At

enter image description here

Hello guys, maybe this is a basic SQL question. How can I combine multiple rows into one cell using SQL?

Expected Result:

enter image description here

Thanks in advance!

2

There are 2 best solutions below

2
Citlalli hg On

You can use CONCAT function to add strings together using SQL, identify the names of the columns you want to merge together and write a query with this syntax: CONCAT(no, " ", add) you can us AS function to return the string in a new column you can define the name you want after

0
Albert D. Kallal On

You can't do this in a query output, but you most certainly can do this in a report.

So, say this simple query of some City and Hotels:

enter image description here

However, a datasheet output is not suitable for formatted output, and thus you can (should) use a report, and that will allow you to group say the city together, and hide repeating values.

So, a report then can output the above like this:

enter image description here

And note that you can place a report (as sub form) into an existing form, so even if you not "reporting" the data but only for display, then once again, a report will allow such grouping.

As a general rule, data output to a datasheet view is not formatted, and such output is quite much only for developers. However, for your "users" of the application, then formatting such query output can and should be done with a report.