Average Price per column within different tables

I'm stumped in trying to complete a problem I was tasked in getting solved.

I'm tasked with doing the following: Calculate the average cost for different products and group them by the category of the product.

How can I do this against different tables?

Upvotes: 0

Views: 116

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269853

I see. You want the "average" per "product", not over all the products. So, you need to calculate this yourself using COUNT(DISTINCT):

SELECT p.category AS category_id,
       SUM(s.selling_price::numeric) / COUNT(DISTINCT p.product_id)
FROM product p JOIN
     supply s
     ON p.product_id = s.product_id
GROUP BY p.category;

Upvotes: 1

Related Questions