0

I have two tables one CUSTOMERS

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ID        | int(11)      | NO   | PRI | NULL    |       |
| FIRSTNAME | varchar(50)  | YES  |     | NULL    |       |
| LASTNAME  | varchar(50)  | YES  |     | NULL    |       |
| ADDRESS   | varchar(100) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

and orders

+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| ID            | int(11)      | NO   | PRI | NULL    |       |
| PRODUCT_NAME  | varchar(100) | YES  |     | NULL    |       |
| PRODUCT_PRICE | double(10,4) | YES  |     | NULL    |       |
| DATE_ORDER    | date         | YES  |     | NULL    |       |
| ID_CUSTOMER   | int(11)      | YES  |     | NULL    |       |
| AMOUNT        | int(11)      | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+

I need to "Get the first and last names of the customers who made orders in total sum greater than the average sum of all orders. Don’t care about duplicates".

Here what I've tried

 select FIRSTNAME, LASTNAME, ID, 
        AVG(PRODUCT_PRICE * AMOUNT)  
 from     CUSTOMERS C 
 join     ORDERS O 
 on       C.ID = ID_CUSTOMER 
 GROUP BY FIRSTNAME, LASTNAME 
 HAVING AVG(PRODUCT_PRICE * AMOUNT) < (
                 select (PRODUCT_PRICE * AMOUNT) 
                 from ORDERS 
                 where C.ID = O.ID_CUSTOMER;

That does not work. I need some help

1
  • 2
    Note that it's fantastically unlikely that a price would be recorded as double rather than decimal... and see normalisation Commented Dec 25, 2016 at 11:26

2 Answers 2

2

You need to find average sum of all orders in a subquery.

select 
    C.ID,
    C.FIRSTNAME,
    C.LASTNAME,
    SUM(O.PRODUCT_PRICE * O.AMOUNT)
from     CUSTOMERS C 
join     ORDERS O 
on       C.ID = O.ID_CUSTOMER 
GROUP BY C.ID, C.FIRSTNAME, C.LASTNAME 
HAVING SUM(PRODUCT_PRICE * AMOUNT) > (
                 select AVG(PRODUCT_PRICE * AMOUNT)
                 from ORDERS)
Sign up to request clarification or add additional context in comments.

2 Comments

That is it!! Only I do not need sum, but I refactored it simply to my needs
Glad it helped.
0

Most part of your code is correct, you want just remove the part where C.ID = O.ID_CUSTOMER;. this is not need to calculate the average value of the order items.

using this following code you can get the required outut

SELECT FIRSTNAME,LASTNAME FROM CUSTOMERS 
INNER JOIN ORDERS ON CUSTOMERS.ID=ORDERS.ID_CUSTOMER 
GROUP BY CUSTOMERS.ID 
HAVING SUM(ORDERS.PRODUCT_PRICE*ORDERS.AMOUNT)>
(SELECT AVG(PRODUCT_PRICE*AMOUNT) FROM ORDERS)

4 Comments

That is it, but you have mistyped in having clause - you used ORDERS.ORDERS (twice) ))
I have made a mistake, Thank you
@anti-k that's exact same query as mine
@K.Sathagar That's not much of a stretch. I think that much OP should be able to do himself.

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.