1

I am trying to join four tables and I am getting the error 'Syntax error in Join Expression'

Please find below the query I am presently trying to write

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)) ;

2 Answers 2

2

MS Access does not permit constants in the on clause. The solution? Switch to a better database. Oh wait. That isn't always an option. So, subqueries to the rescue:

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
     ) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, however I am still getting the same error even after replacing the code as per your suggestion.
"Switch to a better database" too honest :)
@AyanMukherje . . . I thought a subquery would do it. Perhaps a view is needed.
Well it worked after I made a couple of edits and checked the brackets. Thanks for your help Gordon :). I am new to this community and I've a couple of questions, any way we can connect and discuss them?
1

Your original query should work. In MS Access, you can have expressions in ON clauses like WHERE clauses (though intricate expressions will not be viewable in Design View only in SQL View).

Specifically, you are not wrapping the YEAR() function properly. Consider the following adjustment:

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.