I have tables:
Customer (Id_Customer, Name, Address)
Receipt (Id_Customer, Money)
Payment (Id_Customer, Money)
Now, I want to show as below:
Name ------- Debit balance ------ In credit A ----------------------------------------30
B ----------------15-------------------------
C -----------------------------------------20
D -------------- 05-----------------------
Total DB: ----- 20-------------Total IC: 50
In that:
Note, only show Customer if Debit balance OR In credit is different from zero. And Totals over () for paging.
I run with QUERY, but ONLY show the Customers that also have 'money' in two tables Receipt and Payment (if customer only have money in Receipt OR Payment will don't show in result, WHY IS THAT? It CAN'T SHOW THAT cutomer???)
select a.*, Total_DebitBalance=sum(DebitBalance) over (), Total_InCredit=sum(InCredit) over () from (SELECT C.name, C.Address,
CASE WHEN SUM(isnull(R.Money, 0))< SUM(isnull(P.Money, 0)) THEN SUM(isnull(P.Money, 0)) - SUM(isnull(R.Money, 0)) END AS DebitBalance,
CASE WHEN SUM(isnull(R.Money, 0))> SUM(isnull(P.Money, 0)) THEN SUM(isnull(R.Money, 0)) - SUM(isnull(P.Money, 0)) END AS InCredit,
C.Id_Customer, COUNT(*) OVER () AS total_count
FROM Customer C LEFT JOIN Receipt R ON C.Id_Customer = R.Id_Customer LEFT JOIN Payment P ON C.Id_Customer = P.Id_Customer group by C.Id_Customer, C.name, C.Address)a ;
THIS IS RESULT:
Name ------- Debit balance ------ In credit
Please help me. Thanks a lot.