I am trying to merge these 2 Select statements:
1)
SELECT a.*,
b.flightPilotAssignedTimestamp
FROM Pilot a,
(SELECT pilotid,
MAX(flightPilotAssignedTimestamp) flightPilotAssignedTimestamp
FROM FlightPilot
GROUP BY pilotid) b
WHERE a.pilotid IN (SELECT p.pilotid
FROM Flight f,
Pilot p,
PilotFlightPlaneType pfpt,
PilotFlightLocation pfloc,
PilotFlightType pft
WHERE f.flightPlaneTypeId = pfpt.flightPlaneTypeId
AND f.flightLocationId = pfloc.flightLocationId
AND f.flightTypeId = pft.flightTypeId
AND f.flightId = @flightId
)
AND a.pilotId = b.pilotId
ORDER BY a.pilotScore DESC
2)
select top 1 flightStatusTrackingStatusId, *
from flightStatusTracking
where flightId = @flightId --1022
and pilotId = @pilotId --10260
order by flightStatusTrackingId DESC
I am not sure how to to merge these statements as SELECT (2) may not return any records or may return many records, and so I use "TOP" and "order by ... DESC"
The merged select should return flightPilotAssignedTimestamp, flight information, and the most recent flightStatusTrackingStatusId from FlightStatusTracking if there is a record or NULL otherwise for each pilot of @flightId.
Tables:
1) FlightStatusTrackingStatus
flightStatusTrackingStatusTrackingId (primary key)
flightStatusTrackingStatusId - to track flight status
flightId
pilotId
flightStatusTrackingStatusId is used to track the flight status, and flightStatusTrackingStatusTrackingId is the primary key of the flightStatusTracking table. These are 2 different fields.
2) Pilot
pilotId (primary key)
pilotName, address, etc
3) Flight
flightId (primary key)
flightName, destination, path, etc
4) FlightPilot - index table to see which flights belong to which pilots
flightId
pilotId
5) PilotFlightPlaneType - index table
pilotId
flightPlaneTypeId
6) PilotFlightLocation - index table
pilotId
flightLocationId
7) PilotFlightType - index table
pilotId
flightTypeId
The merged select should return the most recent flightPilotAssignedTimestamp, flight information, and the most recent flightStatusTrackingStatusId from FlightStatusTracking if there is a record or NULL otherwise for each pilot of @flightId. FlightStatusTrackingStatusId has a value from 1 to 8 (not in any order).
1)
SELECT a.*,
b.flightPilotAssignedTimestamp
FROM Pilot a,
(SELECT pilotid,
MAX(flightPilotAssignedTimestamp) flightPilotAssignedTimestamp
FROM FlightPilot
GROUP BY pilotid) b
WHERE a.pilotid IN (SELECT p.pilotid
FROM Flight f,
Pilot p,
PilotFlightPlaneType pfpt,
PilotFlightLocation pfloc,
PilotFlightType pft
WHERE f.flightPlaneTypeId = pfpt.flightPlaneTypeId
AND f.flightLocationId = pfloc.flightLocationId
AND f.flightTypeId = pft.flightTypeId
AND f.flightId = @flightId
)
AND a.pilotId = b.pilotId
ORDER BY a.pilotScore DESC
2)
select top 1 flightStatusTrackingStatusId, *
from flightStatusTracking
where flightId = @flightId --1022
and pilotId = @pilotId --10260
order by flightStatusTrackingId DESC
I am not sure how to to merge these statements as SELECT (2) may not return any records or may return many records, and so I use "TOP" and "order by ... DESC"
The merged select should return flightPilotAssignedTimestamp, flight information, and the most recent flightStatusTrackingStatusId from FlightStatusTracking if there is a record or NULL otherwise for each pilot of @flightId.
Tables:
1) FlightStatusTrackingStatus
flightStatusTrackingStatusTrackingId (primary key)
flightStatusTrackingStatusId - to track flight status
flightId
pilotId
flightStatusTrackingStatusId is used to track the flight status, and flightStatusTrackingStatusTrackingId is the primary key of the flightStatusTracking table. These are 2 different fields.
2) Pilot
pilotId (primary key)
pilotName, address, etc
3) Flight
flightId (primary key)
flightName, destination, path, etc
4) FlightPilot - index table to see which flights belong to which pilots
flightId
pilotId
5) PilotFlightPlaneType - index table
pilotId
flightPlaneTypeId
6) PilotFlightLocation - index table
pilotId
flightLocationId
7) PilotFlightType - index table
pilotId
flightTypeId
The merged select should return the most recent flightPilotAssignedTimestamp, flight information, and the most recent flightStatusTrackingStatusId from FlightStatusTracking if there is a record or NULL otherwise for each pilot of @flightId. FlightStatusTrackingStatusId has a value from 1 to 8 (not in any order).