tags:

views:

143

answers:

2

I am trying to join 3 tables with the following sql statement

Select 
    Sum(OrderDetail_table.Price * orderDetail_table.quantity) as totalPrice,
    item_table.GLacct 
from 
    OrderDetail_table 
    left outer join order_table on 
        orderDetail_table.orderID = order_table.orderid 
    left outer join item_table on 
        item_table.itemID = orderDetail_table.itemID 
where 
    Order_table.invoiceDate = #01/31/2009# 
group by 
    item_table.glacct

The error message I am getting is:

"Syntax error(missing operator) in query expression 'orderDetail_table.OrderID = order_table.orderid left outer join item_table on item_table.itemID = orderDetail_table.itemID'."

Any help would be appreciated. Thanks

I want it always return the Sum of the orderDetail_table.price * orderDetail_table.quantity, there might not be an item_table.GLAcct for an orderDEtail_table.ItemID

A: 

As the OrderDetail table is always part of an Order, and each detail always references an Item, you can replace the LEFT OUTER JOIN by INNER JOINs. Especially, since the WHERE and GROUP BY clauses both reference the optional tables.

What exactly is the error message?

devio
A: 

Try brackets:

Select
    Sum(OrderDetail_table.Price * orderDetail_table.quantity) as totalPrice,
    item_table.GLacct 
from
     (OrderDetail_table
     left outer join order_table on
         orderDetail_table.orderID = order_table.orderid)
     left outer join item_table on
         item_table.itemID = orderDetail_table.itemID
where
     Order_table.invoiceDate = #01/31/2009#
group by
     item_table.glacct
Stanislav Kniazev
If you build your SQL in Access using the QBE grid, you'll get joins that are guaranteed to work (Jet SQL doesn't use OUTER, just LEFT or RIGHT, but it doesn't choke when it's in there). It's only when you write it by hand that you end up with these kinds of puzzles.
David-W-Fenton
Eye of the beholder stuff: I think ACE/Jet SQL does use OUTER but it doesn't choke when it's omitted.
onedaywhen
For fun, here's a strange example of ACE/JET not choking on an errant keyword (david_w_fenton) when it should: SELECT O1.OrderID, david_w_fenton (SELECT COUNT(*) FROM [Order Details] AS OD1 WHERE O1.OrderID = OD1.OrderID) FROM Orders AS O1;
onedaywhen