Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Cummulative or running total in SQL query

Posted on 2009-02-16
Medium Priority
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

reb73 earned 2000 total points
ID: 23648651
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

569 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