2

I am trying to write an SQL statement that does this:

For each customer that has an order, I need to list the customer's number, the number of orders that customer has, the total quantity of items on those orders and the total price for those items. I then to need sort the result by the customer number.

I've got the below code. It works, but the results are incorrect. For 1 customer it says the order quantity is 2 but there is only 1 order and the price is coming out as if it's 2 orders. Another customer has 3 orders but it's showing 4. I'm assuming I'm missing a join function?

I have 3 tables, a Customer Table (Customer_Name), Orders Table (order_num, order_date, customer_num), Items Table (item_num, order_num, quantity, total_price)

SELECT   customer.customer_num AS "Customer Number",
         COUNT(DISTINCT orders.order_num) AS "Order Amount",
         SUM(items.quantity) AS "Quantity of Items",
         SUM(items.total_price) AS "Total Price w/o shipping"
FROM     items, orders, customer
WHERE    customer.customer_num = orders.customer_num
AND      orders.order_num = items.order_num
GROUP BY customer.customer_num
ORDER BY customer.customer_num ASC;

Any help would be great. Thanks.

2
  • Youre having issues with duplication due to incorrect JOIN issue. You need to join on all the appropriate columns to reduce duplication. ALSO, join duplication occurs due to many - to - one scenarios, where the duplication is expected results but does screw up thing, especially aggs. Commented Sep 6, 2020 at 19:11
  • 3
    Firstly you should use ANSI standard joins rather than traditional Oracle join. Secondly, you should share some sample data and expecred result for others to help you better. Commented Sep 6, 2020 at 19:14

1 Answer 1

2

Logically, your query looks correct, but it is poorly written using commas in the FROM clause. In addition, you don't need to join to the customers table.

So, I would recommend:

SELECT o.customer_num AS "Customer Number",
       COUNT(DISTINCT o.order_num) AS "Order Amount",
       SUM(i.quantity) AS "Quantity of Items",
       SUM(i.total_price) AS "Total Price w/o shipping"
FROM orders o JOIN
     items i
     ON o.order_num = i.order_num
GROUP BY o.customer_num
ORDER BY o.customer_num ASC;

The COUNT(DISTINCT) should be doing what you want in terms of counting orders.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Gordon, thank you for taking the time, I appreciate it. I am taking an Oracle Programming class and the text book they are using is Oracle 12c by Casteel which is why my code looks the way it does (I'm on Chapters 1-5). I tried something similar and it produces the same results, so this is probably right. 1 question, in FROM you have orders o and items i and then you use o and i in the code. At that FROM area are you basically assigning i and o as the respective table names? Again, thanks for your time.
@AdamDRuffolo . . . It is very sad that a class would use a textbook with such out-of-date SQL. It is not like JOIN was introduced last year. It has been part of the standard since the 1990s and supported by Oracle for a long, long time.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.