Solved

special join between 3 tables

Posted on 2007-12-05
5
224 Views
Last Modified: 2010-03-20
I need a query to find an totals for items and writeins for each company.  I have an Order table, an Orderdetails table, and a OrderWritein table.  Both OrderDetails and OrderWriteIns have a Price and Quantiy column.  I wrote this with joins as follows:
Select C.CompanyName,sum(D.price*D.Quantity) itmAmt,sum (W.price*W.Quantity) wiAmt,max(O.OrderDate) [Last Order]
From tblOrders O
Inner Join tblCompany C on O.companyid = C.companyid
Inner Join tblOrdersDetail D on O.orderid=D.orderid
Inner Join tblOrdersWriteIns W on O.orderid=W.orderid
group by C.companyName

The problem is when I join the write in table it does a cross join.  if I have 10 items and 10 writeIns then the sum is for the 100 rows.  How do I write the query so each company has the correct sum for both itemsDetail and writeIns?
0
Comment
Question by:moraleskp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20416613
Select C.CompanyName,
 (select sum(D.price*D.Quantity) from tblOrdersDetail D where O.orderid=D.orderid) itmAmt,
 (select sum (W.price*W.Quantity) from tblOrdersWriteIns W where O.orderid=W.orderid) wiAmt,
 max(O.OrderDate) [Last Order]
From tblOrders O
Inner Join tblCompany C on O.companyid = C.companyid
group by C.companyName
0
 
LVL 25

Accepted Solution

by:
imitchie earned 125 total points
ID: 20416624
this will work better..
Select OC.CompanyName,
 (select sum(D.price*D.Quantity) from tblOrdersDetail D where OC.orderid=D.orderid) itmAmt,
 (select sum (W.price*W.Quantity) from tblOrdersWriteIns W where OC.orderid=W.orderid) wiAmt,
 OC.[Last Order]
From (
 select C.CompanyName, max(O.OrderDate) [Last Order]
 FROM tblOrders O
 Inner Join tblCompany C on O.companyid = C.companyid
 group by C.companyName
) OC

Open in new window

0
 

Author Comment

by:moraleskp
ID: 20417054
imitchie,
Thanks for responding.
on the first response I got an error saying orderid was not in the group by clause.
In the second response got an error that orderId is an invalid column.  I guess because it is not selected in the sub query.
Your responses gave me some ideas so I tried the following code.  It seems to work.  



select companyName,sum(itmAmt),sum(wiAmt),max(Orderdate)
from
(Select O.orderid,C.CompanyName,
(select sum(D.PriceSold*D.qty) from tblOrdersDetail D where O.orderid=D.orderid)itmAmt,
(select sum(W.RetailPrice*W.Qty) from tblOrders_Writeins W where O.orderid=W.orderid) wiAmt,
O.orderdate
from tblOrders O
inner join tblCompany C on O.companyid=C.companyid
Group by C.companyName,O.orderid,O.orderdate
) query
group by companyName
Order by CompanyName

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20417073
You are right. Good on you to find a solution! This would probably have worked, and does less work

Select OC.CompanyName,
 (select sum(D.price*D.Quantity) from tblOrdersDetail D where OC.orderid=D.orderid) itmAmt,
 (select sum (W.price*W.Quantity) from tblOrdersWriteIns W where OC.orderid=W.orderid) wiAmt,
 OC.[Last Order]
From (
 select O.OrderID, C.CompanyName, max(O.OrderDate) [Last Order]
 FROM tblOrders O
 Inner Join tblCompany C on O.companyid = C.companyid
 group by C.companyName
) OC
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20417077
nvm, orderid is not in the group by..
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question