2011-12-29 18 views
1

我做的兩個內連接聯接:錯誤做2內時,在同一個查詢

use SalesDWH 
go 


select COUNT([specimen id]) as [count],[practice name],qlmlismapping.[mlis practice id],[practice code],[Requesting Physician] 
from quicklabdump a 
inner join qlmlismapping b 
on (b.[quiklab practice code] = a.[practice code]) 
inner join PracticeandPhysician c 
on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME) 
where [DATE entered] between '12/1/2011' and '12/31/2011' 
group by quicklabdump.[practice name],qlmlismapping.[mlis practice id],[practice code],[Requesting Physician] 
order by [count] desc 

,並收到此錯誤:

Msg 4104, Level 16, State 1, Line 10 
The multi-part identifier "quicklabdump.practice name" could not be bound. 
Msg 4104, Level 16, State 1, Line 10 
The multi-part identifier "qlmlismapping.mlis practice id" could not be bound. 
Msg 4104, Level 16, State 1, Line 3 
The multi-part identifier "qlmlismapping.mlis practice id" could not be bound. 

我在做什麼錯?連接是否不正確?

回答

4

使用相同的別名爲您的JOIN

select 
     COUNT([specimen id]) as [count], 
     [practice name], 
     b.[mlis practice id], 
     [practice code], 
     [Requesting Physician] 
from 
    quicklabdump a 
    inner join 
    qlmlismapping b on (b.[quiklab practice code] = a.[practice code]) 
    inner join 
    PracticeandPhysician c on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME) 
where 
    [DATE entered] between '12/1/2011' and '12/31/2011' 
group by 
    a.[practice name], 
    b.[mlis practice id], 
    [practice code], 
    [Requesting Physician] 
order by 
    [count] desc 

的一些注意事項雖然

  • 所有符合條件的列
  • 使用有意義的別名(QLD,QLM,PAP等)
  • 使用ISO日期yyyymmdd
  • 嘗試使用不含空格的列名稱
4

您不能在查詢的其餘部分引用qlmlismapping等,因爲您使用別名隱藏了它,因此它不是公開的相關名稱。

除了實際定義別名的位置外,您需要在別處使用別名名稱而不是基本表名稱。