我有一个可怕的问题,如果你能完全理解,我会给你一些严肃的道具:
DECLARE @flightDateOrNow DATE = GETDATE()
DECLARE @pilotID INT = 1
SELECT P7.pilotID
FROM Pilot P7
WHERE location = 'DPG';
SELECT P1.pilotID, BasicInfo.lName, BasicInfo.fName,
PilotExamTemp.examType, PilotExamTemp.examDate,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'TNG')
AND NOT EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'AVO')
THEN 'X'
ELSE ''
END) TNG,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'AVO')
THEN 'X'
ELSE ''
END) AVO,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'MC')
THEN 'X'
ELSE ''
END) MC,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'IO')
THEN 'X'
ELSE ''
END) [IO],
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = @pilotID
AND certificationPosition = 'AVO'
AND certification = 'SO')
THEN 'X'
ELSE ''
END) SO,
ISNULL((SELECT SUM(P3.flightHours) MCTotal
FROM PilotLog P3
WHERE P3.pilotID = P1.pilotID
AND (flightDate <= P1.flightDate)
AND topLevelPosition = 'AVO'
AND dutyStation = 'MC'
), 0) MCTotal,
ISNULL((SELECT SUM(P3.flightHours) AVOTotal
FROM PilotLog P3
WHERE P3.pilotID = P1.pilotID
AND (flightDate <= P1.flightDate)
AND topLevelPosition = 'AVO'
AND dutyStation <> 'MC'
), 0) AVOTotal,
(SELECT DATEADD(day, 60, (SELECT TOP 1 flightDate
FROM PilotLog P3
WHERE P3.flightDate < P1.flightDate
AND P3.topLevelPosition = 'AVO'
AND P3.dutyStation = 'MC'
ORDER BY P3.flightDate DESC))
) currency60Day,
(
SELECT COUNT(*)
FROM PilotLog P4
WHERE P4.pilotID = @pilotID
AND topLevelPosition = 'AVO'
AND sortieGained = 'True'
AND (P4.flightDate BETWEEN (CASE
WHEN P1.flightDate > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) ))
WHEN P1.flightDate > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
THEN (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
ELSE (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
End)
AND
(P1.flightDate))) semiSorties,
(
SELECT SUM(P4.flightHours)
FROM PilotLog P4
WHERE P4.pilotID = @pilotID
AND topLevelPosition = 'AVO'
AND sortieGained = 'True'
AND (P4.flightDate BETWEEN (CASE
WHEN P1.flightDate > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) ))
WHEN P1.flightDate > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
THEN (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
ELSE (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), P1.flightDate) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
End)
AND
(P1.flightDate))) semiSortieHours,
(CASE
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)))))) ))
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
THEN (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,13,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
ELSE (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))))))
End) semiEnd,
(CASE
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) ))) THEN (DATEADD(yyyy, 1, (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) )))))
ELSE (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID))) )))
END) annualEnd,
PMTemp.expirationDate, PMTemp.classFAAMedical
FROM PilotLog P1, Pilot BasicInfo,
(SELECT DATEADD(dd, -DAY(DATEADD(m,1, medicalDate)), DATEADD(m,13,medicalDate)) expirationDate, classFAAMedical
FROM PilotMedical
WHERE (pilotID = @pilotID)
AND medicalDate = (SELECT MAX(medicalDate) FROM PilotMedical WHERE pilotID = @pilotID)) AS PMTemp,
(SELECT *
FROM PilotExam
WHERE (pilotID = @pilotID)
AND examPosition = 'AVO'
AND examDate = (SELECT MAX(examDate) FROM PilotExam WHERE pilotID = @pilotID AND examPosition = 'AVO')) AS PilotExamTemp
WHERE P1.pilotID = @pilotID
AND P1.topLevelPosition = 'AVO'
AND P1.pilotID = BasicInfo.pilotID
AND P1.flightDate = (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = @pilotID)
ORDER BY flightDate DESC
这导致:
pilotID
1
2
3
4
5
pilotID lName fName examType examDate TNG AVO MC IO SO MCTotal AVOTotal currency60Day semiSorties semiSortieHours semiEnd annualEnd expirationDate classFAAMedical
1 Wilkins Landon Written 2012-10-11 X X X 16.3 7.0 2012-08-14 2 16.7 2012-10-31 2012-10-31 2013-04-30 Third
但我想要的是每个 PilotID 的真正长线。是否有捷径可寻?还是我必须完全重组我的 SQL?(顺便说一句,该 SQL 中真正令人毛骨悚然的部分是我计算出飞行员的半年开始和结束时间,因为它是基于他们的出生月份。真的很烦人)
编辑#1:
我主要使用以下内容:
DECLARE @flightDateOrNow DATE = GETDATE()
SELECT P7.pilotID
FROM Pilot P7
WHERE location = 'DPG';
SELECT DISTINCT BasicInfo.pilotID, BasicInfo.lName, BasicInfo.fName,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'TNG')
AND NOT EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'AVO')
THEN 'X'
ELSE ''
END) TNG,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'AVO')
THEN 'X'
ELSE ''
END) AVO,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'MC')
THEN 'X'
ELSE ''
END) MC,
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'IO')
THEN 'X'
ELSE ''
END) [IO],
(CASE
WHEN EXISTS (SELECT 1
FROM PilotGFRCertification
WHERE pilotID = P1.pilotID
AND certificationPosition = 'AVO'
AND certification = 'SO')
THEN 'X'
ELSE ''
END) SO,
ISNULL((SELECT SUM(P3.flightHours) MCTotal
FROM PilotLog P3
WHERE P3.pilotID = P1.pilotID
AND (flightDate <= (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID))
AND topLevelPosition = 'AVO'
AND dutyStation = 'MC'
), 0) MCTotal,
ISNULL((SELECT SUM(P3.flightHours) AVOTotal
FROM PilotLog P3
WHERE P3.pilotID = P1.pilotID
AND (flightDate <= (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID))
AND topLevelPosition = 'AVO'
AND dutyStation <> 'MC'
), 0) AVOTotal,
(SELECT DATEADD(day, 60, (SELECT TOP 1 flightDate
FROM PilotLog P3
WHERE P3.flightDate < (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)
AND P3.topLevelPosition = 'AVO'
AND P3.dutyStation = 'MC'
ORDER BY P3.flightDate DESC))
) currency60Day,
(
SELECT COUNT(*)
FROM PilotLog P4
WHERE P4.pilotID = P1.pilotID
AND topLevelPosition = 'AVO'
AND sortieGained = 'True'
AND (P4.flightDate BETWEEN (CASE
WHEN (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID) > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) ))
WHEN (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID) > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
THEN (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
ELSE (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
End)
AND
((SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)))) semiSorties,
(
SELECT SUM(P4.flightHours)
FROM PilotLog P4
WHERE P4.pilotID = P1.pilotID
AND topLevelPosition = 'AVO'
AND sortieGained = 'True'
AND (P4.flightDate BETWEEN (CASE
WHEN (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID) > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) ))
WHEN (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID) > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
THEN (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
ELSE (DATEADD(dd,-(DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))-1),DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), (SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
End)
AND
((SELECT MAX(P5.flightDate) FROM PilotLog P5 WHERE pilotID = P1.pilotID)))) semiSortieHours,
(CASE
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )), DATEADD(m,7,(DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) )))
THEN (DATEADD(yyyy, 1, (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID)))))) ))
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
THEN (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,13,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
ELSE (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))), DATEADD(m,7,(DATEADD(yyyy, DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))))))
End) semiEnd,
(CASE
WHEN @flightDateOrNow > (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) ))) THEN (DATEADD(yyyy, 1, (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) )))))
ELSE (DATEADD(dd, -DAY(DATEADD(m,1,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) )), DATEADD(m,13,(DATEADD(yyyy, (DATEDIFF(yy, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID), @flightDateOrNow)) - 1, (SELECT birthDate FROM Pilot WHERE pilotID = P1.pilotID))) )))
END) annualEnd
FROM PilotLog P1
FULL JOIN Pilot BasicInfo
ON P1.pilotID = BasicInfo.pilotID
WHERE
BasicInfo.location = 'DPG'
现在对于模式(在上传它们的过程中,到目前为止这里有几个表)
飞行员
试点日志
试点考试
PilotGFR认证
Pilot医疗