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

Need help with an XML querry

Hi All,
I've been struggeling with this for a while now, and I was
wondering if anyone could help me.

given:
drop table GENCMP.SCRIPTS;

--================================================== ============
-- Table: SCRIPTS
--================================================== ============
create table GENCMP.SCRIPTS
(
SCRIPT_ID CHAR(10) not null,
PAGE_NO INTEGER not null,
EFFECTIVE_FROM DATE not null,
EFFECTIVE_TO DATE not null,
USERID CHAR(8) not null,
PUBLISHED CHAR(1) not null,
SCRIPT_TEXT CLOB(32K) LOGGED NOT COMPACT NOT
NULL
);

CREATE unique INDEX GENCMP.SCRIPTS_0 on GENCMP.SCRIPTS
(SCRIPT_ID, PAGE_NO, EFFECTIVE_TO) pctfree 2 allow
reverse scans ;

insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO, EFFECTIVE_FROM,
EFFECTIVE_TO, USERID, PUBLISHED, SCRIPT_TEXT) values ('CUSTCARE', 1,
'2006-01-01', '9999-12-31', 'APCPG', 'N', 'asd1');
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO, EFFECTIVE_FROM,
EFFECTIVE_TO, USERID, PUBLISHED, SCRIPT_TEXT) values ('CUSTCARE', 2,
'2006-01-01', '9999-12-31', 'APCPG', 'N', 'asd2');
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO, EFFECTIVE_FROM,
EFFECTIVE_TO, USERID, PUBLISHED, SCRIPT_TEXT) values ('CUSTCARE', 3,
'2006-01-01', '9999-12-31', 'APCPG', 'N', 'asd3');
insert into GENCMP.SCRIPTS (SCRIPT_ID, PAGE_NO, EFFECTIVE_FROM,
EFFECTIVE_TO, USERID, PUBLISHED, SCRIPT_TEXT) values ('CUSTCARE', 4,
'2006-01-01', '9999-12-31', 'APCPG', 'N', 'asd4');

select SCRIPT_ID, PAGE_NO, EFFECTIVE_FROM, EFFECTIVE_TO, USERID,
PUBLISHED from GENCMP.SCRIPTS;

You should see:

SCRIPT_ID PAGE_NO EFFECTIVE_FROM EFFECTIVE_TO USERID PUBLISHED
---------- ----------- -------------- ------------ -------- ---------
CUSTCARE 1 01-01-2006 31-12-9999 APCPG N
CUSTCARE 2 01-01-2006 31-12-9999 APCPG N
CUSTCARE 3 01-01-2006 31-12-9999 APCPG N
CUSTCARE 4 01-01-2006 31-12-9999 APCPG N
The query that I have is this:

select XMLSERIALIZE(CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLAGG(

XMLELEMENT(NAME "SCRIPT",

XMLATTRIBUTES(SCRIPT_ID),

XMLELEMENT(NAME "script_text",

XMLATTRIBUTES(PAGE_NO),

SCRIPT_TEXT)

)
)
) AS CLOB
) from gencmp.scripts;
and it generates the following XML:

<SCRIPTS>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="1">asd1</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="2">asd2</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="3">asd3</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="4">asd4</script_text>
</SCRIPT>
</SCRIPTS>
However, what I need is:

<SCRIPTS>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="1">asd1</script_text>
<script_text PAGE_NO="2">asd2</script_text>
<script_text PAGE_NO="3">asd3</script_text>
<script_text PAGE_NO="4">asd4</script_text>
</SCRIPT>
</SCRIPTS>
Can anyone help me massage the query into giving me what I need?

TIA,

-Chris

Nov 22 '06 #1
3 2184
ch****@warpspeed.com.au wrote:
SCRIPT_ID PAGE_NO EFFECTIVE_FROM EFFECTIVE_TO USERID PUBLISHED
---------- ----------- -------------- ------------ -------- ---------
CUSTCARE 1 01-01-2006 31-12-9999 APCPG N
CUSTCARE 2 01-01-2006 31-12-9999 APCPG N
CUSTCARE 3 01-01-2006 31-12-9999 APCPG N
CUSTCARE 4 01-01-2006 31-12-9999 APCPG N
The query that I have is this:

select XMLSERIALIZE(CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLAGG(

XMLELEMENT(NAME "SCRIPT",

XMLATTRIBUTES(SCRIPT_ID),

XMLELEMENT(NAME "script_text",

XMLATTRIBUTES(PAGE_NO),

SCRIPT_TEXT)

)
)
) AS CLOB
) from gencmp.scripts;
and it generates the following XML:

<SCRIPTS>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="1">asd1</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="2">asd2</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="3">asd3</script_text>
</SCRIPT>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="4">asd4</script_text>
</SCRIPT>
</SCRIPTS>
However, what I need is:

