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

  • Home
  • SEARCH
  • 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 6871175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:46:24+00:00 2026-05-27T03:46:24+00:00

Possible Duplicate: How do I calculate a running total in SQL without using a

  • 0

Possible Duplicate:
How do I calculate a running total in SQL without using a cursor?

It’s a little difficult to explain, so I’ll show what I want with an example:

Lets say we have the following table named MonthProfit:

[MonthId][Profit]
1, 10 -- January
2, 20 -- February
3, 30
4, 40
5, 50
6, 60
7, 70
8, 80
9, 90
10, 100
11, 110
12, 120 -- December

Column profit represents the profit for that month.

However, if we have 10 profit in January, and 20 in February, in February we have a total profit of 30.

so I’d like to create a view that shows the following:

[MonthId][Profit][ProfitTotal]
1, 10, 10 -- January
2, 20, 30 -- February
3, 30, 60
4, 40, 100
5, 50, 150
6, 60, 210
7, 70, 280
8, 80, 360
9, 90, 450
10, 100, 550
11, 110, 660
12, 120, 780 -- December

What I did now to solve it, is a view like this:

SELECT [MonthId]
       ,[Profit]
       , (SELECT SUM([Profit])
         FROM MonthProfit
         WHERE [MonthId] <= outer.[MonthId]) as ProfitTotal
FROM MonthProfit as outer

However, I assume this is pretty slow because it has to recount everything all the time, and it does not seem very elegant to me. Is there a “good” way to do this?

  • 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-27T03:46:25+00:00Added an answer on May 27, 2026 at 3:46 am

    I have tried a small example here for your reference this generates the results as per the requirements

    CREATE TABLE [dbo].[tbl_TotalPrevious](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [values] [bigint] NOT NULL) 
    

    INSERT DATA INTO THE TABLE

    insert into tbl_TotalPrevious values ('A', 10)
    insert into tbl_TotalPrevious values ('B', 20)
    insert into tbl_TotalPrevious values ('C', 10)
    insert into tbl_TotalPrevious values ('D', 10)
    insert into tbl_TotalPrevious values ('E', 10)
    insert into tbl_TotalPrevious values ('F', 10)
    insert into tbl_TotalPrevious values ('G', 10)
    insert into tbl_TotalPrevious values ('H', 10)
    insert into tbl_TotalPrevious values ('I', 10)
    insert into tbl_TotalPrevious values ('J', 10)
    insert into tbl_TotalPrevious values ('K', 10)
    insert into tbl_TotalPrevious values ('L', 10)
    insert into tbl_TotalPrevious values ('M', 10)
    insert into tbl_TotalPrevious values ('N', 10)
    insert into tbl_TotalPrevious values ('O', 10)
    insert into tbl_TotalPrevious values ('P', 10)
    insert into tbl_TotalPrevious values ('Q', 10)
    insert into tbl_TotalPrevious values ('R', 10)
    insert into tbl_TotalPrevious values ('S', 10)
    insert into tbl_TotalPrevious values ('T', 10)
    insert into tbl_TotalPrevious values ('U', 10)
    insert into tbl_TotalPrevious values ('V', 10)
    insert into tbl_TotalPrevious values ('W', 10)
    insert into tbl_TotalPrevious values ('X', 10)
    insert into tbl_TotalPrevious values ('Y', 10)
    

    Create a Function eg.

    ALTER FUNCTION testtotal 
    (
        @id int
    )
    RETURNS int
    AS
    BEGIN
        DECLARE @Result int
        SELECT @Result = (SELECT SUM([values])
             FROM tbl_TotalPrevious
             WHERE [id] <= @id)
    
        RETURN @Result
    
    END
    GO
    

    RESULTS GENERATED FROM A SINGLE QUERY

    SELECT [id],[values], (dbo.testtotal(id)) as TotalVals FROM tbl_TotalPrevious 
    

    HOPE THE ABOVE SOLVES YOUR PURPOSE WITH THE TIMING ISSUE AND GENERATES THE DATA FASTER AS REQUIRED.

    RESULTS IMAGE

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

Sidebar

Related Questions

Possible Duplicate: Calculate a Running Total in SqlServer Consider this data Day | OrderCount
Possible Duplicate: How to calculate the difference between two dates using PHP? I have
Possible Duplicate: How do I calculate someone’s age in C#? I want to calculate
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
Possible Duplicate: How to calculate or approximate the median of a list without storing
Possible Duplicate: How to calculate the date difference between 2 dates using php Hi,,
Possible Duplicate: How do I calculate someone's age in Java? hi friends i want
Possible Duplicate: How to calculate the difference between two dates using PHP? How to
Possible Duplicate: How do I calculate relative time? I want to format dates on
Possible Duplicate: Running total by grouped records in table I am trying to put

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.