-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject question 1.sql
More file actions
27 lines (25 loc) · 851 Bytes
/
Copy pathproject question 1.sql
File metadata and controls
27 lines (25 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
WITH
low_stock_cte as
(
SELECT productCode,
round(sum(od.quantityordered) * 1.0 / (
SELECT quantityInStock
FROM products p
WHERE p.productCode = od.productCode), 2) as stockratio
FROM orderdetails od
GROUP BY productCode
ORDER by stockratio DESC
limit 10
),
high_performance_cte as
(
SELECT productCode, sum(quantityOrdered * priceEach) product_performance
FROM orderdetails
GROUP by productCode
ORDER by product_performance DESC
)
SELECT low_stock_cte.productCode, productName, productLine, low_stock_cte.stockratio, high_performance_cte.product_performance
FROM low_stock_cte
JOIN high_performance_cte on low_stock_cte.productCode = high_performance_cte.productCode
JOIN products on high_performance_cte.productCode = products.productCode
ORDER by high_performance_cte.product_performance DESC