473,394 Members | 1,715 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,394 software developers and data experts.

data from several rows in one field

Hi,
I'm looking for a possebility to concat data from several rows of one
table into one field.

Example Table:

No. Name
------------------
1 Peter
1 Alec
1 Jane
2 Annie
2 Louis

I'd like to receive a result like this:

1 Peter, Alec, Jane
2 Annie, Louis

Is it possible to manage this using one SQL-Statement?

Any help is appreciated.
Thanks in advance
Benjamin
using IBM DB2 7.2
Nov 12 '05 #1
8 2401
you need to use recursive SQL todo this....Knut posted a good example of
this in action..easily adapted to return your desired result, see this
thread:

http://groups.google.com/groups?hl=e...readm=bl15kd%2
41uj%241%40fsuj29.rz.uni-jena.de&rnum=2&prev=/groups%3Fq%3Dconcatenate%2Brow
s%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp
..databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm
-db2%26hl%3Den%26lr%3D%26ie%3DUTF-8%26safe%3Doff%26scoring%3Dd%26selm%3Dbl15
kd%25241uj%25241%2540fsuj29.rz.uni-jena.de%26rnum%3D2

"Benjamin Sch?neck" <bs********@gmx.de> wrote in message
news:37**************************@posting.google.c om...
Hi,
I'm looking for a possebility to concat data from several rows of one
table into one field.

Example Table:

No. Name
------------------
1 Peter
1 Alec
1 Jane
2 Annie
2 Louis

I'd like to receive a result like this:

1 Peter, Alec, Jane
2 Annie, Louis

Is it possible to manage this using one SQL-Statement?

Any help is appreciated.
Thanks in advance
Benjamin
using IBM DB2 7.2

Nov 12 '05 #2
Thanks a million. It was exactly what I needed!
Works out perfectly!!!
"Lee Dilworth" <le****************@hotmail.com> schrieb im Newsbeitrag
news:br*************@ID-118488.news.uni-berlin.de...
you need to use recursive SQL todo this....Knut posted a good example of
this in action..easily adapted to return your desired result, see this
thread:

http://groups.google.com/groups?hl=e...readm=bl15kd%2 41uj%241%40fsuj29.rz.uni-jena.de&rnum=2&prev=/groups%3Fq%3Dconcatenate%2Brow s%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp ..databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm -db2%26hl%3Den%26lr%3D%26ie%3DUTF-8%26safe%3Doff%26scoring%3Dd%26selm%3Dbl 15 kd%25241uj%25241%2540fsuj29.rz.uni-jena.de%26rnum%3D2

"Benjamin Sch?neck" <bs********@gmx.de> wrote in message
news:37**************************@posting.google.c om...
Hi,
I'm looking for a possebility to concat data from several rows of one
table into one field.

Example Table:

No. Name
------------------
1 Peter
1 Alec
1 Jane
2 Annie
2 Louis

I'd like to receive a result like this:

1 Peter, Alec, Jane
2 Annie, Louis

Is it possible to manage this using one SQL-Statement?

Any help is appreciated.
Thanks in advance
Benjamin
using IBM DB2 7.2


Nov 12 '05 #3
I've found myself needing this functionality twice for distinct
reasons in the last month. I had used both an original recursive SQL
and used Knut's methodology to get the results that I wanted.

I was thinking that this sort of looks like something that could
possibly written as a column function, much like SUM, or AVG, except
that it would operate on text instead of numerics. Does it seem
reasonable that this could be done as an SQL function or a UDF? Before
I started committing my free hours to such a project, I thought I
would check if any of the database gurus out here had ever
contemplated it and rejected it because of its complexity or
unfeasibility.

Evan

"Benjamin Schöneck" <be****************@dialog-edv.de> wrote in message news:<br*************@news.t-online.com>...
Thanks a million. It was exactly what I needed!
Works out perfectly!!!
"Lee Dilworth" <le****************@hotmail.com> schrieb im Newsbeitrag
news:br*************@ID-118488.news.uni-berlin.de...
you need to use recursive SQL todo this....Knut posted a good example of
this in action..easily adapted to return your desired result, see this
thread:

