在Group Query中使用子句不起作用

时间:2016-06-13 17:44:08

标签: sql ms-access

在我的数据库中,我有两个表:tblReceiptstblDisclosure。一些商业细节:

  1. 有时我会收到钱,但没有披露申请。
  2. 有时我会在收到付款前处理披露信息。
  3. 以下查询旨在返回1和2的所有实例,但实际上并非如此。我没有记录,但我知道存在一个这样的实例。当我运行查询时,我收到了在参数Test中输入数据的请求。如果我删除HAVING子句,查询将正确返回所有记录。如何根据需要应用过滤器?

    SELECT
        Sum([DBSFee] + [MyFee]) AS Fee,
        [Amount] + [Adjustment] AS Rec,
        tblReceipts.ID,
        [Fee] - [Rec] AS Test,
        tblClient.ClientName,
        tblClient.ClientID,
        tblReceipts.Notes
    FROM
        (tblDisclosure
        INNER JOIN tblReceipts ON tblDisclosure.ReceiptsLookup = tblReceipts.ID)
        INNER JOIN tblClient ON tblReceipts.ClientLookup = tblClient.ClientID
    GROUP BY
        [Amount] + [Adjustment],
        tblReceipts.ID,
        tblClient.ClientName,
        tblClient.ClientID,
        tblReceipts.Notes
    HAVING Test <> 0;
    

1 个答案:

答案 0 :(得分:0)

尝试

Select Fee, Rec, ID, Test, ClientName, ClientID, Notes FROM (
SELECT Sum([DBSFee]+[MyFee]) AS Fee, [Amount]+[Adjustment] AS Rec, tblReceipts.ID, [Fee]-[Rec] AS Test, tblClient.ClientName, tblClient.ClientID, tblReceipts.Notes
FROM (tblDisclosure INNER JOIN tblReceipts ON tblDisclosure.ReceiptsLookup = tblReceipts.ID) INNER JOIN tblClient ON tblReceipts.ClientLookup = tblClient.ClientID
GROUP BY [Amount]+[Adjustment], tblReceipts.ID, tblClient.ClientName, tblClient.ClientID, tblReceipts.Notes) as T
WHERE Test<>0;