Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error (missing operator) in query expression in MS Access

Tags:

sql

ms-access

I'm getting the error:

syntax error (missing operator) in query expression '((dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id) AND (dbo_tblSale.sale_id IN (319))) LEFT JOIN
                  dbo_tblItem ON ((dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id) AND (dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number)'.

I've had this issue before and know that it doesn't like my parenthesis setup, but nothing I do seems to make MS Access happy.

SELECT     dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                      dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                      dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium) 
                      AS SumOfitem_premium, dbo_tblBidder.bidder_type, dbo_tblSale.sale_id
FROM         (dbo_tblMailList LEFT JOIN
                      dbo_tblBidder ON ((dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id) AND (dbo_tblBidder.bidder_sale_id IN (319)))) LEFT JOIN
                      dbo_tblSale ON ((dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id) AND (dbo_tblSale.sale_id IN (319))) LEFT JOIN
                      dbo_tblItem ON ((dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id) AND (dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number))
GROUP BY dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                      dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                      dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, dbo_tblBidder.bidder_type, dbo_tblMailList.mail_Comp_Art, 
                      dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic, dbo_tblSale.sale_id
HAVING      (dbo_tblMailList.mail_Comp_GenAm = 1)

If anyone has any ideas, please let me know.

Thanks, James

EDIT

Using Gareth's FROM clause below I now have:

SELECT     dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                      dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                      dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium) 
                      AS SumOfitem_premium, dbo_tblBidder.bidder_type, dbo_tblSale.sale_id
FROM    (
            (   dbo_tblMailList 
                LEFT JOIN dbo_tblBidder 
                    ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id 
                    AND dbo_tblBidder.bidder_sale_id IN (319)
            )
            LEFT JOIN dbo_tblSale 
                ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
        )
        LEFT JOIN dbo_tblItem
            ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id 
            AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
GROUP BY dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                      dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                      dbo_tblMailList.mail_Comp_GenAm, dbo_tblBidder.bidder_number, dbo_tblBidder.bidder_type, dbo_tblMailList.mail_Comp_Art, 
                      dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic, dbo_tblSale.sale_id
HAVING      (dbo_tblMailList.mail_Comp_GenAm = 1)

But I am getting the error Extra ) in query expression ''.

I think it may have something to do with AND dbo_tblBidder.bidder_sale_id IN (319). If I change this to AND dbo_tblBidder.bidder_sale_id = 319, I get the error:

JOIN expression not supported.

And it highlights the dbo_tblBidder.bidder_sale_id = 319.

CONCLUSION

Here is my final working code:

SELECT     mail_ID, mail_FirstName, mail_LastName, mail_Address1, mail_Address2, 
                      mail_City, mail_State, mail_Zip, mail_Phone1, mail_Email1, 
                      mail_Comp_GenAm, SUM(SumOfitem_pr) AS SumOfitem_price, SUM(SumOfitem_premium) AS SumOfitem_premiums
FROM (
    SELECT     dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                          dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                          dbo_tblMailList.mail_Comp_GenAm, SUM(dbo_tblItem.item_pr) AS SumOfitem_pr, SUM(dbo_tblItem.item_premium) 
                          AS SumOfitem_premium
    FROM    (
                (   dbo_tblMailList 
                    LEFT JOIN dbo_tblBidder 
                        ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id 
                )
                LEFT JOIN dbo_tblSale 
                    ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
            )
            LEFT JOIN dbo_tblItem
                ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id 
                AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
    WHERE      dbo_tblMailList.mail_Comp_GenAm = 1 AND dbo_tblBidder.bidder_sale_id IN (319)
    GROUP BY dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                          dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                          dbo_tblMailList.mail_Comp_GenAm, dbo_tblMailList.mail_Comp_Art, 
                          dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic
    UNION
    SELECT     dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                          dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                          dbo_tblMailList.mail_Comp_GenAm, 0 AS SumOfitem_pr, 0 
                          AS SumOfitem_premium
    FROM    (
                (   dbo_tblMailList 
                    LEFT JOIN dbo_tblBidder 
                        ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id 
                )
                LEFT JOIN dbo_tblSale 
                    ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id
            )
            LEFT JOIN dbo_tblItem
                ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id 
                AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number
    WHERE      dbo_tblMailList.mail_Comp_GenAm = 1
    GROUP BY dbo_tblMailList.mail_ID, dbo_tblMailList.mail_FirstName, dbo_tblMailList.mail_LastName, dbo_tblMailList.mail_Address1, dbo_tblMailList.mail_Address2, 
                          dbo_tblMailList.mail_City, dbo_tblMailList.mail_State, dbo_tblMailList.mail_Zip, dbo_tblMailList.mail_Phone1, dbo_tblMailList.mail_Email1, 
                          dbo_tblMailList.mail_Comp_GenAm, dbo_tblMailList.mail_Comp_Art, 
                          dbo_tblMailList.mail_Comp_IndArt, dbo_tblMailList.mail_Comp_Fire, dbo_tblMailList.mail_Comp_Ceramic
    )
