You can try this. SQL Fiddle
WITH cteBookingPassengerVersion AS ( SELECT BookingID, RTRIM( CONCAT ( ISNULL(Title + ' ', ''), ISNULL(FirstName + ' ', ''), ISNULL(MiddleName + ' ', ''), ISNULL(LastName, '') ) ) AS NAME, ModifiedDate, ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum FROM BookingPassengerVersion ) SELECT cte.BookingID, ctePrev.NAME AS OldName, cte.NAME AS NewName, bh.Detail, bh.CreatedAgentCode, bh.ChangeDate FROM BH2 bh JOIN cteBookingPassengerVersion cte ON bh.BookingID = cte.BookingID AND bh.ChangeDate = cte.ModifiedDate LEFT JOIN cteBookingPassengerVersion ctePrev ON ctePrev.BookingID = cte.BookingId AND ctePrev.rowNum = cte.rowNum + 1 ORDER BY cte.BookingID, bh.ChangeDate DESC
EDIT I updated the request to join the date and receive all updates for all orders
Update New SQL Script
To filter the CTE by "ReservationID" number in BH2, you can either do
WITH cteBookingPassengerVersion AS ( SELECT BookingID, RTRIM( CONCAT ( ISNULLLL(Title + ' ', ''), ISNULL(FirstName + ' ', ''), ISNULL(MiddleName + ' ', ''), ISNULL(LastName, '') ) ) AS NAME, ModifiedDate, ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum FROM BH2 JOIN BookingPassengerVersion ON BH2.BookingID = BookingPassengerVersion.BookingID )
or
WITH cteBookingPassengerVersion AS ( SELECT BookingID, RTRIM( CONCAT ( ISNULLLL(Title + ' ', ''), ISNULL(FirstName + ' ', ''), ISNULL(MiddleName + ' ', ''), ISNULL(LastName, '') ) ) AS NAME, ModifiedDate, ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum FROM BookingPassengerVersion WHERE BookingID IN (SELECT BookingID FROM BH2) )
you have to try different things when dealing with large data sets. I would even replace cte with a temp table and see if it helps. Check your execution plan to see if you need any indexes.
temp table instead of cte
SELECT BookingID, RTRIM( CONCAT ( ISNULLLL(Title + ' ', ''), ISNULL(FirstName + ' ', ''), ISNULL(MiddleName + ' ', ''), ISNULL(LastName, '') ) ) AS NAME, ModifiedDate, ROW_NUMBER()OVER(PARTITION BY BookingID ORDER BY ModifiedDate DESC) rowNum INTO