|
|
Question : query to get previous field (similar to crystal 'Previous (fld)' or 'Next(fld)' )
|
|
Hi experts,
I have this two tables that I need to Join and I need to get Next field in the next record. Next field is based on the Date field of the following record. Here are my tables and the result I need.
Table1: ResID Class ALTJ 80
Table2: ResID Date Room ALTJ 3/4/2005 StudioA ALTJ 7/3/2005 StudioC
I need to get: TableResult: ResID Class Date Room NextDateFld ALTJ 80 3/4/2005 StudioA 7/3/2005 ALTJ 80 7/3/2005 StudioC
The last NextDateFld is NULL because there is no next date for that ResID (no other record)
Thanks for any suggestions cm
|
Answer : query to get previous field (similar to crystal 'Previous (fld)' or 'Next(fld)' )
|
|
Since the NextDateFld is a derived column, you cannot include that in the WHERE clause. One thing you can do is the following:
SELECT * FROM ( SELECT DISTINCT A.ResID, A.Class, B.Date, B.Room, ISNULL((SELECT MIN(Date) FROM Table2 C WHERE A.ResID = C.ResID AND C.Date > B.Date), A.LastDate) AS NextDateFld FROM Table1 A INNER JOIN Table2 B ON A.ResID = B.ResID) A WHERE NextDateFld != '01/01/2000'
|
|
|
|
|