Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2003
    Posts
    2

    Unanswered: get intermediate rusult of aggregate function SUM

    I have the following table:

    Table1
    ObjID Amount
    -------- ----------
    1 100
    1 200
    1 150
    1 50
    1 300

    If I execute the statement:
    SELECT ObjID,Sum(Amount) FROM Table1
    GROUP BY ObjID, the I receive 800

    I need the result like this:
    ObjID Amount
    ------- ----------
    1 100
    1 300
    1 450
    1 500
    1 800

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you need an additional column which you can use in the ORDER BY

    then write transact-sql with a cursor


    rudy

  3. #3
    Join Date
    Apr 2003
    Posts
    2
    Is the solution without cursor?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, there is a solution without a cursor, but it involves a self join, where each row is matched to all the rows with a lower sort value

    the database will have n**2/2 records to match and sums to calculate

    you still need that other column to sort on, too

    rudy

Posting Permissions

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