MS Access SQL多个JOIN和WHERE子句

时间:2014-02-26 18:32:11

标签: sql ms-access join where parentheses

我试图将多个表连接到名为“Claims”的主表(下面的代码) 三个表具有以下条件

  1. [纽约州医疗补助计划收费表Jan12]有重复的密钥,必须选择正确的记录,其中主表中的值介于查找表中的两个日期之间
  2. [纽约州医疗补助计划收费表Apr13]有重复的密钥,必须选择正确的记录,其中主表中的值介于查找表中的两个日期之间
  3. [NY Medicare]有重复的密钥,必须将“区域”作为第二级别来选择正确的记录
  4. 当我只使用一个WHERE子句时,查询有效并且没有问题。为单独的表添加1或两个额外的WHERE条件会导致麻烦。

    我在互联网上研究了很多,发现括号对ACCESS很重要。我看到了关于多个连接的正确语法的示例,但没有关于特定表的WHERE子句的多个连接。

    我希望我只是误解了如何放置括号。


    FROM ( ( ( ( ( ( [Claims]  
    LEFT JOIN [BillType] ON [Claims].[Bill_Type] = [BillType].[BillType_Bill Type Key] )  
    LEFT JOIN [PlaceofService] ON [Claims].[Place_of_Service] = [PlaceofService].[POS_Place of Service] )  
    LEFT JOIN [Participating] ON [Claims].[TIN] = [Participating].[TIN] )  
    LEFT JOIN [NYS Medicaid Fee Schedule Apr11] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Apr11].[Apr11_NYS_CODE] )  
    LEFT JOIN [NYS Medicaid Fee Schedule Jan12] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_CODE]  
      WHERE [Claims].[BeginningDOS] BETWEEN [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_EFFECTIVE DATE] AND [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_END DATE] )  
    LEFT JOIN [NYS Medicaid Fee Schedule Apr13] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_CODE]    
      WHERE [Claims].[BeginningDOS] BETWEEN [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_EFFECTIVE DATE] AND [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_END DATE] )  
    LEFT JOIN [NY Medicare] ON [Claims].[FivedigitProcCode] = [NY Medicare].[MEDICARE_HCPCS CODE]  
      WHERE [Claims].[Zone] = [NY Medicare].[MEDICARE_ZONE]  
    

1 个答案:

答案 0 :(得分:1)

SQL查询只有一个where子句。您应该将这些条件更改为on条件的一部分:

FROM ( ( ( ( ( ( [Claims]  
LEFT JOIN [BillType] ON [Claims].[Bill_Type] = [BillType].[BillType_Bill Type Key] )  
LEFT JOIN [PlaceofService] ON [Claims].[Place_of_Service] = [PlaceofService].[POS_Place of Service] )  
LEFT JOIN [Participating] ON [Claims].[TIN] = [Participating].[TIN] )  
LEFT JOIN [NYS Medicaid Fee Schedule Apr11] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Apr11].[Apr11_NYS_CODE] )  
LEFT JOIN [NYS Medicaid Fee Schedule Jan12] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_CODE]  
  AND [Claims].[BeginningDOS] BETWEEN [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_EFFECTIVE DATE] AND [NYS Medicaid Fee Schedule Jan12].[Jan12_NYS_END DATE] )  
LEFT JOIN [NYS Medicaid Fee Schedule Apr13] ON [Claims].[FivedigitProcCode] = [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_CODE]    
  AND [Claims].[BeginningDOS] BETWEEN [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_EFFECTIVE DATE] AND [NYS Medicaid Fee Schedule Apr13].[Apr13_NYS_END DATE] )  
LEFT JOIN [NY Medicare] ON [Claims].[FivedigitProcCode] = [NY Medicare].[MEDICARE_HCPCS CODE]  
  AND [Claims].[Zone] = [NY Medicare].[MEDICARE_ZONE]