Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have tried a small example here for your reference this generates the results as per the requirements </p> <pre><code>CREATE TABLE [dbo].[tbl_TotalPrevious]( [id] [int] IDENTITY(1,1) NOT NULL, [name] [varchar](50) NOT NULL, [values] [bigint] NOT NULL) </code></pre> <p>INSERT DATA INTO THE TABLE </p> <pre><code>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) </code></pre> <p>Create a Function eg. </p> <pre><code>ALTER FUNCTION testtotal ( @id int ) RETURNS int AS BEGIN DECLARE @Result int SELECT @Result = (SELECT SUM([values]) FROM tbl_TotalPrevious WHERE [id] &lt;= @id) RETURN @Result END GO </code></pre> <p>RESULTS GENERATED FROM A SINGLE QUERY </p> <pre><code>SELECT [id],[values], (dbo.testtotal(id)) as TotalVals FROM tbl_TotalPrevious </code></pre> <p>HOPE THE ABOVE SOLVES YOUR PURPOSE WITH THE TIMING ISSUE AND GENERATES THE DATA FASTER AS REQUIRED. </p> <p><img src="https://i.stack.imgur.com/c9AHB.png" alt="RESULTS IMAGE"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload