2

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.

2 Answers 2

2

You have two basic options.

Use DISTINCT

SELECT C.custno, 
       companyname, 
       Count(DISTINCT 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 

Note:

In a comment on another question you say that this scews te result still. This seems to imply that the same orderno can appear in more than one customerorder record. Is that correct?


Use a sub-query to make the join 1:1 instead of 1:many

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 (SELECT orderno, SUM(actualprice) AS actualprice
                     FROM orderline 
                 GROUP BY orderno)
               ON customerorder.orderno = orderline.orderno 
GROUP  BY C.custno, 
          companyname 
4
  • Is there an added benefit to using DISTINCT over subquery or Vice versa?
    – Randy B.
    Nov 14, 2012 at 11:38
  • When I do not use DISTINCT the result is skewed. The OrderNo can appear on multiple instances of the OrderLine table.
    – Randy B.
    Nov 14, 2012 at 11:41
  • @RandyB. - I prefer the SubQuery as using DISTINCT often masks other mistakes (When I see DISTINCT I often wonder if it's a hack to correct a mistake in a bad way). Also, sometimes it's more performant - you should trial both with your data to compare. [You say that using distinct you get the right results, yet on the other answer you say that you don't?]
    – MatBailie
    Nov 14, 2012 at 11:43
  • I read his wrong, using DISTINCT was correct. Thank you both though.
    – Randy B.
    Nov 14, 2012 at 11:48
1

Try this

SELECT C.custno, 
       companyname, 
       Count(Distinct 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

Use Count on DISTINCT order numbers

3
  • That's exactly the one I have tried, but it skews the results of the COUNT query.
    – Randy B.
    Nov 14, 2012 at 11:35
  • Such as I know a certain company has placed 6 orders, but with the SUM and Join, I get the COUNT result of 11 orders for that company.
    – Randy B.
    Nov 14, 2012 at 11:35
  • I read that wrong, you're correct. Sorry about that. I didn't use DISTINCT.
    – Randy B.
    Nov 14, 2012 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.