Select x.DriverID,DriverStatus, LocationDescription, BoardType
FROM
(
SELECT DriverID, DriverStatus FROM
(
SELECT l1.DriverID, l1.StatusDate, DriverStatusMapping.DriverStatusID FROM
(
SELECT DriverID, MAX(StatusDate) AS StatusDate from DriverStatusMapping
GROUP BY DriverID
) l1,
DriverStatusMapping
WHERE l1.DriverID=DriverStatusMapping.DriverID
AND l1.StatusDate = DriverStatusMapping.StatusDate
) l2,
DriverStatus
Where l2.DriverStatusID = DriverStatus.DriverStatusID
) x
INNER JOIN (
--Location Mapping
SELECT DriverID, LocationDescription FROM
(
SELECT l1.DriverID, l1.StatusDate, LocationMapping.LocationID FROM
(
SELECT DriverID, MAX(StatusDate) AS StatusDate from LocationMapping
GROUP BY DriverID
) l1,
LocationMapping
WHERE l1.DriverID=LocationMapping.DriverID
AND l1.StatusDate = LocationMapping.StatusDate
) l2,
Location
Where l2.LocationID = Location.LocationID
) y ON x.DriverId = y.DriverId
--Board Mapping
INNER JOIN (
SELECT DriverID, BoardType FROM
(
SELECT l1.DriverID, l1.StatusDate, BoardTypeID FROM
(
SELECT DriverID, MAX(StatusDate) AS StatusDate FROM BoardMapping Group by DriverID
) l1,
BoardMapping
WHERE l1.DriverID = BoardMapping.DriverID
AND l1.StatusDate = BoardMapping.StatusDate
) l2,
Board
WHERE l2.BoardTypeID = board.BoardTypeID
) z ON x.DriverId = z.DriverId
|