<SCRIPTS>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="1">asd1</script_text>
<script_text PAGE_NO="2">asd2</script_text>
<script_text PAGE_NO="3">asd3</script_text>
<script_text PAGE_NO="4">asd4</script_text>
</SCRIPT>
</SCRIPTS>
If I got this right, then you don't want to have the nested "<SCRIPT>" tags,
right? Therefore, you will have to put the XMLAGG in the right place like
this:

SELECT XMLSERIALIZE(
CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT(NAME "SCRIPT",
XMLATTRIBUTES(script_id),
XMLAGG(
XMLELEMENT(NAME "script_text",
XMLATTRIBUTES(page_no),
script_text))
ORDER BY page_no))
FROM ...
GROUP BY script_id

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 22 '06 #2
Knut Stolze wrote:
If I got this right, then you don't want to have the nested "<SCRIPT>" tags,
right? Therefore, you will have to put the XMLAGG in the right place like
this:

SELECT XMLSERIALIZE(
CONTENT XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT(NAME "SCRIPT",
XMLATTRIBUTES(script_id),
XMLAGG(
XMLELEMENT(NAME "script_text",
XMLATTRIBUTES(page_no),
script_text))
ORDER BY page_no))
FROM ...
GROUP BY script_id
That is basically right. Given that you can not have multiple nested
XMLAGG()'s (is this correct?), I was wondering if I had it in the right
place.

This is the finished, working query:v

SELECT XMLSERIALIZE(CONTENT
XMLELEMENT(NAME "SCRIPTS",
XMLELEMENT( NAME "SCRIPT",
XMLATTRIBUTES(script_id),
XMLAGG(
XMLELEMENT(NAME "script_text",
XMLATTRIBUTES(page_no), script_text)
ORDER BY page_no
) ) ) AS CLOB
)
FROM GENCMP.SCRIPTS
GROUP BY script_id;

Thank you very much for pointing me in the right direction! :-)

It generates this:

<SCRIPTS>
<SCRIPT SCRIPT_ID="CUSTCARE ">
<script_text PAGE_NO="1">asd1</script_text>
<script_text PAGE_NO="2">asd2</script_text>
<script_text PAGE_NO="3">asd3</script_text>
<script_text PAGE_NO="4">asd4</script_text>
</SCRIPT>
</SCRIPTS>

Which is precisely what I wanted.

Thanks again.

-Chris

Nov 22 '06 #3
ch****@warpspeed.com.au wrote:
Given that you can not have multiple nested
XMLAGG()'s (is this correct?),
No, that is not correct (unless there are some constraints I don't know
about). Of course, a single subselect can only contain one level of
aggregation. Nested aggregations would immediately raise the question how
to group things on all levels. However, you can use nested subselects and
each subselect can do its own aggregation. That will give you the ability
to aggregate more:

SELECT XMLAGGR(xml_fragment), ...
FROM ( SELECT XMLAGGR(xml_column), ...
FROM ...
GROUP BY ... ) AS t(xml_fragment, ...)
GROUP BY ...

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 23 '06 #4

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

Similar topics

2
by: Eric Kincl | last post by:
Hello, I have an array of data in PHP. I would like to insert each member of the array into it's own row in SQL. The array is of variable length, so it would have to be dynamic code. How would...
25
by: Andreas Fromm | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Im building an user database with many tables keeping the data for the Address, Phone numbers, etc which are referenced by a table where I...
1
by: Jamie Pittman via AccessMonster.com | last post by:
Does anyone have any suggestion how to create this form. I want to be able to have form that has two fields and that allow me to select specific parmeters for my Union Query to Look up? I have a...
2
by: gurpreet | last post by:
Hi this is gurpreet, I know this is a very simple question but still I want to clear some doubts. What happens when we compile and link a c-program? I hope aquite a lot of responses to my...
5
by: Clownfish | last post by:
OK, I'm having a brain freeze. I have a table like this: Office Name Phone ---------------------------------- SG Larry 555-1212 SG Moe 553-4444 SG Curly ...
3
by: jamie | last post by:
Hello, I have a SQL question with regards to retrieving data from an Access DB. When I use this querry in Access, it works fine, but when I try to get data, from Access, using the querry it...
1
Steve Kiss
by: Steve Kiss | last post by:
Hi. I am developping a site for which one of the pages uses querry strings to pass some parameters. I can use the querry strings if I call the page from a plain html anchor. However, when I add the...
1
by: saddist | last post by:
Hello, TRAININGS(ID_TRAINING, TRAINING_NAME) HOSTED_TRAININGS(ID_HOSTED_TRAINING, ID_TRAINING, DAYS) EMPLOYEES_ON_TRAININGS(ID_HOSTED_TRAINING) What I want to do is a querry with sum of days...
2
by: dipalichavan82 | last post by:
i came across a article, where it was mentioned if we want a dynamic querry to fire then use parameterized querry e.g. string inputcity=textbox.text; SqlCommand cmd = new SqlCommand("select * from...
2
by: Question123 | last post by:
Hi i have one database table Table1.which contains almost 20000000 recordes. record to this table are inserted through storedprocedure. storedprocedure takes parameter as "value", Beginningdate,...
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
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?
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
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
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.