Hi
Consider a financials system with tables account, payment, and receipt.
An account can have many payments and many receipts.
Any query that tries to select payments and receipts together will obtain a cross product of the payments and receipts for each account.
For example:
Code:
SELECT a.account_no, sum(payment_amt), sum(receipt_amt)
FROM account a, payment p, receipt r
WHERE a.account_no = p.account_no
AND a.account_no = r.account_no
GROUP BY a.account_no;
One way to get around this is
Code:
SELECT a.account_no, p.payment_total, r.receipt_total
FROM account a
,( SELECT account_no, sum(receipt_amt) as receipt_total
FROM receipt
GROUP BY account_no) r
,( SELECT account_no, sum(payment_amt) as payment_total
FROM payment
GROUP BY account_no) p
WHERE a.account_no = r.account_no
AND a.account_no = p.account_no
GROUP BY a.account_no
I am sure the relationship i am exposing is an example of a fan trap.
But how do i resolve them in my case ?
The way the system is documented makes me think like an account can have multiple receipts and multiple payments .
But how do i rearrange with the correct wordings to make sure i break the fan trap.
regards
db2hrishy