General information
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
Multi Table View Problem
I am trying to create one view which sums orders and payments by customer. I have three tables; cust, orders and pay and they are linked by custid. All customers have at least one order and one payment.
I get incorrect results with the following code:
SELECT Cust.custid, Cust.custname, sum(orders.amt), sum(pay.payamt);
FROM cust ;
Inner Join orders on Cust.custid = Orders.custid ;
Inner Join pay on cust.custid = pay.custid;
GROUP BY Cust.custid
The following, however, does work correctly, but I want it in one view.
SELECT Cust.custid, Cust.custname, sum(orders.amt);
FROM cust ;
Inner Join orders on Cust.custid = Orders.custid ;
GROUP BY Cust.custid;
into Cursor temp
Select temp.*, sum(pay.payamt);
From Temp;
Inner Join pay on Temp.custid=pay.custid;
Group by Temp.custid
Can anyone explain what is going on and perhaps provide some help?
Thanks
Jay
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only