I want to compare table 1 and table 2 only once. I want to print the same amount as the wrong amount or the data error on each side.
1.table name : hospital_payment_
I think this is what you are after. Let me know if you need something else. You should probably have chart_num in credit_card so you can join the tables together.
Select hpd.chart_num,
hpd.chart_name
hpd.visit,
hpd.card_amount_received,
cc.card_date,
CASE WHEN hpd.card_amount_received = SUM(cc.advenced_amount) THEN hpd.card_amount_received ELSE NULL END AS ‘amount’,
CASE WHEN hpd.card_amount_received > SUM(cc.advenced_amount) THEN ‘error’ ELSE ‘ok’ END AS ‘result’
from hospital_payment_data hpd
LEFT JOIN credit_card ON cc.chart_num=hpd.chart_num AND
date(cc.card_date) = date(hpd.visit)
GROUP BY hpd.chart_num, hpd.visit;
It would probably be a good idea to have a client_visit table in case a client comes to the office twice in a day or sets up a payment plan where they aren’t paying on the day they come to the office.