Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

XQuery Question - Conditional sum()

 
   Database Help (Home) -> XML RSS
Next:  SQL 2005: "Function argument count error.&qu..  
Author Message
notlkk

External


Since: Dec 11, 2008
Posts: 2



(Msg. 1) Posted: Thu Dec 11, 2008 11:44 am
Post subject: XQuery Question - Conditional sum()
Archived from groups: microsoft>public>sqlserver>xml (more info?)

I have an XML doc like this:

<root>
<orderid>123-456</orderid>
<item>
<description>abc</description>
<itemid>999</itemid>
<itemprice>
<component>
<type>A</type>
<amount>1.00</amount>
</component>
<component>
<type>B</type>
<amount>2.00</amount>
</component>
</itemprice>
</item>
<item>
<description>def</description>
<itemid>888</itemid>
<itemprice>
<component>
<type>B</type>
<amount>3.00</amount>
</component>
<component>
<type>C</type>
<amount>4.00</amount>
</component>
</itemprice>
</item>
<item>
<description>ghi</description>
<itemid>777</itemid>
<itemprice>
<component>
<type>A</type>
<amount>5.00</amount>
</component>
<component>
<type>C</type>
<amount>6.00</amount>
</component>
</itemprice>
</item>
</root>

How do I return the sum on component price for type A in this order,
which in this example should be $6.00?

I try to use the following code but it just returns the sum of every
price, not limited to a certain type:

SELECT xmldata.query('
for $p in //root
where $p/orderid = "123-456"
and $p/item/itemprice/component/type = "A"
return $p/item/itemprice/cComponent/amount
')
FROM test

Also, how do I return it as a single row?

Thanks.

 >> Stay informed about: XQuery Question - Conditional sum() 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 2) Posted: Fri Dec 12, 2008 12:50 am
Post subject: RE: XQuery Question - Conditional sum() [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Try this:

DECLARE @xml XML

SET @xml = '<root>
<orderid>123-456</orderid>
<item>
<description>abc</description>
<itemid>999</itemid>
<itemprice>
<component>
<type>A</type>
<amount>1.00</amount>
</component>
<component>
<type>B</type>
<amount>2.00</amount>
</component>
</itemprice>
</item>
<item>
<description>def</description>
<itemid>888</itemid>
<itemprice>
<component>
<type>B</type>
<amount>3.00</amount>
</component>
<component>
<type>C</type>
<amount>4.00</amount>
</component>
</itemprice>
</item>
<item>
<description>ghi</description>
<itemid>777</itemid>
<itemprice>
<component>
<type>A</type>
<amount>5.00</amount>
</component>
<component>
<type>C</type>
<amount>6.00</amount>
</component>
</itemprice>
</item>
</root>'

SELECT @xml.query('sum(root/item/itemprice/component[type="A"]/amount)')


"notlkk" wrote:

> I have an XML doc like this:
>
> <root>
> <orderid>123-456</orderid>
> <item>
> <description>abc</description>
> <itemid>999</itemid>
> <itemprice>
> <component>
> <type>A</type>
> <amount>1.00</amount>
> </component>
> <component>
> <type>B</type>
> <amount>2.00</amount>
> </component>
> </itemprice>
> </item>
> <item>
> <description>def</description>
> <itemid>888</itemid>
> <itemprice>
> <component>
> <type>B</type>
> <amount>3.00</amount>
> </component>
> <component>
> <type>C</type>
> <amount>4.00</amount>
> </component>
> </itemprice>
> </item>
> <item>
> <description>ghi</description>
> <itemid>777</itemid>
> <itemprice>
> <component>
> <type>A</type>
> <amount>5.00</amount>
> </component>
> <component>
> <type>C</type>
> <amount>6.00</amount>
> </component>
> </itemprice>
> </item>
> </root>
>
> How do I return the sum on component price for type A in this order,
> which in this example should be $6.00?
>
> I try to use the following code but it just returns the sum of every
> price, not limited to a certain type:
>
> SELECT xmldata.query('
> for $p in //root
> where $p/orderid = "123-456"
> and $p/item/itemprice/component/type = "A"
> return $p/item/itemprice/cComponent/amount
> ')
> FROM test
>
> Also, how do I return it as a single row?
>
> Thanks.
>
>
>

 >> Stay informed about: XQuery Question - Conditional sum() 
Back to top
Login to vote
notlkk

External


Since: Dec 11, 2008
Posts: 2



(Msg. 3) Posted: Fri Dec 12, 2008 9:38 am
Post subject: Re: XQuery Question - Conditional sum() [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bob,

It works. Thank you very much for your help. Obviously, I was thinking
too much and not good enough at XQUERY yet.
 >> Stay informed about: XQuery Question - Conditional sum() 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
xquery question: Sample from MSDN2 not working, need advice. - From this page http://msdn2.microsoft.com/en-us/library/ms345117.aspx# The example for "Rich Language Constructs in XQuery" returns the follwoing error: "Msg 2209, Level 16, State 1, Line 2 XQuery [docs.xCol.query()]: Syntax error near '...

new to xQuery -- problem - Hello All, I am new to XQuery and also to SQL 2005. I am going through some tutorials on how to select xml nodes out of the database and i have run into a problem i can't figure out. So here is the Xml document that has been stored in the database ..

How to use a Variable in XQuery? - Hello, I have a XML as below: declare @doc2 xml SET @doc2 = '<?xml version="1.0" ?> <p1: Domains_and_Emails xmlns:p1="LDAP"> <Domain> <UnixEmail> <uid>info</uid> <Password>...

XQUERY big problem - Hello I try import data from XML file to MSSQL2005 using XQUERY xml like: <start> <section> <data1> </data1> <data2> </data2> <data3> </data3> <person> P1 </person> <pers...

Performance problems with XQuery - Hello All, I need some help improving the performance of queries that mix TSQL with XQuery. Some of our tables are defined like: create table A ( id uniqueidentifier primary key, Name nvarchar(260), Type nvarchar(260), Description nvarchar(2000),....
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]