Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Location
    El Salvador
    Posts
    46

    Unanswered: accumulation column

    If my query returns the following result:

    NAME1 10
    NAME2 20
    NAME3 15
    NAME4 5
    NAME5 25

    is there any way (without using cursors) to add a column that adds the numbers so that I get the following result:

    NAME1 10 10
    NAME2 20 30
    NAME3 15 45
    NAME4 5 50
    NAME5 25 75

    ? thanks in advance

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, with a theta join
    Code:
    select t1.name
         , t1.num
         , sum(t2.num)  as accum
      from yourtable as t1
    inner
      join yourtable as t2
        on t1.name >= t2.name
    group
        by t1.name
         , t1.num
    order by t1.name
    notice the inequality in the join condition
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •