MS Access - 您尝试执行不包含指定的查询...作为聚合函数的一部分

时间:2014-05-22 16:21:03

标签: sql ms-access

我的聚合函数在Microsoft Access 2010中给出了这个错误。这是有问题的查询:

SELECT
    curr.FileDate, curr.ProdNr, 
    Count(curr.ProdNr) as CountOfProdNr, 
    curr.Desc, 
    IIf(prev.ProdNr is Null, IIf(curr.Status="1", "NEW", "ERR"), 
    IIf(curr.Status<>prev.Status, 
    Switch(
    curr.Status="7", "CAN", 
    curr.Status="T", "TRM", 
    True, "OTH"), 
    IIf(curr.Status="1", "REG", "HIS"))) as Transaction, 
    curr.Status, 
    prev.Status as PrevStatus, 
    min(curr.StartDate) as MinOfStartDate, 
    max(curr.EndDate) as MaxOfEndDate, 
    curr.CancelCode, 
    max(curr.OrigAmt) as MaxOfOrigAmt,
    Switch(curr.Desc="ML", IIf(MaxOfOrigAmt>500000, 500000, MaxOfOrigAmt), 
    curr.Desc="MC", IIf(MaxOfOrigAmt>300000, 300000, MaxOfOrigAmt), 
    True, MaxOfOrigAmt) as Cap, 
    Switch(prev.Desc="ML", IIf(max(prev.OrigAmt)>500000, 500000, max(prev.OrigAmt)), 
    prev.Desc="MC", IIf(max(prev.OrigAmt)>300000, 300000, max(prev.OrigAmt)), 
    True, max(prev.OrigAmt)) as PrevCap, 
    Switch(curr.Desc="ML", IIf(max(curr.Balance)>500000, 500000, max(curr.Balance)), 
    curr.Desc="MC", IIf(max(curr.Balance)>300000, 300000, max(curr.Balance)), 
    True, max(curr.Balance)) as BalCap, 
    Switch(prev.Desc="ML", IIf(max(prev.Balance)>500000, 500000, max(prev.Balance)), 
    prev.Desc="MC", IIf(max(prev.Balance)>300000, 300000, max(prev.Balance)), 
    prev.Desc Is Null, 0, 
    True, max(prev.Balance)) as PrevBalCap, 
    CDbl(BalCap-PrevBalCap) as BalCapChange, 
    curr.JointIndicator, curr.OrigAmt, 
    max(curr.Balance)-Nz(max(prev.Balance), 0) as BalanceChange
FROM 
    Cov as curr 
    LEFT JOIN Cov_PREVMONTH as prev 
    ON curr.Ref = prev.Ref
GROUP BY
    curr.FileDate, 
    curr.Desc, 
    curr.ProdNr, 
    curr.Status, 
    prev.Status, 
    IIf(prev.ProdNr is Null, IIf(curr.Status="1", "NEW", "ERR"), 
    IIf(curr.Status<>prev.Status, 
    Switch(
    curr.Status="7", "CAN", 
    curr.Status="T", "TRM", 
    True, "OTH"), 
    IIf(curr.Status="1", "REG", "HIS"))),
    curr.CancelCode, 
    curr.JointIndicator, 
    curr.OrigAmt
请原谅我糟糕的编码。此表达式出现错误:

Switch(prev.Desc="ML", IIf(max(prev.OrigAmt)>500000, 500000, max(prev.OrigAmt)), 
prev.Desc="MC", IIf(max(prev.OrigAmt)>300000, 300000, max(prev.OrigAmt)), 
True, max(prev.OrigAmt))

当我尝试绕过错误并将此表达式置于GROUP BY子句下时,我收到错误

  

GROUP BY子句中不能有聚合函数

我没有想法 - 这里有什么不对?

谢谢。

0 个答案:

没有答案