XSL: SUM BY GROUP

Discussion in 'XML' started by Rivky, May 17, 2005.

  1. Rivky

    Rivky Guest

    Hi. I have XML that is something like this?

    <ROW>
    <SYMBOL>A</SYMBOL>
    <AMOUNT>10</AMOUNT>
    </ROW>
    <ROW>
    <SYMBOL>A</SYMBOL>
    <AMOUNT>20</AMOUNT>
    </ROW>
    <ROW>
    <SYMBOL>B</SYMBOL>
    <AMOUNT>10</AMOUNT>
    </ROW>
    <ROW>
    <SYMBOL>C</SYMBOL>
    <AMOUNT>50</AMOUNT>

    </ROW>
    <ROW>
    <SYMBOL>C</SYMBOL>
    <AMOUNT>60</AMOUNT>
    </ROW>

    I would like to get the SUM of AMOUNT based on the Symbol. Is there any
    way to do this dynamically, using XSL/XSLT without having to specify
    SYMBOL = 'A'. Rather something like For each Symbol, sum the Amounts.

    Thanks in advance!
    Rivky, May 17, 2005
    #1
    1. Advertising

  2. Re: SUM BY GROUP

    First find the distinct values for SYMBOL, using a grouping method (such as
    the Muenchian method,
    see: http://www.jenitennison.com/xslt/grouping/)

    Then iterate over these distinct values and produce the following value:

    sum($yourDocument//ROW[SYMBOL = current()]/AMOUNT)

    Of course, you may be using keys, in which case you'll be evaluating
    something like this:

    sum(key('kSymByVal', $aDistinctValue)/../AMOUNT)


    Cheers,
    Dimitre Novatchev

    "Rivky" <> wrote in message
    news:...
    > Hi. I have XML that is something like this?
    >
    > <ROW>
    > <SYMBOL>A</SYMBOL>
    > <AMOUNT>10</AMOUNT>
    > </ROW>
    > <ROW>
    > <SYMBOL>A</SYMBOL>
    > <AMOUNT>20</AMOUNT>
    > </ROW>
    > <ROW>
    > <SYMBOL>B</SYMBOL>
    > <AMOUNT>10</AMOUNT>
    > </ROW>
    > <ROW>
    > <SYMBOL>C</SYMBOL>
    > <AMOUNT>50</AMOUNT>
    >
    > </ROW>
    > <ROW>
    > <SYMBOL>C</SYMBOL>
    > <AMOUNT>60</AMOUNT>
    > </ROW>
    >
    > I would like to get the SUM of AMOUNT based on the Symbol. Is there any
    > way to do this dynamically, using XSL/XSLT without having to specify
    > SYMBOL = 'A'. Rather something like For each Symbol, sum the Amounts.
    >
    > Thanks in advance!
    >
    Dimitre Novatchev, May 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ouioui
    Replies:
    1
    Views:
    1,625
    Mukul Gandhi
    Sep 23, 2004
  2. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  3. Rob Smegma
    Replies:
    1
    Views:
    1,808
    shaun
    Sep 26, 2005
  4. Akaketwa
    Replies:
    1
    Views:
    4,884
    impaler
    Sep 22, 2006
  5. Alex Sauceda

    SQL Query to Linq (Group By and Sum)

    Alex Sauceda, Feb 4, 2008, in forum: ASP .Net
    Replies:
    6
    Views:
    41,029
    Alex Sauceda
    Feb 4, 2008
Loading...

Share This Page