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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:23:49+00:00 2026-05-25T10:23:49+00:00

I have the following table: CREATE TABLE tbl_proc( [proc] float, subscriber bigint ) data:

  • 0

I have the following table:

CREATE TABLE tbl_proc(
    [proc] float,
    subscriber bigint   
)

data:

proc | subscriber
-----|-----------
0.7  | 123456   
0.5  | 1234567  
0.3  | 12345    
0.3  | 45678    
0.3  | 1234 
0.2  | 123455   
0.1  | 894562   

I would like to find a nice method to add a new column to the table that represents the sum of the above values.

Result :

proc | subscriber | col3
-----|------------|------------
0.7  | 123456     | 0.7
0.5  | 1234567    | 1.2 -- 0.7 + proc
0.3  | 12345      | 1.5
...

I found the following method:

Select a.[proc],SUM(b.[proc])
from tbl_proc a, tbl_proc b
where a.[proc] <= b.[proc] and (a.[proc] <> b.[proc] or a.subscriber >= b.subscriber)
group by a.[proc],a.subscriber
order by a.[proc] desc

In my table the data is sorted desc by proc. Also the subscriber column is unique.

This method I found is a little bit too expensive ( my tables are large ).
Due to performance reasons I did not considered th cursor – like solution.

Any suggestions?


Update:

I googled the problem a little bit more and I found the “Update to a local variable” solution on this page:

http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005—the-optimal.aspx

As far as I tested this proves to be the best solution so far.

declare @runningTotal float = 0

UPDATE tbl_proc
SET @RunningTotal = new_col = @RunningTotal + [proc]
FROM tbl_proc

  • 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-25T10:23:50+00:00Added an answer on May 25, 2026 at 10:23 am

    This is generally known as calculating running totals.

    There is a very fast method to do what you want to do called “quirky update” but it relies on undocumented behaviour.

    Other than that cursors are the fastest way for large sets as the workload for these grows linearly whereas your triangular join workload grows exponentially (until next version and the improved OVER clause).

    See this document by Itzik Ben Gan for more about the issue.

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

Sidebar

Related Questions

I have the following table and data in SQL Server 2005: create table LogEntries
I have the following table CREATE TABLE `data` ( `acquire_time` decimal(26,6) NOT NULL, `sample_time`
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),

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.