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

select distinct() with XML query functions.

Hi all.
I'm really stuck with getting the right output from a XML query.

Given this structure:

create table GENCMP.SCRIPTS
(
SCRIPT_ID CHAR(10) not null,
PAGE_NO INTEGER not null
);

CREATE unique INDEX GENCMP.SCRIPTS_0 on GENCMP.SCRIPTS
(SCRIPT_ID, PAGE_NO) pctfree 2 allow reverse scans ;
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('CUSTCARE',
1);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('CUSTCARE',
2);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('LEAD', 1);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('LEAD', 2);
What query do I use to get the following output:

<SCRIPTS>
<SCRIPT>CUSTCARE</SCRIPT>
<SCRIPT>LEAD</SCRIPT>
</SCRIPTS>
I just can not get it happening.

I would appreciate any support/help.

-Chris

Feb 15 '07 #1
3 12815
ch****@warpspeed.com.au wrote:
Hi all.
I'm really stuck with getting the right output from a XML query.

Given this structure:

create table GENCMP.SCRIPTS
(
SCRIPT_ID CHAR(10) not null,
PAGE_NO INTEGER not null
);

CREATE unique INDEX GENCMP.SCRIPTS_0 on GENCMP.SCRIPTS
(SCRIPT_ID, PAGE_NO) pctfree 2 allow reverse scans ;
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('CUSTCARE',
1);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('CUSTCARE',
2);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('LEAD', 1);
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO) values ('LEAD', 2);
What query do I use to get the following output:

<SCRIPTS>
<SCRIPT>CUSTCARE</SCRIPT>
<SCRIPT>LEAD</SCRIPT>
</SCRIPTS>
How about this:

SELECT XMLSERIALIZE(XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT(NAME "SCRIPT", script_id)) AS VARCHAR(2000))
FROM ( SELECT DISTINCT script_id
FROM scripts ) AS t
That's on DB2 V9.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Feb 15 '07 #2
On Feb 15, 5:44 pm, Knut Stolze <sto...@de.ibm.comwrote:
>
How about this:

SELECT XMLSERIALIZE(XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT(NAME "SCRIPT", script_id)) AS VARCHAR(2000))
FROM ( SELECT DISTINCT script_id
FROM scripts ) AS t

That's on DB2 V9.
Thank you so much! I could not work out where the distinct would fit
in. Brain fade me.

This is what I got working under V8:

SELECT XMLSERIALIZE(CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLAGG(
XMLELEMENT( NAME "SCRIPT", SCRIPT_ID ) ) ) AS
VARCHAR(80) ) AS RESULT
FROM ( select distinct script_id from GENCMP.SCRIPTS ) as T;

RESULT
--------------------------------------------------------------------------------
<SCRIPTS><SCRIPT>CUSTCARE </SCRIPT><SCRIPT>LEAD </SCRIPT></
SCRIPTS>

1 record(s) selected.

:-))))))))))))))))

Danke.

-Chris

Feb 15 '07 #3
ch****@warpspeed.com.au wrote:
On Feb 15, 5:44 pm, Knut Stolze <sto...@de.ibm.comwrote:
>>
How about this:

SELECT XMLSERIALIZE(XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT(NAME "SCRIPT", script_id)) AS VARCHAR(2000))
FROM ( SELECT DISTINCT script_id
FROM scripts ) AS t

That's on DB2 V9.

Thank you so much! I could not work out where the distinct would fit
in. Brain fade me.

This is what I got working under V8:

SELECT XMLSERIALIZE(CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLAGG(
XMLELEMENT( NAME "SCRIPT", SCRIPT_ID ) ) ) AS
VARCHAR(80) ) AS RESULT
FROM ( select distinct script_id from GENCMP.SCRIPTS ) as T;

RESULT
--------------------------------------------------------------------------------
<SCRIPTS><SCRIPT>CUSTCARE </SCRIPT><SCRIPT>LEAD </SCRIPT></
SCRIPTS>

1 record(s) selected.

:-))))))))))))))))
Right. I didn't try this myself, so I missed the syntax error (missing
CONTENT keyword) and the XMLAGG function. Thanks for correcting that.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Feb 16 '07 #4

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

Similar topics

2
by: dr. zoidberg | last post by:
Hello, I'm trying to select all distinct years from a unixtimestamp field in MySQL database. I have a query: SELECT DISTINCT YEAR(date_field) As theYear FROM table but it gives me an empty...
1
by: Perre Van Wilrijk | last post by:
Hi, I have a table as following aa Text1 aa, p@xxx.be, 15267 aa Text1 aa, p@xxx.be, 16598 aa Text1 aa, p@xxx.be, 17568 aa Text2 aa, p@xxx.be, 25698 aa Text3 aa, x@zzz.be, 12258
9
by: Kelvin | last post by:
Okay so this is baking my noodle. I want to select all the attritbutes/fields from a table but then to excluded any row in which a single attributes data has been duplicated. I.E. Here's my...
6
by: John M | last post by:
Hi, The line below is used to feed a combobox. (It is from a database which is used to log pupil behaviour!) The 'incidents' table contains a list of students who have been involved in incidents....
18
by: mathilda | last post by:
My boss has been adamant that SELECT DISTINCT is a faster query than SELECT all other factors being equal. I disagree. We are linking an Access front end to a SQL Server back end and normally are...
3
by: orekinbck | last post by:
Hi There Our test database has duplicate data: COMPANYID COMPANYNAME 1 Grupple Group 2 Grupple Group 5 Grupple Group 3 Yada Inc 4 Yada...
10
by: Farmer | last post by:
Hello Folks, can anybody tell me how to do the following very simple thing: I have a table that has say departments, divisions and units of an organisation and what i am trying to do is find a...
3
lwwhite
by: lwwhite | last post by:
I've got a a datasheet subform that gets its data from a SELECT DISTINCT query. The data is not editable. When I remove the DISTINCT qualifier from the query, the data is editable, but of course I...
8
by: cmrhema | last post by:
Hello I am preparing an application where I have different data stored in an array eg. Dim Vehicle(100) as String In this array different values are stored in an ordered manner. eg....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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...
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
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
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...

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.