0

3つの異なるテーブルからデータを取得するSQLクエリを作成しようとしています。私の考えは、CTEを使用して2つのテーブルから情報を収集し、次に右結合を実行してデータを残りのクエリに追加することでした。各フィールドから異なるデータを取り込む必要がありますが、悪名高いエラーが発生していますmulti-part identifier could not be bound 。これまでに書いた内容は次のとおりです。

with cte1 as
(
SELECT          
[Physician ID] as InternalIdentifier, NPI, [Last Name] as LastName, [First Name]     
as     
FirstName,
Physician_T1HYCPP.Specialty, DOB, Degree, Department, [State License Number], [UPIN Number],    
[Physician Status]
FROM  Physician_T1HYCPP left outer JOIN PhysicianFile 
on Physician_T1HYCPP.[Physician ID] = PhysicianFile.[TSI MD Number]
where NPI <> ''
),
cteView
AS
(
Select [Doctor_Number], Address, City, State, Zip, Phone, Fax
from V_PhysicianList
)
Select 
InternalIdentifier, NPI, LastName, FirstName,
Specialty, DOB, Degree, Department, [State License Number], [UPIN Number],
[Physician Status],     
[Doctor_Number],
Address,City, State, Zip, Phone, Fax
from cte1
right outer join cteView on
V_PhysicianList.[Doctor_Number] = PhysicianFile.[Doctor Number]

具体的なエラーは次のとおりです。

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "V_PhysicianList.Doctor_Number" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PhysicianFile.Doctor Number" could not be bound.

ここでの目標は、最初のCTEの2つのテーブルのすべてのフィールドを取り込み、次に2番目のCTEのフィールドに「マージ」して、アドレスなどが最終結果セットで評価されるようにすることです。cte1とcteViewの両方のフィールドが正しくマージされていることを確認するにはどうすればよいですか?

4

1 に答える 1

2

最後のSELECTで、CTEから選択しますが、JOIN句でベーステーブルを参照します。正しいプレフィックスを使用してください。

Select 
InternalIdentifier, NPI, LastName, FirstName,
Specialty, DOB, Degree, Department, [State License Number], [UPIN Number],
[Physician Status],     
[Doctor_Number],
Address,City, State, Zip, Phone, Fax
from cte1
right outer join cteView on
cteView.[Doctor_Number] = cte1.[Doctor Number]

ただし、その列もcte1に含める必要があります。最終選択中、SQL Serverはそこに記載されているテーブル、ビュー、またはCTEにしかアクセスできないため、ベーステーブルへの参照を解決できません。

于 2012-11-02T22:23:48.793 に答える