Hello, I have two queries that each of then giving me the right result but together something is wrong .
and you see the result below.
the first query sums all invoices for specific customer and the other sums the receipts.
now I'm substracting the invoice - receipts.
the first 3 columns give me the right results but the last column is giving me a wrong number .
it should give me 81250 - 83360.28 = -2110.68, (but I'm getting -85471.36) for some reason.
now when I'm connecting the 2 queries and the result.
adding the code here for convenient.
SELECT C.CUSTNAME, system.dbo.tabula_hebconvert(C.CUSTDES),
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(I.IVDATE), GETDATE()) < 30 THEN I.TOTPRICE - recipe.[30 Days] ELSE 0 END) AS '30 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(I.IVDATE), GETDATE()) BETWEEN 30 and 59 THEN I.TOTPRICE - recipe.[60 Days] ELSE 0 END) AS '60 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(I.IVDATE), GETDATE()) BETWEEN 60 and 89 THEN I.TOTPRICE - recipe.[90 Days] ELSE 0 END) '90 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(I.IVDATE), GETDATE()) > 89 THEN I.TOTPRICE - recipe.[more than 90 Days] ELSE 0 END) AS '> 90 Days'
FROM INVOICES I
INNER JOIN CUSTOMERS C ON I.CUST = C.CUST
LEFT JOIN(
SELECT C.CUSTNAME AS 'Customer number', system.dbo.tabula_hebconvert(C.CUSTDES) AS 'Customer name',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(IV.IVDATE), GETDATE()) < 30 THEN IV.TOTPRICE ELSE 0 END) AS '30 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(IV.IVDATE), GETDATE()) BETWEEN 30 and 59 THEN IV.TOTPRICE ELSE 0 END) AS '60 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(IV.IVDATE), GETDATE()) BETWEEN 60 and 89 THEN IV.TOTPRICE ELSE 0 END) AS '90 Days',
SUM(CASE WHEN DATEDIFF(DAY, system.dbo.tabula_dateconvert(IV.IVDATE), GETDATE()) > 89 THEN IV.TOTPRICE ELSE 0 END) AS 'more than 90 Days'
FROM CUSTOMERS C, INVOICES IV
WHERE IV.CUST = C.CUST
AND C.CUSTNAME = '100542'
AND IV.TYPE = 'T'
GROUP BY C.CUSTNAME, system.dbo.tabula_hebconvert(C.CUSTDES)
)recipe ON C.CUSTNAME = recipe.[Customer number]
WHERE C.CUSTNAME = '100542'
AND I.CUST = C.CUST
AND I.TYPE = 'F'
GROUP BY C.CUSTNAME, system.dbo.tabula_hebconvert(C.CUSTDES)