472,342 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Combine column multiple rows into one row

Is it possible to combine multiple rows returned from select statement
into one row?

SELECT NAME FROM TABLE1;

I want all names to be combined into one row seperated by commas.

Dec 7 '05 #1
5 37550
Antanas wrote:
Is it possible to combine multiple rows returned from select statement
into one row?

SELECT NAME FROM TABLE1;

I want all names to be combined into one row seperated by commas.


Use recursive SQL to do the concatenation. Examples are posted here every
few weeks...

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 7 '05 #2
Thanks Knut.

Dec 7 '05 #3
Antanas wrote:
Is it possible to combine multiple rows returned from select statement
into one row?

SELECT NAME FROM TABLE1;

I want all names to be combined into one row seperated by commas.

This is quickly turning into a FAQ. What's going on all of a sudden?Wat
is this used for?
Ideally pretty printing is the job of your application.
Anyway this is one of many way to do it (untested):

WITH source(rn, name) AS (SELECT ROW_NUMBER() OVER() AS rc,
name
FROM T),
rec(n, name) AS (SELECT 2, CAST(name AS VARCHAR(2000)
FROM source WHERE rn = 1
UNION ALL
SELECT n+1, rec.name || ',' || source.name
FROM source WHERE n = rn)
SELECT name FROM rec ORDER BY n DESC FETCH FIRST ROW ONLY;

If you have a primary key on the table you don't need the ROWNUMBER() mess.
Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Dec 8 '05 #4
The basic principle of a tiered architecture is that display is done in
the front end and never in the back end. This a more basic programming
principle than just SQL and RDBMS.

So be sure to comment the code so people will know that you are
destroying 1NF on purpose and do not know about C/S architecture. That
might help someone maintian the code and correct it after you are gone.

Dec 8 '05 #5
--CELKO-- wrote:
The basic principle of a tiered architecture is that display is done in
the front end and never in the back end. This a more basic programming
principle than just SQL and RDBMS.

So be sure to comment the code so people will know that you are
destroying 1NF on purpose and do not know about C/S architecture. That
might help someone maintian the code and correct it after you are gone.


There are perfectly legitimate reasons for collapsing multiple rows
into one on the back end. Perhaps you've got a presentation team that
needs all the items on a single row - to avoid expanding their result
set? You could just write a query for them (they might not be able
to), but maybe it's safer to put that into a view and support it on the
back end. Or maybe for performance reasons you put the data into an
indexed summary table using partitioning/mdc/etc based on that query.

Or maybe someone has a commercial product, or an interface to a
commercial product. And in this case they don't give you the option of
adding an entire table to describe your list well - they only give you
a single column. You'll have to make do.

But other than that, your comment was probably very valuable.

buck

Dec 9 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: RotterdamStudents | last post by:
Hello there, i have a strange problem. I can't get php to insert multiple rows at once in a MySQL database. I use the $sql = "INSERT INTO...
1
by: Valerie | last post by:
Hi everyone, I really appreciate if anyone could help me with this tricky problem that I'm having. I'm looking for a sample script to combine...
2
by: laurenq uantrell | last post by:
IS there a way to combine all matching rows in a table so that it outputs as one row, for example: tblMyStuff UniqueID int IDENTITY ParentID...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which...
11
by: UDBDBA | last post by:
Hi: This is a merge questions which has been posted and answered... in my case need more clairification when target table (tableB) matched...
7
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is...
18
by: Apple001 | last post by:
Hi all! I am having trouble with joining multiple rows into one row. I will appreciate any help. For columns in the query, I have: invID(autot...
1
by: Sanjaylml | last post by:
I have a form through which, e-mail address of parties are showing through datasheet mode. Is their any way to club the data of multiple rows in one...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.