1

我有一个可怕的问题,如果你能完全理解,我会给你一些严肃的道具:

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医疗 在此处输入图像描述

4

2 回答 2

2

你可以这样做:

  • 定义一个表变量来保存您要处理的飞行员 ID

    DECLARE @PilotIDs TABLE (PilotID INT)
    
    INSERT INTO @PilotIDs 
       SELECT P7.pilotID 
       FROM Pilot P7
       WHERE location = 'DPG';
    
  • 在开始时(在要选择的列列表中),您有几次这种情况:

    WHERE pilotID = @pilotID  
    

    将其替换为:

    WHERE pilotID = P1.PilotID
    
  • 不理会其余的查询-最后您有一些条件,例如

    WHERE P1.pilotID = @pilotID 
    

    将它们更改为:

    WHERE P1.pilotID IN (SELECT PilotID FROM @PilotIDs)
    

应该基本上照顾它 - 我希望!这是一个非常巨大的丑陋查询怪物......我希望它不会破坏 QueryZilla 中间的任何地方......

于 2012-07-24T15:48:17.087 回答
1

乍一看,我相信SELECT通过以下两个更改更改您的最后一条语句应该会得到您正在寻找的结果:

  1. 将所有实例替换@pilotIDP1.PilotID
  2. 删除WHERE子句P1.pilotID = @pilotID

不过,我很想看看整体架构,因为这看起来可以写成更短的查询,而是利用一些查找表和 JOIN。

于 2012-07-24T15:48:38.663 回答