使用MS Access 2010数据库显示余额的SQL查询 - 表单中有表单

时间:2018-05-18 20:55:04

标签: sql ms-access-2010

我写的以下查询工作正常,并显示我从预算金额支付的金额总和。但它没有表现出余额。我非常感谢您帮助确定显示余额的逻辑/查询。

SELECT DISTINCTROW tblInvoices.FY, tblInvoices.ContractNumber, 
       Sum(tblInvoices.AmountPaid) AS [Sum Of AmountPaid]
FROM tblInvoices
GROUP BY tblInvoices.FY, tblInvoices.ContractNumber
HAVING (((tblInvoices.FY)=[which FY]));

请参阅附带的屏幕截图以供参考。表格中有一个表格。当我单击按钮(YTD Spent)时,表单将显示并显示有关(金额总和),(预算)的信息,但不显示余额文本框中的(余额=预算 - 金额总和)。 enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

在这里猜测因为不知道数据库表结构是什么

select b.*, a.budget-b.[Sum Of AmountPaid] from (SELECT DISTINCTROW tblInvoices.FY, tblInvoices.ContractNumber, 
       Sum(tblInvoices.AmountPaid) AS [Sum Of AmountPaid]
FROM tblbudget
GROUP BY tblInvoices.FY, tblInvoices.ContractNumber
HAVING (((tblInvoices.FY)=[which FY])))a join
(
SELECT DISTINCTROW tblInvoices.FY, tblInvoices.ContractNumber, 
       Sum(tblInvoices.AmountPaid) AS [Sum Of AmountPaid]
FROM tblInvoices
GROUP BY tblInvoices.FY, tblInvoices.ContractNumber
HAVING (((tblInvoices.FY)=[which FY])))b
on a.fy=b.fy and a.contractnumber=b.contractnumber

答案 1 :(得分:0)

感谢@ Daniel的工作,基于我研究并编写了以下查询,完美无缺。 SELECT DISTINCTROW tblInvoices.FY,tblInvoices.ContractNumber,Sum(tblInvoices.AmountPaid)

AS [AmountPaid的总和],tblBudget.Budget,tblBudget.Budget-Sum(tblInvoices.AmountPaid)AS [Balance#]

FROM tblInvoices,tblBudget

其中tblInvoices.FY = tblBudget.FiscalYear和tblInvoices.ContractNumber = tblBudget.ContractNumber

GROUP BY tblInvoices.FY,tblInvoices.ContractNumber,tblBudget.Budget

HAVING(((tblInvoices.FY)= [哪个FY]));