The low stock represents the quantity of each product sold divided by the quantity of product in stock. We can consider the ten lowest rates. These will be the top ten products that are (almost) out-of-stock.
We'll need this two tables to perform the calculation: low stock=SUM(quantityOrdered)/quantityInStock
I tried to solve it but I'm getting a different output, so looking for some help.
My code:
SELECT p.productCode,ROUND(SUM(quantityOrdered)/quantityInStock,2) as low_stock
FROM products p
JOIN orderdetails od
ON p.productCode = od.productCode
GROUP BY p.productCode
ORDER BY low_stock
LIMIT 10;
Solution:
SELECT productCode,
ROUND(SUM(quantityOrdered) * 1.0 / (SELECT quantityInStock
FROM products p
WHERE od.productCode = p.productCode), 2) AS low_stock
FROM orderdetails od
GROUP BY productCode
ORDER BY low_stock
LIMIT 10;
I would really like to understand the solution code and where I have gone wrong.
