INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Find Next Related Record By Date

Find Next Related Record By Date

(OP)
First off this is not my database I am just fixing a report. I have two tables with following relevant fields.

Table 1 AR_Detail
Fields AR_Detail_Id, P_Id, Trans_Type, Posting_Stamp

Table 2 Assessed_Values
Field Value_Id, P_Id, History_Id, Assessed_Value, Change_Date

Their only related field is P_ID. This report is pulling the AR_Detail records where Trans_Type is 'avc' and I need the Assessed_Value that is the first record where Change_Date is >= Posting_Stamp.

The problem I am having is someone entered a wrong assessed value change and then made a correct assessed value change and I can't get the AR_Detail record and it subsequent Assesed_Values record to match up.

I can make a View or Stored Procedure for the report source.

RE: Find Next Related Record By Date

Pretty sure there are some other ways to do this, but this should work (once you clean up whatever I got wrong from writing code I can't test):

CODE

SELECT {the fields you need from AR_Detail}, AssessedValue
  FROM AR_Detail ARD
    JOIN Assessed_Values AV
      ON ARD.P_Id = AV.P_Id
    WHERE AV.Change_Date = 
      (SELECT MIN(AV2.Change_Date)
         FROM Assessed_Values AV2
         WHERE AV2.P_Id = ARD.P_Id
           AND AV2.Change_Date > ARD.Posting_Stamp) 

Tamar

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close