HiThis is a follow on from one of my earlier threads where I was trying to return one specific record. In this case I am trying to return multiple records for the same person ID and within a number of days range. Snapshot of my date with same PersonID:PersonID Arrival_Date Leaving_Date ArrivalID======== ============ ============ =========123456 01/12/2012 01/12/2013 arr_56464123456 10/12/2012 10/12/2013 arr_56474123456 13/12/2012 13/12/2013 arr_56494
And from this I want to check if one record's leaving date of the record is within 7 days of another record's arrival date. I also want to return the record that had a leaving date within 7 days of the next arrival date.I understand that if I self join on personID with the data above I will get 9 rows, for each row I will get 3 matches, I am using INNER JOIN to join to the same table but with a different alias so I assume this is the self join I should be using.Simple code============But then how do I process this? I would want to say for record 1 check the leaving date is within 7 days of arrival date of any other record matching that PersonId but not ArrivalID, and return both records.From my snapahot of code I would eventually want to return:PersonID Arrival_Date Leaving_Date ArrivalID======== ============ ============ =========123456 10/12/2012 10/12/2013 arr_56474123456 13/12/2012 13/12/2013 arr_56494
But can't seem to get this using a self join query like this:select a.PersonID, a.Leaving_date,a.Arrival_Date,a.arrivalIDfrom arrivals a INNER JOIN arrivals bON b.personID = a.personIDWHERE a.arrivalid != b.arrivalidand DATEDIFF(DD, b.[Leaving_date], a.[Arrival_Date]) <=7and DATEDIFF(DD, b.[Leaving_date], a.[Arrival_Date]) >=0
If any clarification as to what I want to see is needed please ask.ThanksG