简体   繁体   中英

Convert a SQL Server 2005 query to an Access 2003 query

I have a query that I can't get working in Access. I run the query in Access and I get

Syntax error. in query expression.

Something odd is going on here unless the error message is supposed to have a period in the middle of the sentence and a closing quote without an opening quote.

This is the query that works

select 
    CUS_CustomerID, CUS_CorpName, D.LastRevBy AS DeniedBy, 
    D.LastRevDate AS DeniedDate, S.LastRevBy AS ScreenBy, 
    S.LastRevDate AS ScreenDate, S.Comment AS Comments
from 
    (tblscreening S
inner join 
    Customer on CUS_CustomerID = S.PartyID)
inner join 
    tblscreening D on D.partyid = S.partyid 
                   and D.screennumber = (select min(screennumber) 
                                         from tblscreening 
                                         where partyid = S.partyid 
                                           and partytype = 'customer' 
                                           and deniedparty = 1
                                         group by partyid, partytype)
where 
    S.partytype = 'customer' and S.DeniedParty = 1
    and S.screennumber = (select max(screennumber) 
                          from tblscreening 
                          where partyid = S.partyID and partytype = 'customer'
                          group by partyid, partytype)
order by 
    S.partyid

Here is some sample data for a single customer. The report will actually produce data from multiple customers.

PartyID PartyType   ScreenNumber    DeniedParty LastRevBy   LastRevDate             Comment
794020  Customer    0               0           827         2007-07-12 13:47:45.000 R# 298479
794020  Customer    1               0           644         2007-08-10 10:48:48.000 RFQ/UPDATED CUSTOMER CARD
794020  Customer    2               0           827         2008-04-01 09:24:09.000 R# 311494
794020  Customer    3               0           827         2008-10-21 12:11:59.000 R# 317773
794020  Customer    4               0           827         2009-06-02 10:59:25.000 R# 324163
794020  Customer    5               0           644         2010-06-22 16:05:02.000 R-335656
794020  Customer    6               0           947         2013-02-04 10:45:53.357 New Inquiry (M8815/6-8)
794020  Customer    7               1           943         2016-04-26 10:07:41.143 Added to denied party
794020  Customer    8               1           944         2016-04-26 10:08:14.107 Verified denied party

The results for a single customer would be (I added quotes around partyid to improve legibility)

794020  ROCOM CORP. '943'   2016-04-26 10:07:41.143 '944'   2016-04-26 10:08:14.107 Verified denied party

This query works perfectly in SQL Server. I tried using the designer to create a self join. I then copied the syntax and came up with this

SELECT 
    S.PartyID, S.LastRevBy, S.LastRevDate, D.LastRevBy, D.LastRevDate
FROM 
    tblExDPScreen S, tblExDPScreen AS D
WHERE 
    D.screennumber = (SELECT MIN(screennumber) 
                      FROM tblExDpscreen 
                      WHERE D.partyid = partyid 
                        AND partytype = 'customer' AND deniedparty = 1
                      GROUP BY partyid, partytype)
    AND S.screennumber = (SELECT MAX(screennumber) 
                          FROM tblscreening 
                          WHERE S.partyid = partyID 
                            AND partytype = 'customer'
                          GROUP BY partyid, partytype)
    AND S.partytype = 'customer' 
    AND S.DeniedParty = 1

But even though this runs, it does not work at all. My only option right now is to normalize the table. But this will cause the report to be nonoperational for a longer time than I would prefer. I would like to get the report to work then decide if it is worth the work to break up the table.

Any suggestions?

SELECT 
    S.PartyID, S.LastRevBy, S.LastRevDate, D.LastRevBy, D.LastRevDate
FROM 
    tblExDPScreen S
INNER JOIN tblExDPScreen AS D ON d.screennumber = (SELECT MIN(screennumber) 
                      FROM tblExDpscreen 
                      WHERE D.partyid = partyid 
                        AND partytype = 'customer' AND deniedparty = 1
                      GROUP BY partyid, partytype)
WHERE S.screennumber = (SELECT MAX(screennumber) 
                          FROM tblscreening 
                          WHERE S.partyid = partyID 
                            AND partytype = 'customer'
                          GROUP BY partyid, partytype)
    AND S.DeniedParty = 1

I've removed the second comparison to partytype. It might not be the final answer, but it's going to get close. Remove the table D altogether and see if you get an error.

I finally got it. Perhaps Access does not support the sub select in the join. Moving the sub selects to separate queries then using those queries to self join

qryScreeningLast:

SELECT 
    MAX(screennumber) AS LastScreenNumber, partyid, partytype
FROM 
    tblscreening 
GROUP BY 
    partyid, partytype

and qryScreeningDenied

SELECT 
    MIN(screennumber) AS DeniedScreenNumber, partyid, partytype
FROM 
    tblscreening 
WHERE 
    deniedparty=1
GROUP BY 
    partyid, partytype

And finally

SELECT 
    CUS_CustomerID, CUS_CorpName, S.PartyID, S.LastRevBy AS ScreenBy, S.LastRevDate AS ScreenDate, T.LastRevBy AS DeniedBy, T.LastRevDate AS DeniedDate, S.Comment
FROM 
    tblscreening AS S, qryScreeningLast AS L, qryScreeningDenied AS D, tblscreening AS T, Customer 
WHERE 
    (((Customer.CUS_CustomerID)=[S].[PartyID]) AND ((S.PartyID)=[L].[partyID] 
    AND (S.PartyID)=[D].[partyID]) AND ((S.PartyType)='customer' 
    AND (S.PartyType)=[L].[partytype] AND (S.PartyType)=[D].[partytype]) 
    AND ((S.DeniedParty)=1) AND ((S.ScreenNumber)=[LastScreenNumber]) 
    AND ((T.PartyID)=[D].[partyID]) AND ((T.PartyType)=[D].[partytype]) 
    AND ((T.ScreenNumber)=[DeniedScreenNumber])) 
ORDER BY 
    CUS_CorpName

As much as I dislike the "Access way" of doing things I must confess I do like the fact that the customer filter is only needed in one place. That is where my compliments end though. Thanks for all who helped..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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