http://groups.google.com/groups?hl=e...readm=bl15kd%2

41uj%241%40fsuj29.rz.uni-jena.de&rnum=2&prev=/groups%3Fq%3Dconcatenate%2Brow

s%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp

.databases.ibm-db2%2Bgroup:comp.databases.ibm-db2%2Bgroup:comp.databases.ibm
-db2%26hl%3Den%26lr%3D%26ie%3DUTF-8%26safe%3Doff%26scoring%3Dd%26selm%3Dbl

15

Nov 12 '05 #4
> I was thinking that this sort of looks like something that could
possibly written as a column function, much like SUM, or AVG, except
that it would operate on text instead of numerics. Does it seem
reasonable that this could be done as an SQL function or a UDF? Before
I started committing my free hours to such a project, I thought I
would check if any of the database gurus out here had ever
contemplated it and rejected it because of its complexity or
unfeasibility.


SQL Server are offering a PIVOT operator (along with UNPIVOT to do the
opposite) to acheive this in the Yukon release.
eg:
SELECT *
FROM ItemAttributes AS ATR
PIVOT
(
MAX(value)
FOR attribute IN([artist], [name], [type], [height], [width])
) AS PVT
WHERE itemid IN(5,6)

I've not seen any mention of it in any existing or proposed ANSI/SQL
standard. I'd find the feature useful for generating spreadsheet
output, but the recursive method works well enough.

See the following link for details:
http://msdn.microsoft.com/library/de...SQLEnhance.asp

Christian.
Nov 12 '05 #5
What Benjamin needs here is a user defined aggreate.
Informix IDS has this technology and it is being looked at as a
requirements. Too early to comment on a ship vehicle though.
User defined aggregates can be simulated with scratchpad UDF to some degree.
But again, recursion usually does the job as well and works well enough.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #6
AK
Knut Stolze has recently published a great article on customized sums on DB2DD.
I think oyu could try the same approach, a UDF with a scratchpad.
Nov 12 '05 #7
AK
Hi Serge,

the way I'm reading execution plans, a cursor seems to be open for
every iteration of a recursive query, and a temporary result set is
opened (and later discarded) once per every iteration. The performance
of recursive queries seems to depend more on number of iterations than
on anything else.

Please correct me if I'm wrong.
Nov 12 '05 #8
You are correct that there is a cursor, but it stays open (it gets
secondary EOFs and can recover from them The TEMP stays. It just
accumulates. I think it's called semi-naive recursion in the literature.
(I by contrast am naive and cursing)
Rows
RETURN
( 1)
Cost
I/O
|
334.333
TBSCAN
( 2)
173.39
0
|
334.333
TEMP
( 3)
140.897
0
|
334.333
UNION
( 4)
118.422
0
/----+---\
333.333 1
TBSCAN TBSCAN
( 5) ( 6)
112.466 0.0048
0 0
| |
334.333 1
TEMP TABFNC: SYSIBM
( 3) GENROW
140.897
0

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #9

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

Similar topics

5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
4
by: MikeY | last post by:
Hi everyone, I have posted earlier this week, but I'm still scratching my head trying to figure out how to change/modify my data back to my db. Using C# Windows forms. I am trying to learn how...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
3
by: snowweb | last post by:
I'm creating my first web application in PHP/MySQL, however, when I go to view the database contents, the data has been stored in the wrong columns. I've gone through my insertion code with a fine...
2
by: mael.iosa | last post by:
Hi, I'm new to this group and fairly new to Access. I have a bunch of data, and after several other queries, I generate the following query which has two fields: Bin, Time 20 3.5 20 3.9 20...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
2
by: =?Utf-8?B?dmFuZGls?= | last post by:
I have a web app that I have been working on for the last couple of weeks trying to solve this problem. One page contains a GridView with four base columns, and an unknown number of columns to be...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.