GROUP BY mail_ID, mail_FirstName, mail_LastName, mail_Address1, mail_Address2, 
                      mail_City, mail_State, mail_Zip, mail_Phone1, mail_Email1, 
                      mail_Comp_GenAm, SumOfitem_price, SumOfitem_premiums

Thanks Gareth!

like image 800
James Avatar asked Mar 20 '23 13:03

James


1 Answers

In access you cannot have multiple joins without separating them with parentheses, i.e.

SELECT  *
FROM    A
        INNER JOIN B
            ON A.ID = B.AID
        INNER JOIN C
            ON B.ID = C.BID;

Is not valid, it would need to be:

SELECT  *
FROM    (A
        INNER JOIN B
            ON A.ID = B.AID)
        INNER JOIN C
            ON B.ID = C.BID;

So, your from clause would need to be:

FROM    (
            (   dbo_tblMailList 
                LEFT JOIN dbo_tblBidder 
                    ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id 
                    AND dbo_tblBidder.bidder_sale_id IN (319)
            )
            LEFT JOIN dbo_tblSale 
                ON dbo_tblBidder.bidder_sale_id = dbo_tblSale.sale_id)
                AND dbo_tblSale.sale_id IN (319)
        )
        LEFT JOIN dbo_tblItem
            ON dbo_tblBidder.bidder_sale_id = dbo_tblItem.item_sale_id 
            AND dbo_tblBidder.bidder_number = dbo_tblItem.item_bidder_number

N.B I have removed all the unnecessary parentheses from the joins to reduce the clutter in the query (it is not necessary to enclose every predicate in parentheses), and exaggerated the tab indentations to show clearly where parentheses open and close


EDIT

I forgot, you cannot apply the constant expression in the JOIN clause in Access, you would need to create a subselect,

dbo_tblMailList 
LEFT JOIN dbo_tblBidder 
    ON dbo_tblMailList.mail_ID = dbo_tblBidder.bidder_mail_id 
    AND dbo_tblBidder.bidder_sale_id IN (319)

You would need to do

dbo_tblMailList AS m
LEFT JOIN (SELECT * FROM dbo_tblBidder WHERE bidder_sale_id IN (319)) AS b
    ON m.mail_ID = b.bidder_mail_id 

So your full query would be:

SELECT  m.mail_FirstName, 
        m.mail_LastName, 
        m.mail_Address1, 
        m.mail_Address2, 
        m.mail_City, 
        m.mail_State, 
        m.mail_Zip, 
        m.mail_Phone1, 
        m.mail_Email1, 
        m.mail_Comp_GenAm, 
        b.bidder_number, 
        SUM(i.item_pr) AS SumOfitem_pr, 
        SUM(i.item_premium) AS SumOfitem_premium, 
        b.bidder_type, 
        s.sale_id
FROM    (
            (   dbo_tblMailList AS m
                LEFT JOIN 
                (   SELECT  bidder_mail_id , bidder_number, bidder_type, bidder_sale_id
                    FROM    dbo_tblBidder 
                    WHERE   bidder_sale_id IN (319)
                ) AS b
                    ON m.mail_ID = b.bidder_mail_id 
            )
            LEFT JOIN dbo_tblSale AS s
                ON b.bidder_sale_id = s.sale_id
        )
        LEFT JOIN dbo_tblItem AS i
            ON b.bidder_sale_id = i.item_sale_id 
            AND b.bidder_number = i.item_bidder_number
GROUP BY 
        m.mail_FirstName, m.mail_LastName, m.mail_Address1, m.mail_Address2, m.mail_City, m.mail_State,
        m.mail_Zip, m.mail_Phone1, m.mail_Email1, m.mail_Comp_GenAm, b.bidder_number, b.bidder_type, s.sale_id;

(I have used short table aliases to try and condense the code, not a convention you have to follow)

like image 140
GarethD Avatar answered Mar 22 '23 19:03

GarethD