Using Delphi X10 & Absolute Database SQL92 Compatible. I am using this SQL script :
SELECT SUM (CREDIT) AS "SUM_CREDIT",
SUM (DEBIT) AS "SUM_DEBIT",
SUM (CREDIT) - SUM (DEBIT) AS Balance,
YearPay,
IDCustomer,
DescCredit
FROM CostumersPayments
GROUP BY YearPay, IDCustomer, DescCredit
HAVING (SUM (CREDIT) - SUM (DEBIT)) > 0
ORDER BY YearPay;
My table has about 3000 records; the returned result is about 300 records.
The problem is that I get for equal debit with equal credit, balance as 0
Result:
Credit | Debit | Balance
$ 4,56 | $ 4,56 | 0
$12,12 | $12,12 | 0
$21,6 | $21,6 | 0
.....
This happens for about 30 result records, the others result records are ok.
This is a strange behavior because I want HAVING (SUM (CREDIT) - SUM (DEBIT)) > 0
. So after searching a little I have found that when AS FLOAT
converting:
CAST(SUM (CREDIT) AS FLOAT) AS "SUM_CREDIT",
CAST(SUM (DEBIT) AS FLOAT) AS "SUM_DEBIT",
CAST(SUM (CREDIT) - SUM (DEBIT) AS FLOAT) AS "SUM_BALANCE"
The returned results for the 30 same records after the conversion are:
Credit | Debit | Balance
$ 4,56 | $ 4,56 | 8,88178419700125E-16
$12,12 | $12,12 | 1,77635683940025E-15
$21,6 | $21,60 | 3,5527136788005E-15
In most Balance result the values are 3,5527136788005E-15 and 7,105427357601E-15. The resulting 0's have values 0,000something. I have found the records and retype the debit & credit values. Same result.
The field types are Currency on the Table.
Where is the problem?
I don't know why it returns those values, but a quick fix around would be to discard all those garbage decimals when checking if CREDIT - DEBIT = 0.
SELECT SUM (CREDIT) AS "SUM_CREDIT",
SUM (DEBIT) AS "SUM_DEBIT",
SUM (CREDIT) - SUM (DEBIT) AS Balance,
YearPay,
IDCustomer,
DescCredit
FROM CostumersPayments
GROUP BY YearPay, IDCustomer, DescCredit
HAVING cast(SUM (CREDIT) - SUM (DEBIT) as numeric(18, 4)) > 0
ORDER BY YearPay;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.