0

我有这个查询:

SELECT  TM_User.FullName ,
                Task.end_date
        FROM    TM_User
        INNER JOIN ( 
            SELECT Task.task_id ,
                Task_Hours.user_id ,
                ROW_NUMBER() OVER ( PARTITION BY user_id ORDER BY Task.end_date DESC ) rtt ,
                Task.end_date
            FROM   Task_Hours
            INNER JOIN ( 
                SELECT Task.task_id ,
                    Task.end_date ,
                    ROW_NUMBER() OVER ( PARTITION BY Task.task_id ORDER BY Task.end_date DESC ) rr
                FROM   Task
                WHERE  Task.start_dte >= '2013-08-15'
                    AND Task.end_date <= '2013-08-31'
             ) Task ON Task.task_id = Task_Hours.task_id
        ) a ON TM_User.TM_UserID = a.user_id
        WHERE   a.rtt = 1
                AND is_active = 1 
4

1 回答 1

1

在您编写最外层SELECT子句时,只有两个行集名称/别名仍在范围内,TM_User(表或视图)和a(您为子查询指定的名称)。

因此,使用包含end_date-的行集的名称a

SELECT  TM_User.FullName ,
                a.end_date
        FROM    TM_User
        INNER JOIN ( 
            SELECT Task.task_id ,
                Task_Hours.user_id ,
                ROW_NUMBER() OVER ( PARTITION BY user_id ORDER BY Task.end_date DESC ) rtt ,
                Task.end_date
            FROM   Task_Hours
            INNER JOIN ( 
                SELECT Task.task_id ,
                    Task.end_date ,
                    ROW_NUMBER() OVER ( PARTITION BY Task.task_id ORDER BY Task.end_date DESC ) rr
                FROM   Task
                WHERE  Task.start_dte >= '20130815'
                    AND Task.end_date <= '20130831'
             ) Task ON Task.task_id = Task_Hours.task_id
        ) a ON TM_User.TM_UserID = a.user_id
        WHERE   a.rtt = 1
                AND is_active = 1 

(还将日期文字更改为明确的)

于 2013-08-29T06:25:18.743 回答