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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:53:51+00:00 2026-05-17T16:53:51+00:00

Table has 2 cols: [nr] and [diff] diff is empty (so far – need

  • 0

Table has 2 cols: [nr] and [diff]

diff is empty (so far – need to fill)

nr has numbers:

 1
 2
 45
 677
 43523452

on the diff column i need to add the differences between pairs

 1 | 0
 2 | 1
 45 | 43
 677 | 632
 43523452 | 43522775

so basically something like :

 update tbl set diff = @nr - @nrold where nr = @nr

but i don’t want to use fetch next, because it’s not cool, and it’s slow (100.000) records

how can I do that with one update?

  • 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-17T16:53:52+00:00Added an answer on May 17, 2026 at 4:53 pm
    CREATE TABLE #T(nr INT,diff INT)
    
    INSERT INTO #T (nr) SELECT 1 
                  UNION SELECT 2 
                  UNION SELECT 45 
                  UNION SELECT 677 
                  UNION SELECT 43523452
    
    ;WITH cte AS
    (
    SELECT nr,diff, ROW_NUMBER() OVER (ORDER BY nr) RN
    FROM #T
    )
    UPDATE c1
    SET diff = ISNULL(c1.nr - c2.nr,0)
    FROM cte c1
    LEFT OUTER JOIN cte c2 ON c2.RN+1= c1.RN
    
    SELECT nr,diff FROM #T
    
    DROP TABLE #T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PROFESSIONAL Table has many columns FIRST_NAME,MIDDLE_NAME, LAST_NAME, TITLE_CODE, COMMUNICATION_TYPE_CODE, COMMUNICATION_VALUE PROFESSIONAL Table COMMUNICATION_TYPE_CODE column
My DB table has a column that can contain a very big amount of
An Orders table has a CustomerId column and an OrderId column. For certain reasons
My table has few fields with an amount column of type decimal. This column
I have table TB1 which has the following cols: ID | date --------------------- 1
I have a table that is dynamically built using DOM. It has 10 cols,
I have a table with function add new column. When it is pressed, a
Table has the following info: date |vendor|sales|percent| -------------------------- 2009-03-03| 10 |13.50| 1.30 | 2009-03-10|
My table has records for every minute, now I want to fetch records with
my table has the following fields videos |id| |average| |views| now i select some

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.