0

好的,这是我的查询:

SELECT DISTINCT
    CS.CPL_Schedule_Id          AS CplScheduleId
    ,S.Schedule_Status_Id       AS ScheduleStatusId
    ,S.Record_Created           AS Created
FROM CPL_Schedule CS
    JOIN (
            SELECT TOP 1 CSL.CPL_Schedule_ID, CSL.Record_Created, CSL.Schedule_Status_Id
            FROM CPL_Schedule_Audit_Log CSL
            WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID
            ORDER BY Record_Created DESC
        ) S
        ON S.CPL_Schedule_ID = CS.CPL_Schedule_ID
WHERE 
    CS.Exhibitor_Id = 1 

我在这条线上得到了错误WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID

无法绑定多部分标识符“CS.CPL_Schedule_ID”。

在我看来,这似乎是在说嵌套查询不了解 CS,这是真的吗?我在这里想念什么?非常感谢您的帮助,谢谢!

4

1 回答 1

4

看来,您需要cross apply代替join,因此不需要ON

SELECT DISTINCT
    CS.CPL_Schedule_Id          AS CplScheduleId
    ,S.Schedule_Status_Id       AS ScheduleStatusId
    ,S.Record_Created           AS Created
FROM CPL_Schedule CS
    CROSS APPLY (
            SELECT TOP 1 CSL.CPL_Schedule_ID, CSL.Record_Created, CSL.Schedule_Status_Id
            FROM CPL_Schedule_Audit_Log CSL
            WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID
            ORDER BY Record_Created DESC
        ) S
WHERE 
    CS.Exhibitor_Id = 1 
于 2013-07-25T21:39:27.033 回答