其中条件在sql中的多个内连接上失败

时间:2015-11-26 14:00:16

标签: sql

我正在尝试内部连接多个列,但导致无效列在where条件。 查询:

select t_contract,t_complete,t_zone_name, t_street_name, t_vrm, t_make, t_model,t_colour,t_date_finally_settled,t_cancelled,t_offence_code,
    t_date_time_issued,    CONVERT(VARCHAR, t_date_time_issued, 103) as date_issued,   CONVERT(VARCHAR, t_date_time_issued, 108) AS time_issued,   te_event, lhr.*  
        FROM tickets t  
        LEFT OUTER JOIN   
         (  
         SELECT DISTINCT thr.thr_system_ref, hr.hr_description AS latest_on_hold_reason  
                FROM dbo.tickets t  
                INNER JOIN dbo.ticket_hold_record thr  
                ON (t.t_number=thr.thr_system_ref)  
                INNER JOIN  
                (  
                    SELECT mthr.thr_system_ref, MAX(mthr.thr_on_hold_date) AS m_thr_on_hold_date  
                    FROM dbo.ticket_hold_record mthr  
                    GROUP BY mthr.thr_system_ref  
                ) latest  
                ON (thr.thr_system_ref=latest.thr_system_ref AND thr.thr_on_hold_date=latest.m_thr_on_hold_date)  
                INNER JOIN dbo.hold_reasons hr    " ON (thr.thr_hold_type=hr.hr_code) where hr_code in (' 2260675','2793360','2810778','2903324','2420135')
                ON (thr.thr_hold_type=hr.hr_code)  
            )lhr  
            ON (t.t_number=lhr.thr_system_ref)  

         Inner JOIN  
           ticket_events  te  
         ON ( t.t_number = te.te_system_ref)  

        where  thr.thr_hold_type = '2420135' 
        and  convert (datetime,te_date,101) between convert(datetime,'2015/11/01',101)
           and convert (datetime,'2015/11/25',101) 

错误: 列前缀' thr'与查询中使用的表名或别名不匹配。

1 个答案:

答案 0 :(得分:1)

与以前相同的查询,但我已将thr.thr_hold_type = '2420135'条件移动到子查询中以进入范围(并且也不会混淆不同):

select t_contract,t_complete,t_zone_name, t_street_name, t_vrm, t_make, t_model,t_colour,t_date_finally_settled,t_cancelled,t_offence_code,
    t_date_time_issued,    CONVERT(VARCHAR, t_date_time_issued, 103) as date_issued,   CONVERT(VARCHAR, t_date_time_issued, 108) AS time_issued,   te_event, lhr.*  
        FROM tickets t  
        LEFT OUTER JOIN   
         (  
         SELECT DISTINCT thr.thr_system_ref, hr.hr_description AS latest_on_hold_reason  
                FROM dbo.tickets t  
                INNER JOIN dbo.ticket_hold_record thr  
                ON (t.t_number=thr.thr_system_ref)  
                INNER JOIN  
                (  
                    SELECT mthr.thr_system_ref, MAX(mthr.thr_on_hold_date) AS m_thr_on_hold_date  
                    FROM dbo.ticket_hold_record mthr  
                    GROUP BY mthr.thr_system_ref  
                ) latest  
                ON (thr.thr_system_ref=latest.thr_system_ref AND thr.thr_on_hold_date=latest.m_thr_on_hold_date)  
                INNER JOIN dbo.hold_reasons hr  
                ON (thr.thr_hold_type=hr.hr_code)  
                WHERE thr.thr_hold_type = '2420135' 
            )lhr  
            ON (t.t_number=lhr.thr_system_ref)  

         Inner JOIN  
           ticket_events  te  
         ON ( t.t_number = te.te_system_ref)  

        where convert (datetime,te_date,101) between convert(datetime,'2015/11/01',101)
           and convert (datetime,'2015/11/25',101)