将SQL Server 2005查询转换为Access 2003查询

时间:2016-04-27 19:02:31

标签: sql-server ms-access

我有一个查询,我无法在Access中工作。我在Access中运行查询,我得到了

  

语法错误。在查询表达式中。

除非错误消息假设句子中间有一段句子,而且没有开头的报价,否则会发生奇怪的事情。

这是可行的查询

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

以下是单个客户的一些示例数据。该报告实际上将产生来自多个客户的数据。

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

单个客户的结果将是(我在partyid周围添加引号以提高易读性)

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

此查询在SQL Server中完美运行。我尝试使用设计器来创建自联接。然后我复制了语法并提出了这个

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

但即使这样,它根本不起作用。我现在唯一的选择是规范化表格。但这会导致报告的非运作时间比我想要的时间长。我想让报告工作,然后决定是否值得开展工作。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

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

我已将第二次比较删除为partytype。这可能不是最终答案,但它会接近尾声。完全删除表D,看看是否收到错误。

答案 1 :(得分:0)

我终于明白了。也许Access不支持连接中的子选择。移动子选择分离查询,然后使用这些查询进行自联接

qryScreeningLast:

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

和qryScreeningDenied

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

最后

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

尽管我不喜欢"访问方式"做事我必须承认我确实喜欢客户过滤器只需要在一个地方。这是我的赞美结束的地方。感谢所有帮助过的人..