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

select query, return CSV in field

Hi,

I have the following data being returned:

ID Region
---- ------------
1 northwest
1 southeast
2 midwest
Is it possible to use SQL to return this data as:

ID Region
---- ------------
1 northwest,southeast
2 midwest

Currently I'm using code to loop through and create the second data
structure, but it would be nice if I could have the database take care
of it for me.

Thanks,

Ben

May 24 '06 #1
7 1783
aj
This was asked earlier today.

Brian Tkatch replied:
"Search for XMLAGG() functions in the newsgroups."

Hope that helps.

aj

be***********@gmail.com wrote:
Hi,

I have the following data being returned:

ID Region
---- ------------
1 northwest
1 southeast
2 midwest
Is it possible to use SQL to return this data as:

ID Region
---- ------------
1 northwest,southeast
2 midwest

Currently I'm using code to loop through and create the second data
structure, but it would be nice if I could have the database take care
of it for me.

Thanks,

Ben

May 25 '06 #2

Hi aj,Ben

I cant use Xml function...is it possible using sql

Thanks

May 25 '06 #3
Thyis is called violation of First Normal Form and violationof the
basic prinicple of tiered architecture. You are suipposed to be doign
formatting in the front end, not the database. You might want to read a
basic RDBMS book before you start programming.

May 25 '06 #4
rAinDeEr wrote:

Hi aj,Ben

I cant use Xml function...is it possible using sql


Yes you can with SQL ... by using one of DB2's XML functions (XMLAGG as
others have pointed out) for something it wasn't meant to be used for
:-).

It's a hack, and it's not obvious because as Celko points out this
doesn't "fit" relational theory. If you search this newsgroup on Google
Groups for "XMLAGG" or "Aggregating Strings" the answer is in a post
fairly high up the results list.

Alternatively, if you meant your version of DB2 is old enough that it
doesn't support the XMLAGG function ... well, you'll have to create an
external UDF to handle this or, better still, take Celko's suggestion
and do it in the application layer.
Dave.

--

May 25 '06 #5
It's a DB2 FUNCTION. There's a good example posted in this newsgroup.
Just search for it, and one or two ought to pop up.

B.

May 25 '06 #6
Hi,

First of all I seek forgiveness in the name of all RDBMS gods for this
question. But strange requirements in front end always result in this
kind of questions.

A family table with one level of hierarchy

================================================== ===========================
PERSON NAME PARENT NAME
----------------------- -----------------------
JACK NULL
PETER NULL
JOHN JACK
ANNIE JACK
THOMAS NULL
TOM THOMAS
================================================== ===========================
The front end requirement states that two record should be shown per
page as a single column with (parent name alone if no children is
present) or (parent name,child names... if children is present). Front
end formatting is also nice but the pagination sorts of mandates to get
the children seperately and add them to the parents shown in the page.
Its just a matter of executing one more query but still the existence
of column functions like sum,max.min etc makes me think if there is
column function like CONCAT_ROWS() which will just return rows as a
single column.

================================================== ===========================
FAMILY DATA
----------------------
JACK,ANNIE,JOHN
PETER
================================================== ===========================
Thanks,
rAinDeEr

May 25 '06 #7
Hi, Ben

How about a recursive CTE?

CREATE TABLE Foo(
id INT NOT NULL,
descr VARCHAR(15) NOT NULL,
PRIMARY KEY(id, descr));

INSERT INTO Foo(id, descr)
VALUES(1, '1ST.1'),
(1, '1ST.2'),
(2, '2ND.1'),
(2, '2ND.2'),
(2, '2ND.3'),
(2, '2ND.4'),
(3, '3RD.1');

WITH F(id, cur_min_descr, max_descr, joined_descr) AS(
SELECT id, MIN(descr), MAX(descr), CAST(MIN(descr) AS VARCHAR(1600))
FROM Foo
GROUP BY id
UNION ALL
SELECT Foo.id, Foo.descr, F.max_descr, F.joined_descr || '||' ||
Foo.descr
FROM Foo, F
WHERE Foo.id = F.id
AND Foo.descr > F.cur_min_descr
AND NOT EXISTS(SELECT *
FROM Foo AS F2
WHERE F2.id = Foo.id
AND F2.descr > F.cur_min_descr
AND F2.descr < Foo.descr)
)
SELECT id, joined_descr
FROM F
WHERE cur_min_descr = max_descr;

DROP TABLE Foo;

---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)

May 26 '06 #8

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

Similar topics

2
by: phpuser32423 | last post by:
Hi everyone Is it by any chance possible to use mysql and php to auto create the content for drop-down lists on forms by retrieving the values from a database? The reason i ask is that on a site...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
45
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.