Summing upto a specific value

  • Hi,

    I have a table with columns accountid,date, quantity and price. For generation of a report in need to sum the quantity. But i need to stop summing the quantity once the quantity =2000000 and at that date i need to find (quantity* price) for each id.

    Please help me in writing a query.

    Thanks

  • Use your normal sum, but add a having(sum()) >= xxx and order by date.

  • Thanks for your response.

    I have already tried that option.

    But what i actually required is that, each day there are number of entries for quantity.

    Quantity mean "shares" a accountid makes. So i have to find (quantity* price) for each id, whose id have a profitablility of $100 shares during their first 2,000,000 shares of their trading.

  • I could be wrong, but this sounds like a "running total" problem with an additional SELECT to find the 2 million mark... please see the following...

    http://www.sqlservercentral.com/articles/Advanced+Querying/61716/

    --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)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply