2015-11-26 61 views
0

我想內連接多個列,但導致在哪裏條件無效列。 查詢:其中條件失敗的多個內部連接sql

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」不與在查詢中使用表名或別名相匹配。

+2

當時只需刪除一個條件,和看看它何時執行沒有錯誤。然後你可以糾正它。 (提示:where thr.thr_hold_type ='2420135'...移到子查詢中。) – jarlh

+0

從外部選擇中不可見thr表。您應該將條件放在從此表中選擇的查詢 – Aaron

+0

Jarlh只有一個條件,並且該列來自多個內部聯接,並且我使用了最新的lhr別名但仍然失敗。 –

回答

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) 
+0

謝謝我已經更新了我的代碼內連接有多個條件,我應該使用該特定列上的條件.. –

+0

謝謝解決它.... –

+0

很高興聽到你已經得到它! – jarlh