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

Aggregating strings

Hi folks,

One the more frequently asked questions is how to "sum" up strings by
virtue of concatenating them in an aggregate function.
Her eis a rather simpel solution that may be worth sharing (you'll be
teh judge):
DROP TABLE T;
CREATE TABLE T(servername VARCHAR(15), volume VARCHAR(10), capacity
VARCHAR(10));
INSERT INTO T VALUES
('ServerA', 'Vol1', '5GB'),
('ServerB', 'Vol1', '5GB'),
('ServerC', 'Vol2', '11GB'),
('ServerD', 'Vol2', '11GB'),
('ServerE', 'Vol3', '20GB');

SELECT Volume, Capacity,
VARCHAR(REPLACE(REPLACE(VARCHAR(XML2CLOB(XMLAGG(XM LELEMENT(NAME
a, ServerName)
ORDER BY ServerName)), 60),
'<A>', ''),
'</A>', ','), 60) AS ServerList
FROM T GROUP BY Volume, Capacity;

VOLUME CAPACITY SERVERLIST
---------- ---------- ------------------
Vol1 5GB ServerA,ServerB,
Vol2 11GB ServerC,ServerD,
Vol3 20GB ServerE,

3 record(s) selected.
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 24 '06 #1
3 3018
Serge Rielau wrote:
Hi folks,

One the more frequently asked questions is how to "sum" up strings by
virtue of concatenating them in an aggregate function.
Her eis a rather simpel solution that may be worth sharing (you'll be
teh judge):
DROP TABLE T;
CREATE TABLE T(servername VARCHAR(15), volume VARCHAR(10), capacity
VARCHAR(10));
INSERT INTO T VALUES
('ServerA', 'Vol1', '5GB'),
('ServerB', 'Vol1', '5GB'),
('ServerC', 'Vol2', '11GB'),
('ServerD', 'Vol2', '11GB'),
('ServerE', 'Vol3', '20GB');

SELECT Volume, Capacity,
VARCHAR(REPLACE(REPLACE(VARCHAR(XML2CLOB(XMLAGG(XM LELEMENT(NAME
a, ServerName)
ORDER BY ServerName)), 60),
'<A>', ''),
'</A>', ','), 60) AS ServerList
FROM T GROUP BY Volume, Capacity;

VOLUME CAPACITY SERVERLIST
---------- ---------- ------------------
Vol1 5GB ServerA,ServerB,
Vol2 11GB ServerC,ServerD,
Vol3 20GB ServerE,

3 record(s) selected.


Thanks a lot for a solution to a problem that has been worrying us for a
while.

What level of DB2 did the XML functions appear?

Can this be wrapped in a function with the argument 'ServerName' (I
don't see it but you are more expert here)?

Is there any easy way to remove the trailing comma? This would be easy
if SUBSTR understood negative start and length parameters like PHP.
(Hint, hint)

The following formatting may be a little easier to comprehend (if I've
understood it myself):

SELECT Volume, Capacity,
VARCHAR(
REPLACE(
REPLACE(
VARCHAR(
XML2CLOB(
XMLAGG(
XMLELEMENT(NAME a, ServerName)
ORDER BY ServerName)
),
60),
'<A>', ''),
'</A>', ','),
60) AS ServerList
Feb 24 '06 #2
DB2 V8.1 GA for LUW I think (at least there is no change marker on the
docs):
http://publib.boulder.ibm.com/infoce...n/r0000736.htm

You can't wrap aggrehgates into functions. They need the context of
their group. (Same for MAX, MIN, etc...)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Feb 24 '06 #3
Wow, that's another good one. I'm not even sure i knew of the XML
FUNCTIONs. I do find it odd that it's listed under Expressions though.

Just last week i was working on something that could use this, and
ended up implementing a FOR LOOP. This may replace that on the next
iteration.

B.

Feb 27 '06 #4

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
5
by: James Foreman | last post by:
I've got a whole load of data that I've been logging to record different search terms used on a website. This all resides in a varchar(50) column. Contents will be something like: toaster...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
3
by: Piotr Sobolewski | last post by:
Hello, are there any aggregating functions in Access that can concatenate (glue together) text?
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
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...

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.