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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:27:34+00:00 2026-06-15T21:27:34+00:00

I have a weird update query to write. Here’s the table PK-ID (int) —

  • 0

I have a weird update query to write.

Here’s the table

PK-ID (int) --- FK-ID (int) --- Value (int)

In my data set, if I group by FK-ID and order by PK-ID, suppose this is an example of one group:

5 --- 10 --- 23
7 --- 10 --- 49
8 --- 10 --- 81

Due to a bug in some old software, records 7 and 8 have incorrect values. The correct value for 7 is (49-23) = 26 and the correct value for 8 is (81-49) = 32. Record 5 is correct.

I need to update each record to subtract the value of the record immediately preceding it when it is grouped by FK-ID and ordered by PK-ID. If there is no preceding record I do not need to change the value.

Is there a way to write a general sql update query to accomplish this? How would I (conditionally) retrieve the value of the preceding record in the group? I’m using SQL server 2008.

Thanks!

  • 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-06-15T21:27:35+00:00Added an answer on June 15, 2026 at 9:27 pm
    with ordered as (
        select *, rn = row_number() over (partition by fk_id order by pk_id)
          from tbl
        )
    update cur
       set value = cur.value - prior.value
      from ordered cur
      join ordered prior on prior.fk_id = cur.fk_id
                    and prior.rn = cur.rn-1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird problem. When I execute a query the data is not
Hey guys, I have a weird problem. I have an update system that refreshes
I have a weird problem with my mysql query syntax. I have made a
In the last past days I noticed something weird optimizing my query. I have
I have to deal with a table where there is a set of fields
Ok, I have an update function with a weird twist. Due to the nature
I need some help in building the correct query. I have an Employees table.
I have a weird problem in mysql with php I am running this query
We have one of those weird cryptic data corruption bugs that pops up every
I got a weird problem of MySQL index. I have a table views_video :

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.