繁体   English   中英

连接表达式中的MS Access 2007 SQL语法错误

[英]MS Access 2007 SQL Syntax Error in Join expression

我试图加入四个表,我收到错误'加入表达式中的语法错误'

请在下面找到我目前正在尝试编写的查询

SELECT a.*,
switch(a.[Start Date] between b.[SEASON1START] and b.[SEASON1END],b.[LRA_S1_RT1_SGL],
a.[Start Date] between c.[SEASON1START] and c.[SEASON1END],c.[LRA_S1_RT1_SGL],
a.[Start Date] between d.[SEASON1START] and d.[SEASON1END],d.[LRA_S1_RT1_SGL]) as [Negotiated Rate Local],
switch(a.[Start Date] between b.[SEASON1START] and b.[SEASON1END],
b.[RATE_CURR],a.[Start Date] between c.[SEASON1START] and c.[SEASON1END],c.[RATE_CURR],
a.[Start Date] between d.[SEASON1START] and d.[SEASON1END],d.[RATE_CURR]) as [Negotiated Currency]
FROM ((([Q1001 - Split Transactions] a 
left join [2014 Negotiated Rate] b on (a.[RX_ID] = b.[PROPCODE] And YEAR(a.[Start Date] = 2014))
left join [2015 Negotiated Rate] c on (a.[RX_ID] = c.[PROPCODE] And YEAR(a.[Start Date] = 2015)) 
left join [2016 Negotiated Rate] d on (a.[RX_ID] = d.[PROPCODE] And YEAR(a.[Start Date] = 2016)) ;

MS Access不允许on子句中的常量。 解决方案? 切换到更好的数据库。 等一下。 这并不总是一种选择。 所以,救援的子查询:

FROM ((([Q1001 - Split Transactions] a left join
        (SELECT b.*, 2014 as yyyy FROM [2014 Negotiated Rate] as b
        ) as b
        on a.[RX_ID] = b.[PROPCODE] And YEAR(a.[Start Date]) = b.yyyy
       ) left join
       (SELECT c.*, 2015 as yyyy FROM [2015 Negotiated Rate] as c
       ) as c
       on a.[RX_ID] = c.[PROPCODE] And YEAR(a.[Start Date]) = c.yyyy
      ) left join
      (SELECT d.*, 2016 as yyyy FROM [2016 Negotiated Rate] as d
      ) as d
      on a.[RX_ID] = d.[PROPCODE] And YEAR(a.[Start Date]) = d.yyyy
     ) 

您的原始查询应该有效。 在MS Access中,您可以在ON子句中使用表达式,例如WHERE子句(尽管复杂的表达式只能在SQL视图中的设计视图查看 )。

具体来说,您没有正确包装YEAR()函数。 考虑以下调整:

FROM ((([Q1001 - Split Transactions] a 
left join [2014 Negotiated Rate] b 
      on (a.[RX_ID] = b.[PROPCODE] And YEAR(a.[Start Date]) = 2014))
left join [2015 Negotiated Rate] c 
      on (a.[RX_ID] = c.[PROPCODE] And YEAR(a.[Start Date]) = 2015)) 
left join [2016 Negotiated Rate] d 
      on (a.[RX_ID] = d.[PROPCODE] And YEAR(a.[Start Date]) = 2016));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM