Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 570075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:21:46+00:00 2026-05-13T13:21:46+00:00

Suppose I have a SQL table of Awards, with fields for Date and Amount.

  • 0

Suppose I have a SQL table of Awards, with fields for Date and Amount. I need to generate a table with a sequence of consecutive dates, the amount awarded in each day, and the running (cumulative) total.

Date         Amount_Total   Amount_RunningTotal
----------   ------------   -------------------
1/1/2010              100                   100
1/2/2010              300                   400
1/3/2010                0                   400
1/4/2010                0                   400
1/5/2010              400                   800
1/6/2010              100                   900
1/7/2010              500                  1400
1/8/2010              300                  1700

This SQL works, but isn’t as quick as I’d like:

Declare @StartDate datetime, @EndDate datetime 
Select @StartDate=Min(Date), @EndDate=Max(Date) from Awards 

; With 

/* Returns consecutive from numbers 1 through the 
number of days for which we have data */
Nbrs(n) as (
   Select 1 Union All 
   Select 1+n 
   From Nbrs 
   Where n<=DateDiff(d,@StartDate,@EndDate)),

/* Returns all dates @StartDate to @EndDate */
AllDays as (
   Select Date=DateAdd(d, n, @StartDate) 
   From Nbrs ) 

/* Returns totals for each day */
Select 
 d.Date,
 Amount_Total = (
        Select Sum(a.Amount) 
        From Awards a 
        Where a.Date=d.Date),
 Amount_RunningTotal = (
        Select Sum(a.Amount) 
        From Awards a 
        Where a.Date<=d.Date)
From AllDays d
Order by d.Date 
Option(MAXRECURSION 1000)

I tried adding an index to Awards.Date, but it made a very minimal difference.

Before I resort to other strategies like caching, is there a more efficient way to code the running total calculation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:21:46+00:00Added an answer on May 13, 2026 at 1:21 pm

    I generally use a temporary table for this:

    DECLARE @Temp TABLE
    (
        [Date] date PRIMARY KEY,
        Amount int NOT NULL,
        RunningTotal int NULL
    )
    
    INSERT @Temp ([Date], Amount)
        SELECT [Date], Amount
        FROM ...
    
    DECLARE @RunningTotal int
    
    UPDATE @Temp
    SET @RunningTotal = RunningTotal = @RunningTotal + Amount
    
    SELECT * FROM @Temp
    

    If you can’t make the date column a primary key then you need to include an ORDER BY [Date] in the INSERT statement.

    Also, this question’s been asked a few times before. See here or search for “sql running total”. The solution I posted is, as far as I know, still the one with the best performance, and also easy to write.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a SQL table called AT_Devices with each record representing a piece
I need help designing a SQL table. Suppose I have cardnumber , cardtype ,
Suppose you have a table in SQL: Prices ------ 13.99 14.00 52.00 52.00 52.00
Suppose I have a myTest.sql scripts, which contains thousands of create table blahblah statements.
I have a problem in SQL Server 2005: Suppose I have the table PLAYER
I have one query in SQL Server output, Suppose i have one table (Ex.StudentMaster)
Suppose I have an SQL table entitled Facility which looks like this: || FacilityID
Suppose I have a SQL function that returns a table and I get the
Suppose I have a SQL function that returns a table and I get the
Suppose I have a GAME table with the following fields user_id, result I wish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.