我的要求是使用{{1来验证Column2
的总计或总和(Column1
分组)是否应等于Column3
(Column1
分组) }}。
举个例子,
MS Access
总计(DebitAmount
分组)应等于AccountNumber
(对于相同的帐号)。即,AccountNumber ='129610896'的BillAmount
(或DebitAmount
)总和应等于30000
同样,AccountNumber ='184046309'的SUM(DebitAmount)
(或DebitAmount
)总和应等于19000
SUM(DebitAmount)
我写了一个查询如下
AccountNumber Code DebitAmount BillAmount
129610896 B2B001 25000 30000
129610896 B2B032 5000 30000
184046309 B2B001 15830 19000
184046309 B2B032 3170 19000
结果仅按帐号显示记录组。如果我有50条记录,则按帐号分组只显示20条记录。
但我希望显示所有记录,如果借方金额等于账单金额,则IsVerify列应显示值“1”。如果我在查询中包含“代码”列,则会显示所有记录,但显示的结果无效。
SELECT AccountNumber, BillAmount, iif ((Sum(DebitAmount) - BillAmount) = 0,1,0) as IsVerify
FROM Master_Table GROUP BY AccountNumber,BillAmount
AccountNumber BillAmount IsVerify
129610896 30000 1
184046309 19000 1
那么如何编写查询以显示所有显示验证标志的记录,如下所示。
SELECT Code,AccountNumber, BillAmount, iif ((Sum(DebitAmount) - BillAmount) = 0,1,0) as IsVerify FROM Master_Table GROUP BY Code,AccountNumber,BillAmount
答案 0 :(得分:0)
SELECT tm.*,
IIF(
(
SELECT Sum(t.DebitAmount)
FROM Master_Table t
WHERE t.AccountNumber = tm.AccountNumber
) = tm.BillAmount, 1, 0
)
as IsVerify
FROM Master_Table tm
对不起,如果有一些小错误,因为我在mysql和t-sql上工作。但逻辑是正确的。
答案 1 :(得分:0)
你快到了! :)试试这个:
SELECT mt.AccountNumber, mt.GLCode, mt.DebitAmount, mt.BillAmount, IIf(t1.SumOfDebitAmount - mt.BillAmount = 0,1,0) AS IsVerify
FROM Master_Table AS mt INNER JOIN
(SELECT AccountNumber, Sum(DebitAmount) AS SumOfDebitAmount
FROM Master_Table
GROUP BY AccountNumber) AS t1
ON mt.AccountNumber = t1.AccountNumber
ORDER BY mt.AccountNumber, mt.GLCode;
我们在派生表中获取DebitAmount总计,然后在帐号上加入。如果DebitAmount减去BillAmount的总数为0,则验证代表DebitAmounts的行。请注意声明存在问题:
IIf(t1.SumOfDebitAmount = mt.BillAmount,1,0)
它一直显示#Error。不确定为什么,真的,但我无法这样做。无论如何,这应该适合你。