-1

This is my SQL Query

SELECT m.Bldg, 
SUM(e.TotCost * SWITCH(e.Utility='E', 1, true, 0)) AS ECost, 
SUM(g.TotCost * SWITCH(e.Utility='G', 1, true, 0)) AS GCost, 
SUM(h.TotCost * SWITCH(e.Utility='H', 1, true, 0)) AS HCost, 
SUM(c.TotCost * SWITCH(e.Utility='C', 1, true, 0)) AS CCost, 
SUM(w.TotCost * SWITCH(e.Utility='W', 1, true, 0)) AS WCost, 
SUM(s.TotCost * SWITCH(e.Utility='S', 1, true, 0)) AS SCost
FROM tblBldgMeters m
LEFT JOIN qryMtrHistory e ON m.EMeter=e.Meter
LEFT JOIN qryMtrHistory g ON m.GMeter=g.Meter
LEFT JOIN qryMtrHistory h ON m.HMeter=h.Meter
LEFT JOIN qryMtrHistory c ON m.CMeter=C.Meter
LEFT JOIN qryMtrHistory w ON m.WMeter=W.Meter
LEFT JOIN qryMtrHistory s ON m.SMeter=S.Meter
GROUP BY m.Bldg

I am getting this error message: enter image description here

When I get out of the message, this part is highlighted "UM(e." from the very first line of the query. I think the error is that I need more parentheses around the LEFT JOINS, but I'm not sure how to format them. If anyone can help, I would appreciate it. I'm assuming Access is throwing me an error about me trying something that is legal in SQL but not in access, but I'm not sure how to fix it. Thank you!

3
  • 2
    According to this: msdn.microsoft.com/en-us/library/bb208894%28v=office.12%29.aspx Access doesn't have explicit "OUTER" keyword. "LEFT JOIN" is implicitly an outer join. Could you try to drop "OUTER" from your query? Commented May 7, 2015 at 19:42
  • I think that's also necessary, however that alone didn't fix it :( Commented May 7, 2015 at 19:44
  • 1
    LEFT OUTER JOIN vs. LEFT JOIN is not a problem; Access accepts either. You need parentheses in the FROM clause. See Multiple INNER JOIN SQL ACCESS Beware Access can be finicky with outer joins, so adding parentheses may give you a different error. Commented May 7, 2015 at 19:49

1 Answer 1

3

Try this(EDITED), using parenthesis :

 SELECT m.Bldg, 
 SUM(e.TotCost * SWITCH(e.Utility='E', 1, true, 0)) AS ECost, 
 SUM(g.TotCost * SWITCH(e.Utility='G', 1, true, 0)) AS GCost, 
 SUM(h.TotCost * SWITCH(e.Utility='H', 1, true, 0)) AS HCost, 
 SUM(c.TotCost * SWITCH(e.Utility='C', 1, true, 0)) AS CCost, 
 SUM(w.TotCost * SWITCH(e.Utility='W', 1, true, 0)) AS WCost, 
 SUM(s.TotCost * SWITCH(e.Utility='S', 1, true, 0)) AS SCost
 FROM 
 ((((
 (tblBldgMeters m LEFT JOIN qryMtrHistory e ON (m.EMeter=e.Meter) )
   LEFT JOIN qryMtrHistory g ON (m.GMeter=g.Meter)
  )
    LEFT  JOIN qryMtrHistory h ON (m.HMeter=h.Meter)
  )
    LEFT JOIN qryMtrHistory c ON (m.CMeter=C.Meter)
  )
    LEFT JOIN qryMtrHistory w ON (m.WMeter=W.Meter)
  )
    LEFT JOIN qryMtrHistory s ON (m.SMeter=S.Meter)

  GROUP BY m.Bldg ;

Cause of the problem was :

The Simple Query Wizard adds an extra set of parentheses in the join expression of the SQL statement that it creates for the query.

The error occurs if your query meets both of the following conditions: If your query is based on two or more tables. OR If two of the tables contain a relationship or join based on a three-field primary key.

Take a look at this : https://support.microsoft.com/en-us/kb/207868

Sign up to request clarification or add additional context in comments.

2 Comments

When I try running that, it processes for a second like it's going to work, but then gives the error "Microsoft Access can't represent the join expression m.EMeter=e.Meter in Design view
And if I try to just view, it asks for a parameter value for m.EMeter

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.