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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:52:31+00:00 2026-05-15T19:52:31+00:00

I am trying to keep a rolling checksum to account for order, so take

  • 0

I am trying to keep a rolling checksum to account for order, so take the previous ‘checksum’ and xor it with the current one and generate a new checksum.

Name      Checksum     Rolling Checksum
------    -----------  -----------------
foo       11829231     11829231
bar       27380135     checksum(27380135 ^ 11829231) = 93291803
baz       96326587     checksum(96326587 ^ 93291803) = 67361090

How would I accomplish something like this?

(Note that the calculations are completely made up and are for illustration only)

  • 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-15T19:52:32+00:00Added an answer on May 15, 2026 at 7:52 pm

    This is basically the running total problem.

    Edit:

    My original claim was that is one of the few places where a cursor based solution actually performs best. The problem with the triangular self join solution is that it will repeatedly end up recalculating the same cumulative checksum as a subcalculation for the next step so is not very scalable as the work required grows exponentially with the number of rows.

    Corina’s answer uses the “quirky update” approach. I’ve adjusted it to do the check sum and in my test found that it took 3 seconds rather than 26 seconds for the cursor solution. Both produced the same results. Unfortunately however it relies on an undocumented aspect of Update behaviour. I would definitely read the discussion here before deciding whether to rely on this in production code.

    There is a third possibility described here (using the CLR) which I didn’t have time to test. But from the discussion here it seems to be a good possibility for calculating running total type things at display time but out performed by the cursor when the result of the calculation must be saved back.

    CREATE TABLE TestTable
    (
    PK int identity(1,1) primary key clustered,
    [Name] varchar(50),
    [CheckSum] AS CHECKSUM([Name]),
    RollingCheckSum1 int NULL,
    RollingCheckSum2 int NULL
    )
    
    
    /*Insert some random records (753,571 on my machine)*/
    INSERT INTO TestTable ([Name])
    SELECT newid() FROM sys.objects s1, sys.objects s2, sys.objects s3
    

    Approach One: Based on the Jeff Moden Article

    DECLARE @RCS int
    
     UPDATE TestTable
        SET @RCS = RollingCheckSum1 = 
                                     CASE WHEN @RCS IS NULL THEN 
                                                            [CheckSum] 
                                     ELSE 
                                                 CHECKSUM([CheckSum] ^ @RCS) 
                                     END
       FROM TestTable WITH (TABLOCKX)
     OPTION (MAXDOP 1)
    

    Approach Two – Using the same cursor options as Hugo Kornelis advocates in the discussion for that article.

    SET NOCOUNT ON
    BEGIN TRAN
    
    DECLARE @RCS2 INT
    DECLARE @PK INT, @CheckSum INT
    
    DECLARE curRollingCheckSum CURSOR LOCAL STATIC READ_ONLY
        FOR
        SELECT PK, [CheckSum]
        FROM         TestTable
        ORDER BY PK
    
       OPEN curRollingCheckSum
    
      FETCH NEXT FROM curRollingCheckSum
       INTO @PK, @CheckSum
    
      WHILE @@FETCH_STATUS = 0
      BEGIN
    
      SET @RCS2 = CASE WHEN @RCS2 IS NULL THEN @CheckSum ELSE CHECKSUM(@CheckSum ^ @RCS2) END
    
    
     UPDATE dbo.TestTable 
        SET RollingCheckSum2 = @RCS2
      WHERE @PK = PK
    
      FETCH NEXT FROM curRollingCheckSum
       INTO @PK, @CheckSum
    
        END
    
    COMMIT
    

    Test they are the same

    SELECT * FROM TestTable
    WHERE RollingCheckSum1<> RollingCheckSum2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to keep elements in header in one row. My current styles
Hi guys I'm trying to keep developing SMS popup under the name of SMS
I'm trying to keep a hash local to one function that remembers its state
I was keep trying to update one of my application in Android Market. And
I am trying to keep one instance of a Window around and when needed
In trying to keep with unobtrusive JavaScript guidelines I moved all the JavaScript out
I'm trying to keep my code clean and keep the number of files down.
I'm trying to keep my xsl DRY and as a result I wanted to
I'm trying to keep Visual Studio from attempting to serialize the datasource of drop
I'm trying to keep the list of values from drop-down selections when the browser

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.