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

SQL question: how to select multiple row into one row

Hi

If I have a table with two colum ITEM and NAME, like the following

ITEM NAME
======= ============
Apple Nancy
Orange Nancy
Lemon Margaret
Orange Margaret
Banana Tom

and I want to have a result like this

Apple Nancy
Orange Nancy, Margaret
Lemon Margaret
Banana Tom

Can I do it in SQL?

Thank you

Jacinle
Nov 12 '05 #1
3 7275
Jacinle,

Got to developer Works and under DB2 search for papers by Knut Stolze.
Hs as an article about user defined aggregates wich is what you need here.

You find the webpage of DB2's homepage.

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

Nov 12 '05 #2
Jacinle Young <ja***********@hongkong.com> wrote:
Hi

If I have a table with two colum ITEM and NAME, like the following

ITEM NAME
======= ============
Apple Nancy
Orange Nancy
Lemon Margaret
Orange Margaret
Banana Tom

and I want to have a result like this

Apple Nancy
Orange Nancy, Margaret
Lemon Margaret
Banana Tom

Can I do it in SQL?


Either you use an external function to do your aggregation, then you might
want to have a look here:

http://www7b.boulder.ibm.com/dmdd/li...309stolze.html

Or you do the string concatenation in SQL using a recursive query. This
would be similar to parsing the string into its pieces, which is described
here:

http://www7b.boulder.ibm.com/dmdd/li...03stolze1.html
Your query could look like this (untested):

WITH t1(rowNum, item, name) AS
( SELECT rownumber() over(), item, name
FROM yourTable
GROUP BY item ),
t2(item, list, cnt) AS
( SELECT item, name, 1
FROM t1
WHERE rowNum = 1
UNION ALL
SELECT t2.item, list || ', ' || name, cnt + 1
FREM t2, t1
WHERE t2.item = t1.item AND
t2.cnt + 1 = t1.rowNum )
SELECT item, list
FROM t2
WHERE ( item, cnt ) IN ( SELECT item, MAX(rowNum)
FROM t1
GROUP BY item )

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #3
AK
yes, you can use a recursive query to accopmplish the task. No problem
Nov 12 '05 #4

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

Similar topics

3
by: Simon Harvey | last post by:
Hi everyone, I was wondering if it is possible, to use SQL to return more than one table at a time into a dataset. I only know the basics of SQL and so I'm not sure if I'm just asking a stupid...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
3
by: cg_news | last post by:
Hello, I have what I hope is a simple sql (DB2/AS400) question. Assume 2 files exist. One is a transaction file with two fields, the transaction ID and the transaction name. File two is keyed to...
5
by: Keith | last post by:
Good Morning, I am new to .Net so forgive me if this is question is kind of out there. Is there a way to consolidate multiple Dim statements for Sql Commands? I have a page that has around 50 Dim...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
6
by: osa | last post by:
I have a table 'cust' as follows : rec_id name dept ------- ------- ------- 1, 'john', 'a' 2, 'tom', 'b' 3, 'tom', 'a' 4, ...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.