473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 38099
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 database (a,b,c,d,e) VALUES ('$a', '$b' ,'$c', '$d',...
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 data in multiple rows into one row. I'm using...
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 int SomeSuch nvarchar(50) SomeSuch2 nvarchar(50) ...
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 is contained in an ASPX page. I haven't been...
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 multiple rows to be updated based on the ON condition...
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 how the data is at the moment: Company_ID ...
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 number), entryDate, invDate, vendor, invoiceAmount...
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 column? Like : ajaimathur@rediffmail.com...
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 the selected rows in a single stroke? just like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.