-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremaining_weight_query.sql
More file actions
36 lines (35 loc) · 1.12 KB
/
remaining_weight_query.sql
File metadata and controls
36 lines (35 loc) · 1.12 KB
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
28
29
30
31
32
33
34
35
36
SELECT
s2.ing_name,
s2.ordered_weight,
ing.ing_weight,
inv.quantity,
ing.ing_weight * IFNULL(inv.quantity, 0) AS total_inv_weight,
(ing.ing_weight * IFNULL(inv.quantity, 0)) - s2.ordered_weight AS remaining_weight
FROM (
SELECT
ing_id,
ing_name,
SUM(order_quantity * recipe_quantity) AS ordered_weight
FROM (
SELECT
o.item_id,
i.sku,
i.item_name,
r.ing_id,
ing.ing_name,
r.quantity AS recipe_quantity,
SUM(o.quantity) AS order_quantity,
ing.ing_weight,
ing.ing_price
FROM
Orders o
LEFT JOIN Item i ON o.item_id = i.item_id
LEFT JOIN Recipe r ON i.sku = r.recipe_id
LEFT JOIN Ingredient ing ON ing.ing_id = r.ing_id
GROUP BY
o.item_id, i.sku, i.item_name, r.ing_id, r.quantity, ing.ing_name, ing.ing_weight, ing.ing_price
) s1
GROUP BY ing_id, ing_name
) s2
LEFT JOIN Inventory inv ON TRIM(inv.item_id) = TRIM(s2.ing_id)
LEFT JOIN Ingredient ing ON TRIM(ing.ing_id) = TRIM(s2.ing_id);