cancel
Showing results for 
Search instead for 
Did you mean: 

Strange SELECT behaviour

gchg
Explorer
0 Kudos
147

If I use this statement

SELECT SUM(Debit) - SUM(Credit) FROM A_Purchase_Ledger WHERE Supplier_ID = 16

It returns 0 as the sum of debit is 250 and the sum of credit is 250. However, in this...

SELECT A_Purchase_Ledger.Supplier_ID AS 'SupplierID', 
A_Purchase_Ledger.Credit AS 'Credit', 
A_Purchase_Ledger.Document_Date AS 'DocDate', 
A_Purchase_Ledger.P_Description As 'DocDesc', 
A_Purchase_Ledger.Document_No AS 'DocNumber', 
Suppliers.Bank_BankName AS 'BankName', 
Suppliers.Bank_RoutingNumber AS 'RoutingNumber', 
Suppliers.Bank_AccountName AS 'AccountNumber', 
Suppliers.Bank_AccountName AS 'AccountName', 
Suppliers.Due_Days AS 'DueDays', 
(SELECT(SUM(A_Purchase_Ledger.Credit) - SUM(A_Purchase_Ledger.Debit))WHERE A_Purchase_Ledger.Supplier_ID = SupplierID) as 'TotalBalance'
FROM A_Purchase_Ledger 
INNER JOIN Suppliers ON A_Purchase_Ledger.Supplier_ID = Suppliers.Supplier_ID 
WHERE A_Purchase_Ledger.Paid = 'N' AND A_Purchase_Ledger.Hold = 0 AND 
A_Purchase_Ledger.eCheck_Pending = 0 AND A_Purchase_Ledger.eSig_Required = 0 
AND Type = 'PI' AND dateadd(day,Suppliers.Due_Days, A_Purchase_Ledger.Document_Date) <= current date
GROUP BY A_Purchase_Ledger.Supplier_ID, Credit, Document_Date, P_Description, Document_No, Bank_BankName, Bank_RoutingNumber, Bank_BankName, Bank_AccountName, Due_Days

It returns 250.00. Any ideas what the heck I am doing wrong?

Thanks

View Entire Topic
gchg
Explorer
0 Kudos

Sometimes after you have stared at something for hours nothing pops up until you ask for help

(SELECT SUM(A_Purchase_Ledger.Credit)- SUM(A_Purchase_Ledger.Debit)FROM A_Purchase_Ledger WHERE A_Purchase_Ledger.Supplier_ID = SupplierID) as 'TotalBalance'

 

VolkerBarth
Contributor
0 Kudos
Hm, so what's the answer - just that I don't have to stare myself for hours, too...?
gchg
Explorer
Didn't add FROM A_Purchase_Ledger! Hey Volker what happened to the new 'unofficial' SQL Anywhere site?
VolkerBarth
Contributor
0 Kudos
Well, as to the "other" forum, I don't know (and have not yet asked Justin), feel free to ask that as a separate question...