SQL - 信用借记和余额

时间:2016-08-26 09:00:42

标签: sql absolute-db

我正在使用带有绝对数据库的Delphi(SQL92)我想在SQL查询中计算以下内容:

Year | Description | Credit | Debit | Balance
2016 | Car Service |    100 |     0 |      100

我希望按年度客户和描述搜索到期金额超过0(> 0)的所有客户

我正在使用这个脚本,但我在结果中也将结果数量设为0:

SELECT 
SUM(CT1.CREDIT) AS "SUM_CREDIT", 
SUM(CT1.DEBIT) As "SUM_DEBIT", 
SUM(SUM(CT1.CREDIT)- SUM(CT1.DEBIT)) As Balance,
CT1.YearPay, CT1.IDCustomer, CT1.DescCredit

FROM "CustomersPayments" CT1 

GROUP BY CT1.YearPay, CT1.IDCustomer, CT1.DescCredit

HAVING (SUM(CT1.CREDIT) - SUM(CT1.DEBIT)) > 0

ORDER BY CT1.YearPay

我做错了什么?

1 个答案:

答案 0 :(得分:0)

SELECT
  SUM(CT1.CREDIT)          AS "SUM_CREDIT",
  SUM(CT1.DEBIT)           AS "SUM_DEBIT",
  (SUM_CREDIT - SUM_DEBIT) AS Balance,
  CT1.YearPay,
  CT1.IDCustomer,
  CT1.DescCredit
FROM "CustomersPayments" CT1
GROUP BY CT1.YearPay, CT1.IDCustomer, CT1.DescCredit
HAVING (SUM(CT1.CREDIT) - SUM(CT1.DEBIT)) > 0
ORDER BY CT1.YearPay