为什么查询MAX(日期)不会返回任何记录?

时间:2014-12-10 15:18:32

标签: sql sql-server

我有一个像下面这样的SQL查询:

SELECT CPH.CheckPointID,
    CPH.ID AS Check_Point_History_ID,
    CLCP.SequenceNo AS Sequence,
    CP.Code AS Point_Code,
    CPV.ID,
    TT.Medium AS Description,
    [TEXT_TRANSLATION_ANS].[Medium] AS Value,
    CPH.Value_ AS Additional_Information,
    EMP.NAME AS Checked_By,
    CPH.AnsweredOn AS Checked_On
FROM CHECK_LIST_HISTORY CLH
LEFT JOIN CHECK_POINT_HISTORY CPH
    ON CLH.ID = CPH.CheckListHistoryID
INNER JOIN (
        SELECT CPH2.CheckPointID,
            MAX(CPH2.AnsweredOn) AS MaxDate
        FROM CHECK_LIST_HISTORY CLH2
        LEFT JOIN CHECK_POINT_HISTORY CPH2
            ON CLH2.ID = CPH2.CheckListHistoryID
        GROUP BY CPH2.CheckPointID
    ) tm
    ON CPH.CheckPointID = tm.CheckPointID
    AND CPH.AnsweredOn = tm.MaxDate
LEFT JOIN CHECK_POINT CP
    ON CPH.CheckPointID = CP.ID
LEFT JOIN CHECK_POINT_VALUE CPV
    ON CPH.CheckPointValueID = CPV.ID
LEFT JOIN TEXT_TRANSLATION TT
    ON CP.TextID = TT.TextID
    AND TT.LanguageID = LanguageID
LEFT JOIN CHECK_LIST_CHECK_POINT CLCP
    ON CP.ID = CLCP.CheckPointID
LEFT JOIN EMPLOYEE EMP
    ON CPH.EmployeeID = EMP.ID
LEFT JOIN [TEXT_TRANSLATION] [TEXT_TRANSLATION_ANS]
    ON CPV.AnswerTextID = [TEXT_TRANSLATION_ANS].[TextID]
    AND [TEXT_TRANSLATION_ANS].[LanguageID] = TT.LanguageID
LEFT JOIN [TEXT_TRANSLATION] [TEXT_TRANSLATION_RES]
    ON CPV.ResponseTextID = [TEXT_TRANSLATION_RES].[TextID]
    AND [TEXT_TRANSLATION_RES].[LanguageID] = TT.LanguageID
WHERE CLH.WipOrderNo = 304
    AND CLH.WipOrderType = 26
    AND CLCP.WorkCenter = 'WC03'
    AND CLCP.Facility = 'C1P1'

此查询应该返回两条具有最大日期的记录,但它不返回任何内容。我认为问题出在INNER JOIN中,因为当注释了INNER JOIN的行时,查询返回下表:

table

0 个答案:

没有答案