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 8954075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:14:07+00:00 2026-06-15T14:14:07+00:00

I need a faster method to calculate and display a running sum. It’s an

  • 0

I need a faster method to calculate and display a running sum.

It’s an MVC telerik grid that queries a view that generates a running sum using a sub-query. The query takes 73 seconds to complete, which is unacceptable. (Every time the user hits “Refresh Forecast Sheet”, it takes 73 seconds to re-populate the grid.)

The query looks like this:

SELECT outside.EffectiveDate
[omitted for clarity]
       ,(
              SELECT SUM(b.Amount)
              FROM vCI_UNIONALL inside
              WHERE inside.EffectiveDate <= outside.EffectiveDate
              ) AS RunningBalance
[omitted for clarity]
FROM vCI_UNIONALL outside

“EffectiveDate” on certain items can change all the time… New items can get added, etc. I certainly need something that can calculate the running sum on the fly (when the Refresh button is hit). Stored proc or another View…? Please advise.

Solution: (one of many, this one is orders of magnitude faster than a sub-query)

Create a new table with all the columns in the view except for the RunningTotal col. Create a stored procedure that first truncates the table, then INSERT INTO the table using SELECT all columns, without the running sum column.

Use update local variable method:

DECLARE @Amount DECIMAL(18,4)
SET @Amount = 0
UPDATE TABLE_YOU_JUST_CREATED SET RunningTotal = @Amount, @Amount = @Amount + ISNULL(Amount,0)

Create a task agent that will run the stored procedure once a day. Use the TABLE_YOU_JUST_CREATED for all your reports.

  • 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-06-15T14:14:08+00:00Added an answer on June 15, 2026 at 2:14 pm

    Take a look at this post
    Calculate a Running Total in SQL Server

    If you have SQL Server Denali, you can use new windowed function.

    In SQL Server 2008 R2 I suggest you to use recursive common table expression.
    Small problem in CTE is that for fast query you have to have identity column without gaps (1, 2, 3,…) and if you don’t have such a column you have to create a temporary or variable table with such a column and to move you your data there.

    CTE approach will be something like this

    declare @Temp_Numbers (RowNum int, Amount <your type>, EffectiveDate datetime)
    
    insert into @Temp_Numbers (RowNum, Amount, EffectiveDate)
    select row_number() over (order by EffectiveDate), Amount, EffectiveDate
    from vCI_UNIONALL
    
    -- you can also use identity
    -- declare @Temp_Numbers (RowNum int identity(1, 1), Amount <your type>, EffectiveDate datetime)
    -- insert into @Temp_Numbers (Amount, EffectiveDate)
    -- select Amount, EffectiveDate
    -- from vCI_UNIONALL
    -- order by EffectiveDate
    
    ;with 
    CTE_RunningTotal
    as
    (
        select T.RowNum, T.EffectiveDate, T.Amount as Total_Amount
        from @Temp_Numbers as T
        where T.RowNum = 1
        union all
        select T.RowNum, T.EffectiveDate, T.Amount + C.Total_Amount as Total_Amount
        from CTE_RunningTotal as C
            inner join @Temp_Numbers as T on T.RowNum = C.RowNum + 1
    )
    select C.RowNum, C.EffectiveDate, C.Total_Amount
    from CTE_RunningTotal as C
    option (maxrecursion 0)
    

    There’re may be some questions with duplicates EffectiveDate values, it depends on how you want to work with them – do you want to them to be ordered arbitrarily or do you want them to have equal Amount?

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

Sidebar

Related Questions

I need to calculate sum of occurences of some data in two columns in
I need help optimizing the code to run faster, unless it is optimized the
I need to determine the quadrant of point, in a faster way. I am
Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
Need help with a query that I wrote: I have three tables Company id
I need to calculate the directory size in VB .Net I know the following
I have a system of 6 equations that I need to solve over and
I need to display a date and, if it's not representing midnight, the time
I am writing a private utility method in a Spring Controller. I need to
I wrote a method that gathers data from an Oracle server, formats and encrypts

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.