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

时间:2015-01-09 20:29:29

标签: sql-server

我需要加入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

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以为表使用别名:

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,取决于哪个应该是您的数据来源。

答案 1 :(得分:1)

提供不同的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

`

答案 2 :(得分:1)

您仍然可以使用第二个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

请确保在您的选择中使用别名:P1.NAME

但我建议使用添加的连接条件的第一种方法。