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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:55:52+00:00 2026-06-03T04:55:52+00:00

I need to update the second record AFS_OH column (ForecastID =23) with the previous

  • 0

I need to update the second record AFS_OH column (ForecastID =23) with the previous AFS_ToProduce value (ForecastID = 22) from the first record below. In otherwords the second record value for AFS_OH = 307923.

Also, I need to update these values for the entire table so I the ForecastID numbers will differ based on the criteria of AFS_ToProduce < 0 as you will notice in my WHERE clause below.

My script below is updating the same record value instead of the following record. Thanks for your help!

ForecastID        AFS_OH     AFS_ToProduce
22                307923    -307923
23                     0     316602

Here’s my approach:

UPDATE FC_Test
SET AFS_OH = (AFS_ToProduce * (-1))
FROM FC_Test S3
INNER JOIN
(
    SELECT S1.FORECASTID, S2.AFS_ToProduce AS AFS_OH
    FROM FC_Test S1
    LEFT OUTER JOIN
    (
        SELECT *
        FROM FC_Test
    ) S2
    ON S1.ForecastID = S2.ForecastID
)S4 ON S3.ForecastID = S4.ForecastID
WHERE AFS_ToProduce < 0
  • 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-03T04:55:54+00:00Added an answer on June 3, 2026 at 4:55 am

    The following update will transfer negative value of AFS_ToProduce to next row if next row has positive AFS_ToProduce. If not, this row will be ignored.
    Inner select top 1 will retrieve previous row if AFS_ToProduce is negative; if not, current row from fc_test will be skipped. This allows for gaps in ForecastID.

    update fc_test
    set AFS_OH = - fc2.AFS_ToProduce
    from fc_test
    cross apply
    (
      select fc2.AFS_ToProduce
      from 
      (
        select top 1
            fc2.AFS_ToProduce
          from fc_test fc2
         where fc2.ForecastID < fc_test.ForecastID
         order by fc2.ForecastID desc
      ) fc2
      where fc2.AFS_ToProduce < 0
    ) fc2
    where fc_test.AFS_ToProduce > 0
    

    TEST is here on Sql Fiddle.

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

Sidebar

Related Questions

I need to update the attribute value of bkp_path using VB6. XML File <ServerDetails>
I need to update the primary key for a record but it's also the
I need to update a record in the datastore, but instead of updated record
I need to get 5 random records from a table plus a further record
I need to extract records from a table, copy the data to a second
Basically, what I need to do is update a record in my datatable without
I'm using insert-/update triggers to update a second table's column Price . The insert
I need to check for updates on a (max) one second interval for updates.
I need to update a sharded counter and return the current count. I'm following
I need to update an existing SharePoint content type by adding a new field

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.