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

XQuery sum() function

 
   Database Help (Home) -> XML RSS
Next:  Missing files  
Author Message
Loren Dummer

External


Since: Dec 02, 2008
Posts: 1



(Msg. 1) Posted: Tue Dec 02, 2008 8:27 pm
Post subject: XQuery sum() function
Archived from groups: microsoft>public>sqlserver>xml (more info?)

I am having problems getting a sum from xml.

The following works and returns - 1150.75

DECLARE @Xml xml

SELECT @Xml = '<Charges>
<Charge>
<Amount>100.00</Amount>
</Charge>
<Charge>
<Amount>50.50</Amount>
</Charge>
<Charge>
<Amount>1000.25</Amount>
</Charge>
</Charges>'

SELECT @Xml.query('
sum( /Charges/Charge/Amount )
')



Now take the amount 1000.25 and change it to 1,000.25. It will return
150.5.

DECLARE @Xml xml

SELECT @Xml = '<Charges>
<Charge>
<Amount>100.00</Amount>
</Charge>
<Charge>
<Amount>50.50</Amount>
</Charge>
<Charge>
<Amount>1,000.25</Amount>
</Charge>
</Charges>'

SELECT @Xml.query('
sum( /Charges/Charge/Amount )
')


How can I get Sql Server to give me the correct sum of 1150.75 when some
values have commas in the values?

Thank-you in advance.

 >> Stay informed about: XQuery sum() function 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 2) Posted: Wed Dec 03, 2008 3:40 am
Post subject: RE: XQuery sum() function [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Workaround:

DECLARE @xml XML
SELECT @xml = '<Charges>
<Charge>
<Amount>100.00</Amount>
</Charge>
<Charge>
<Amount>50.50</Amount>
</Charge>
<Charge>
<Amount>1,000.25</Amount>
</Charge>
</Charges>'

SELECT SUM( x.y.value('.', 'MONEY') )
FROM @xml.nodes( '/Charges/Charge/Amount' ) x(y)

Don't think you can you sum directly as the amount doesn't meet the rules
for an xs:decimal.

HTH
wBob
"Loren Dummer" wrote:

> I am having problems getting a sum from xml.
>
> The following works and returns - 1150.75
>
> DECLARE @Xml xml
>
> SELECT @Xml = '<Charges>
> <Charge>
> <Amount>100.00</Amount>
> </Charge>
> <Charge>
> <Amount>50.50</Amount>
> </Charge>
> <Charge>
> <Amount>1000.25</Amount>
> </Charge>
> </Charges>'
>
> SELECT @Xml.query('
> sum( /Charges/Charge/Amount )
> ')
>
>
>
> Now take the amount 1000.25 and change it to 1,000.25. It will return
> 150.5.
>
> DECLARE @Xml xml
>
> SELECT @Xml = '<Charges>
> <Charge>
> <Amount>100.00</Amount>
> </Charge>
> <Charge>
> <Amount>50.50</Amount>
> </Charge>
> <Charge>
> <Amount>1,000.25</Amount>
> </Charge>
> </Charges>'
>
> SELECT @Xml.query('
> sum( /Charges/Charge/Amount )
> ')
>
>
> How can I get Sql Server to give me the correct sum of 1150.75 when some
> values have commas in the values?
>
> Thank-you in advance.
>
>
>

 >> Stay informed about: XQuery sum() function 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
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),....

Faster search with xquery contains() - Hi In our work we have a table with 37000 records (aprox). The table have a xml column, with primary xml index and a secondary path index. The xml structure is something like this: <Usuario> <mbUSR_Usuario> ..
   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 ]