Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Next Related Record By Date 1

Status
Not open for further replies.

xyle

Programmer
Jul 2, 2003
23
0
0
US
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top