Reputation: 5471
I have three tables which you can also find in the SQL fiddle:
CREATE TABLE Sales (
Product_ID VARCHAR(255),
Sales_Value VARCHAR(255),
Sales_Quantity VARCHAR(255)
);
INSERT INTO Sales
(Product_ID, Sales_Value, Sales_Quantity)
VALUES
("P001", "500", "200"),
("P002", "600", "100"),
("P003", "300", "250"),
("P004", "900", "400"),
("P005", "800", "600"),
("P006", "200", "150"),
("P007", "700", "550");
CREATE TABLE Products (
Product_ID VARCHAR(255),
Product_Name VARCHAR(255),
Category_ID VARCHAR(255)
);
INSERT INTO Products
(Product_ID, Product_Name, Category_ID)
VALUES
("P001", "Shirt", "C001"),
("P002", "Dress", "C001"),
("P003", "Hoodie", "C002"),
("P004", "Ball", "C002"),
("P005", "Ski", "C002"),
("P006", "Boot", "C003"),
("P007", "Flip-Flop", "C003");
CREATE TABLE Categories (
Category_ID VARCHAR(255),
Category_Name VARCHAR(255)
);
INSERT INTO Categories
(Category_ID, Category_Name)
VALUES
("C001", "Fashion"),
("C002", "Sport"),
("C003", "Shoes");
The first table contains the Sales
for each product.
The second table contains details about each product
.
The third table contains categories
.
Now, I want to display all products and the average_sales_price_per_category
next to each product.
The result should look like this:
Product_ID Category average_sales_price_per_category
P001 Fashion 3.66
P002 Fashion 3.66
P003 Sport 1.60
P004 Sport 1.60
P005 Sport 1.60
P006 Shoes 1.28
P007 Shoes 1.28
I tried to go with the solution from this question but I get an Error
:
SELECT s.Product_ID, c.Category_Name,
(SELECT SUM(SS.Sales_Value) / SUM(SS.Sales_Quantity)
FROM Sales SS
WHERE SS.Category_ID = S.Category_ID
) AS average_sales_price
FROM Sales s
JOIN Products p ON p.Product_ID = s.Product_ID
JOIN Categories c ON c.Category_ID = p.Category_ID;
Error
Unknown column 'SS.Category_ID' in 'where clause'
What do I need to change in my code to get the expected result?
Upvotes: 1
Views: 54
Reputation: 133360
You s table is not visible in inner subquery for avoid the where condition in inner subquery you could use a join on the grouped aggreated subquery
SELECT
s.Product_ID,
Price_Category.average_sales_price_per_category
FROM Sales s
JOIN Products p ON p.Product_ID = s.Product_ID
JOIN
(SELECT
c.Category_ID,
c.Category_Name,
SUM(s.Sales_Value) / SUM(s.Sales_Quantity) AS average_sales_price_per_category
FROM Sales s
JOIN Products p ON p.Product_ID = s.Product_ID
JOIN Categories c ON c.Category_ID = p.Category_ID
GROUP BY 1) Price_Category ON Price_Category.Category_ID = p.Category_ID;
Upvotes: 0
Reputation: 65218
Firstly, you don't have any Category_Id
column within the Sales
table.
Replace your JOIN
clauses with LEFT JOIN
s against probability of non-matched cases, and add GROUP BY c.Category_Name, c.Category_ID
at the end :
SELECT c.Category_Name,c.Category_ID,
SUM(S.Sales_Value) / SUM(S.Sales_Quantity) AS average_sales_price
FROM Sales s
LEFT JOIN Products p ON p.Product_ID = s.Product_ID
LEFT JOIN Categories c ON c.Category_ID = p.Category_ID
GROUP BY c.Category_Name, c.Category_ID;
where a correlated subquery is not needed.
Upvotes: 0
Reputation: 222432
The problem with your query is that categories are not available at sales level.
If you are running MySQL 8.0, you can combine aggregation and window functions as follows:
SELECT
p.Product_ID,
c.Category_Name,
SUM(SUM(s.Sales_Value)) OVER(PARTITION BY c.Category_ID)
/ SUM(SUM(s.Sales_Quantity)) OVER(PARTITION BY c.Category_ID)
AS average_sales_price
FROM Sales s
JOIN Products p ON p.Product_ID = s.Product_ID
JOIN Categories c ON c.Category_ID = p.Category_ID
GROUP BY p.Product_ID, c.Category_ID, c.Category_Name
| Product_ID | Category_Name | average_sales_price | | ---------- | ------------- | ------------------- | | P001 | Fashion | 3.6666666666666665 | | P002 | Fashion | 3.6666666666666665 | | P003 | Sport | 1.6 | | P004 | Sport | 1.6 | | P005 | Sport | 1.6 | | P006 | Shoes | 1.2857142857142858 | | P007 | Shoes | 1.2857142857142858 |
Upvotes: 1