473,499 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query question...

hi, not sure if db2 has a aggregate function to 'sum (concat)' a string
column in a group by?
i have a table like this:
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string3'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string4'
.......

i want to group CatKye, SubCatKey, GrpKey, SubGrpKey
so, the result set to be like this (in sorted order):
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1,string4'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2,string3'

any idea? i heard there is something like ROWCONCAT... didn't find it
in db2 reference though.
thanks a lot.

Oct 7 '06 #1
8 1898
alanchinese wrote:
hi, not sure if db2 has a aggregate function to 'sum (concat)' a string
column in a group by?
i have a table like this:
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string3'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string4'
......

i want to group CatKye, SubCatKey, GrpKey, SubGrpKey
so, the result set to be like this (in sorted order):
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1,string4'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2,string3'

any idea? i heard there is something like ROWCONCAT... didn't find it
in db2 reference though.
thanks a lot.


CREATE TABLE Employee(name VARCHAR(15),
dept VARCHAR(15));

Name Dept
----- ------
Miso Solutions
John Development
Serge Solutions
Lee L3
Mark ID
Jack L3
Lily Quality
Berni Solutions

SELECT Dept,
SUBSTR(Names, 1, LENGTH(names) -1)
FROM (SELECT
Dept,
REPLACE
(REPLACE
(XMLSERIALIZE
(CONTENT XMLAGG(XMLELEMENT(NAME a, name)
ORDER BY name)
AS VARCHAR(60)), '<A>', ''), '</A>', ',') AS Names
FROM Employee GROUP BY Dept) AS X;

Dept Names
----- --------------------
Solutions Berni, Miso, Serge
Development John
L3 Jack, Lee
ID Mark
Quality Lily
IDUG Europe Presentation C04 "SQL on Fire!" last Tuesday
There will be a repeat at the IOD Conference a week from now.
Where are you located?

Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 7 '06 #2
wow, that sounds REALLY compliated.
is there easier solutions? but thanks for the direction...
i am in bay area SF.

Serge Rielau wrote:
alanchinese wrote:
hi, not sure if db2 has a aggregate function to 'sum (concat)' a string
column in a group by?
i have a table like this:
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string3'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string4'
......

i want to group CatKye, SubCatKey, GrpKey, SubGrpKey
so, the result set to be like this (in sorted order):
myCategory, mySubCategory, myGroup, mySubGroup, myString
CatKey1, SubCatKey1, GrpKey1, SubGrpKey1, 'string1,string4'
CatKey1, SubCatKey1, GrpKey1, SubGrpKey2, 'string2,string3'

any idea? i heard there is something like ROWCONCAT... didn't find it
in db2 reference though.
thanks a lot.

CREATE TABLE Employee(name VARCHAR(15),
dept VARCHAR(15));

Name Dept
----- ------
Miso Solutions
John Development
Serge Solutions
Lee L3
Mark ID
Jack L3
Lily Quality
Berni Solutions

SELECT Dept,
SUBSTR(Names, 1, LENGTH(names) -1)
FROM (SELECT
Dept,
REPLACE
(REPLACE
(XMLSERIALIZE
(CONTENT XMLAGG(XMLELEMENT(NAME a, name)
ORDER BY name)
AS VARCHAR(60)), '<A>', ''), '</A>', ',') AS Names
FROM Employee GROUP BY Dept) AS X;

Dept Names
----- --------------------
Solutions Berni, Miso, Serge
Development John
L3 Jack, Lee
ID Mark
Quality Lily
IDUG Europe Presentation C04 "SQL on Fire!" last Tuesday
There will be a repeat at the IOD Conference a week from now.
Where are you located?

Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 8 '06 #3
alanchinese wrote:
wow, that sounds REALLY compliated.
is there easier solutions? but thanks for the direction...
i am in bay area SF.
Then IOD is just a drive away. I've still got seats :-)
This is both the easiest and fastest way.
You could do recursion, but that's slower.
It's not complex, really.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 8 '06 #4
What you are looking for exists in Sybase as LIST() and one of the open
source products (Posttgres? I cannot remember). But that is not the
real question. Why do you wish to destroy First Normal Form (1NF) with
a concatendated list structure? It is the foundation of RDBMS, after
all.

Why are you formatting data in the back end? 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.

Yes, there are kludges in SQL to do this. You can also still write
procedural code with GOTOs and get "spaghetti code", but it does not
mean you should.

Oct 8 '06 #5
alanchinese wrote:
wow, that sounds REALLY compliated.
If you are familiar with recursive queries, you might find following
example easier to understand; but it's still quite a bit of code.
Essentially, it start with the "first" value and keeps appending "next"
value for remaining rows and you select the "last" value. (In the
following example first, next and last are arbitrary)

create table test(c1 varchar(128) not null);
insert into test values('One'), ('Two'), ('Three'), ('Four');

with t(r, c1) as
( select row_number() over() r
, c1
from test
)

, q(r, c1) as
( select r
, c1
from t
where r = 1
union all
select q.r + 1
, q.c1 || ',' || t.c1
from q q, t t
where t.r = q.r + 1
)

select c1
from q
where r = (select max(r) from q);

You can generalize above example to create aggregate/column function for
any scalar function or operator.

P Adhia
Oct 8 '06 #6
--CELKO-- wrote:
Yes, there are kludges in SQL to do this. You can also still write
procedural code with GOTOs and get "spaghetti code", but it does not
mean you should.
GOTO is actually quite useful _if used appropriately_. For example, it
gives you a nice way in C code to have a single place in a function that
does cleanup.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 8 '06 #7
>GOTO is actually quite useful _if used appropriately_. For example, it gives you a nice way in C code to have a single place in a function that does cleanup. <<

In a low level language like C you can get forced into a corner.
Modern, higher level languages use the TRY.. CATCH from OO languages or
the WHENEVER.. DO model from SQL/PSM and ADA for error handling and get
rid of the GOTO completely.

The best reasons for getting rid of the GOTO are correctness proofs
(Zohar Manna's book is back in print from Dover Publications) and
optimization. My favorite was BLISS, which had no GOTOs and could
consistently beat the best assembly programmers at CMU.

Oct 9 '06 #8
--CELKO-- wrote:
>>GOTO is actually quite useful _if used appropriately_. For example, it
gives you a nice way in C code to have a single place in a function that
does cleanup. <<

In a low level language like C you can get forced into a corner.
Modern, higher level languages use the TRY.. CATCH from OO languages or
the WHENEVER.. DO model from SQL/PSM and ADA for error handling and get
rid of the GOTO completely.

The best reasons for getting rid of the GOTO are correctness proofs
(Zohar Manna's book is back in print from Dover Publications) and
optimization. My favorite was BLISS, which had no GOTOs and could
consistently beat the best assembly programmers at CMU.
I think you missed my point completely. Most concepts were introduced for a
certain reason and many are still appropriate today in the right
environment - so is concatenation/denormalization of data in SQL
statements.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 10 '06 #9

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

Similar topics

9
3396
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in...
8
3222
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
14111
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
3
3055
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
7
3367
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
6
4812
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 ...
2
2026
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
22
31146
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
3
2053
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few...
16
3460
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
7169
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
7215
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...
1
6892
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
7385
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...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4597
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...
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.