0

I have the following tables:

Product(maker, model, type)

PC(model, speed, ram, hd, price)

Laptop(model, speed, ram, hd, screen, price)

Printer(model, color, type, price)

I need to write a query that will return the average price of all products made by each maker, but only if that average is >= 200, in descending order by price.

I have tried 2 different methods and both get me very close but not exactly what I need:

(SELECT maker, AVG(price) as a
FROM Product NATURAL JOIN PC
WHERE price >= 200
GROUP BY maker)

UNION

(SELECT maker, AVG(price) as b
FROM Product NATURAL JOIN Laptop
WHERE price >= 200
GROUP BY maker)

UNION

(SELECT maker, AVG(price) as c
FROM Product NATURAL JOIN Printer
WHERE price >= 200
GROUP BY maker)

ORDER BY a;

The above gives me the average prices made by each maker for all the products they have made but it is all in one column so you cannot visually tell what product each average is linked to.

SELECT maker,

(SELECT AVG(price)
FROM PC
WHERE price >= 200) as 'Average Cost of PCs',

(SELECT AVG(price)
FROM Laptop
WHERE price >= 200
GROUP BY maker) as 'Average Cost of Laptops',

(SELECT AVG(price)
FROM Printer
WHERE price >= 200
GROUP BY maker) as 'Average Cost of Printers'

FROM Product
GROUP BY maker;

The above successfully gives each type of product its own column and also a column for all the makers, but it gives the average cost for all PCs, Printers, and Laptops in their respective columns instead of the average cost of each made by the maker it is parallel to.

Im not sure which one I am closer to the answer with but I've hit a wall and I'm not sure what to do. If I could get the first code to divide into different columns it would be correct. if I could get the second one to average correctly it would be right.

I am very new to Stack Overflow so I apologize if I did not ask this question in the correct format

3
  • what do you actually want to achieve. Could give an example output?
    – nbk
    Oct 23, 2019 at 17:58
  • I'd caution against using natural join. All you need to do is add a column that accidentally matches the name of a column in another table, and you've changed the join criteria of those two tables.
    – Uueerdo
    Oct 23, 2019 at 18:19
  • Do you mean "averages >= 200" or "average of prices >= 200"? Because your words say the first, but your queries do the second.
    – Uueerdo
    Oct 23, 2019 at 18:21

1 Answer 1

0

You can UNION the "detail" table data, and join to that, and use what is referred to as conditional aggregation (aggregate functions ignore null values) to get your averages:

SELECT p.maker
   , AVG(CASE WHEN d.Type = 'PC' THEN d.price ELSE NULL END) AS pcAvg
   , AVG(CASE WHEN d.Type = 'Laptop' THEN d.price ELSE NULL END) AS laptopAvg
   , AVG(CASE WHEN d.Type = 'Printer' THEN d.price ELSE NULL END) AS printerAvg
FROM Product AS p
LEFT JOIN (
   SELECT model, price, 'PC' AS Type FROM PC
   UNION SELECT model, price, 'Laptop' AS Type FROM Laptop
   UNION SELECT model, price, 'Printer' AS Type FROM Printer
) AS d ON p.model = d.model
GROUP BY p.maker

If you want the average of prices >= 200 you can filter them out in the unioned subqueries or add an AND d.price >= 200 to each WHEN.

If you only want averages >= 200, you need to wrap the query above like so:

SELECT q.maker
   , CASE WHEN q.pcAvg >= 200 THEN q.pcAvg ELSE NULL END AS pcAvg
   , CASE WHEN q.laptopAvg >= 200 THEN q.laptopAvg ELSE NULL END AS laptopAvg 
   , CASE WHEN q.printerAvg >= 200 THEN q.printerAvg ELSE NULL END AS printerAvg 
FROM (the query above) AS q
;

You cannot omit a column if the value is <= 200, you can only give a different value.

Sidenote: You can actually omit ELSE NULL from a CASE statement, the lack of an else implies else null; I was just being explicit for clarity of example and intent.

1
  • Thank you so much. this gives exactly what I need! (I tried to upvote but I guess it won't show because im new haha)
    – Zari Case
    Oct 23, 2019 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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