Using this query, I get the result I need of how many orders each customer has placed. However, when I use the SUM(OrderLine.ActualPrice) in the SELECT field and use an inner join to link the Order table to the OrderLine table which contains the price. The COUNT results are skewed, as the OrderLine table contains multiple OrderNo of the same Order, since one order can contain multiple products.
How do I keep the COUNT to the result I have with this query, while putting in the SUM query for the Total Purchase Amount of their orders?
SELECT C.custno,
companyname,
Count(customerorder.orderno)AS 'Total Orders',
FROM customer C
INNER JOIN customerorder
ON customerorder.custno = C.custno
GROUP BY C.custno,
companyname
MODIFIED QUERY WITH INCORRECT RESULT
SELECT C.custno,
companyname,
Count(customerorder.orderno)AS 'Total Orders',
Sum(orderline.actualprice)
FROM customer C
INNER JOIN customerorder
ON customerorder.custno = C.custno
INNER JOIN orderline
ON customerorder.orderno = orderline.orderno
GROUP BY C.custno,
companyname
This is the query I tried to use and received the skewed COUNT results.
You have two basic options.
Use DISTINCT
Note:
In a comment on another question you say that this scews te result still. This seems to imply that the same
ordernocan appear in more than onecustomerorderrecord. Is that correct?Use a sub-query to make the join 1:1 instead of 1:many