如何在此查询中使用相关名称

How to use correlation names in this query

我需要加入 PROVIDERS 两次。使用相关名称是否有效,我如何在这里使用它?如果我的问题中遗漏了任何重要信息,请告诉我。

SELECT PROVIDERS.NAME
     , PROVIDERS.CITY
     , PROVIDERS.STATE
     , PROVIDERS.ZIP
     , NPI.[Provider First Line Business Practice Location Address] as "ADDRESS"
     , PostalCodes.County                                      
FROM Taxonomy
INNER JOIN       NPI_Taxonomy ON Taxonomy.[Taxonomy Code] = NPI_Taxonomy.[Healthcare Provider Taxonomy Code] 
RIGHT OUTER JOIN PROVIDERS    ON NPI_Taxonomy.NPI = PROVIDERS.FACILITYID
RIGHT OUTER JOIN npi          ON PROVIDERS.FACILITYID = NPI.NPI
LEFT OUTER JOIN  PROVIDERS    ON PostalCodes.Code = PROVIDERS.ZIP 
WHERE     (1=1)
AND (PROVIDERS.STATE = 'AL')
AND ([Taxonomy Specialization] like 'general acute care hospital')
order by PROVIDERS.NAME

您可以为表使用别名:

SELECT  PROVIDERS.NAME ,
        PROVIDERS.CITY ,
        PROVIDERS.STATE ,
        PROVIDERS.ZIP ,
        NPI.[Provider First Line Business Practice Location Address] AS "ADDRESS" ,
        PostalCodes.County
FROM    Taxonomy
        INNER JOIN NPI_Taxonomy ON Taxonomy.[Taxonomy Code] = NPI_Taxonomy.[Healthcare Provider Taxonomy Code]
        RIGHT OUTER JOIN PROVIDERS p1 ON NPI_Taxonomy.NPI = p1.FACILITYID
        RIGHT OUTER JOIN npi ON p1.FACILITYID = NPI.NPI
        LEFT OUTER JOIN PROVIDERS p2 ON PostalCodes.Code = p2.ZIP
WHERE   ( 1 = 1 )
        AND ( PROVIDERS.STATE = 'AL' )
        AND ( [Taxonomy Specialization] LIKE 'general acute care hospital' )
ORDER BY PROVIDERS.NAME

您需要更改每个 PROVIDERS。至 p1 或 p2,具体取决于您的数据来源。

给予不同alias name

SELECT P1.NAME, 
       P1.CITY, 
       P1.STATE,
       P1.ZIP, 
       NPI.[Provider First Line Business Practice Location Address] as "ADDRESS", 
       PostalCodes.County                                      
FROM Taxonomy
     INNER JOIN NPI_Taxonomy 
     ON Taxonomy.[Taxonomy Code] = NPI_Taxonomy.[Healthcare Provider Taxonomy Code] 
     RIGHT OUTER JOIN PROVIDERS P1 ON NPI_Taxonomy.NPI = P1.FACILITYID
     RIGHT OUTER JOIN npi ON P1.FACILITYID = NPI.NPI
     LEFT OUTER JOIN PostalCodes ON PostalCodes.Code = P1.ZIP 
WHERE     (1=1)
AND (P1.STATE = 'AL')
AND ([Taxonomy Specialization] like 'general acute care hospital')
order by P1.NAME

`

您仍然可以使用第二个 PROVIDERS 加入条件:

SELECT PROVIDERS.NAME, PROVIDERS.CITY, PROVIDERS.STATE, PROVIDERS.ZIP, NPI.[Provider First Line Business Practice Location Address] as "ADDRESS", PostalCodes.County                                      
FROM Taxonomy
     INNER JOIN NPI_Taxonomy ON Taxonomy.[Taxonomy Code] = NPI_Taxonomy.[Healthcare Provider Taxonomy Code] 

     --Notice the change

RIGHT OUTER JOIN PROVIDERS ON NPI_Taxonomy.NPI = PROVIDERS.FACILITYID and PROVIDERS.ZIP = PostalCodes.Code
         RIGHT OUTER JOIN npi ON PROVIDERS.FACILITYID = NPI.NPI


WHERE     (1=1)
AND (PROVIDERS.STATE = 'AL')
AND ([Taxonomy Specialization] like 'general acute care hospital')
order by PROVIDERS.NAME`

或者,如果您真的想要第二次加入,您可以使用别名。

RIGHT OUTER JOIN PROVIDERS P ON NPI_Taxonomy.NPI = P.FACILITYID 

LEFT OUTER JOIN PROVIDERS P1 on  PostalCodes.Code =  P1.ZIP

只需确保在 select 中使用别名:P1.NAME

但我会推荐第一种方法,添加了连接条件。