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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:14:03+00:00 2026-06-05T20:14:03+00:00

I have a database which, simiplified, is as follows : ID | SecondID |

  • 0

I have a database which, simiplified, is as follows :

ID  |  SecondID   |  DateRecorded  |  NumberToCheck  |  NumberToUpdate

NumberToUpdate currently has a value for 0 for all rows.

I wish to update NumberToUpdate with the value for NumberToCheck on the same row, MINUS the value for NumberToCheck which has the earliest DateRecorded “(Min(DateRecorded)” where NumberToCheck is greater than 0, and which has the same ID and secondID as the original row.

So far I have

UPDATE dbo.Table
SET NumberToUpdate =//Update NumberToUpdate
   NumberToCheck - //NumberToCheck from the current row, subtracting...
      (SELECT TOP 1 t2.NumberToCheck FROM Table t2 WHERE ID = t2.ID AND secondID = t2.secondID AND t2.DateRecorded = 
      (SELECT TOP 1 MIN(t3.DateRecorded) FROM Table t3  WHERE t2.ID = t3.ID AND t2.secondID = t3.secondID AND t3.Passes > 0))
      //the NumberToCheck with the earliest date, of the same ID.

This is not correct however, and is returning me values which make no sense (including minus values of which there shouldn’t be any!)

What have I forgotten here?

Thanks very much

  • 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-05T20:14:04+00:00Added an answer on June 5, 2026 at 8:14 pm

    First, you should start with a select query to get the value you want:

    select t.*
    from (select t.*, row_number() over (partition by id order by date) as seqnum
          from table t
          where number_to_check > 0
         ) t
    where seqnum = 1
    

    Now, you can use this in your original update:

    with vals as (select t.*
                  from (select t.*, row_number() over (partition by id order by date) as seqnum
                        from table t
                        where NumberToCheck > 0
                       ) t
                  where seqnum = 1
                 )
    update table
        set NumberToUpdate = NumberToCheck - vals.NumberToCheck
    from vals
    where t.id = vals.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database which has a table recording when a vehicle was last
I have an INT column in a SQL Server database which stores a value
I have a simple database table (SQL Server 2008 R2 Express), which has a
I have a database which is going to have UMT timestamps in standard sql
I have a database which contains the link to audio files. I am trying
i have a database which contains an arabic tables , i can read it
I have a database which I regularly need to import large amounts of data
I have a database which contains picture data stored as a binary blob. The
I have a database which stores (among other things), the following pieces of information:
I have a database which needs to store year ranges (such as lifespan) which

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.