Cummulative or running total in SQL query

Posted on 2009-02-16
Last Modified: 2012-05-06
I have a table that I need to calculate the running total on, and I'm not sure how to work it out in the colum called CumulDaySales - that column need the running daily total from the Sales column.  Does anyone know how to calculate it?
Seq	Date	       DayTarget	CumulDayTgt     Sales         CumulDaysales

1	2009-02-01 	27	27	       15	            ?

2	2009-02-02 	27	54	       25	            ?

3	2009-02-03 	27	81	       21	            ?

4	2009-02-04 	27	108	       20	            ?

5	2009-02-05 	27	135	       17	            ?

6	2009-02-06 	27	162	       10	            ?


Open in new window

Question by:Lapchien
    1 Comment
    LVL 25

    Accepted Solution

    SELECT T1.Seq, T1.Date, T1.DayTarget, T1.CumulDayTgt, T1.Sales, CumulDaySales = (SELECT sum(T2.Sales) FROM <Table> T2 WHERE T2.Seq <= T1.Seq)
    FROM <Table> T1

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now