• schleep (3/19/2013)


    How about:

    IF OBJECT_ID('MyTable') IS NOT NULL

    DROP TABLE MyTable

    GO

    SELECT * INTO MyTable

    FROM (VALUES(1,19), (2,90), (3,20), (4,8), (5, 9)) AS M(ID, VALUE)

    GO

    SELECTm.Id, SUM(m2.Value)

    FROMMytable m

    JOINMytablem2ON m2.ID <= m.id

    GROUP BY m.Id

    That will definitely work... for a while. It's what's known as a "Triangular Join". Please see the following article as to why it shouldn't be used.

    http://www.sqlservercentral.com/articles/T-SQL/61539/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)