-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL40.sql
More file actions
87 lines (75 loc) · 2 KB
/
DataExpert-SQL40.sql
File metadata and controls
87 lines (75 loc) · 2 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- Question: Optimizing Stock of Prime and Non-Prime Items in a Warehouse
-- https://www.dataexpert.io/question/warehouse-item-optimization
-- DataLemur: https://datalemur.com/questions/prime-warehouse-storage
-- Approach 01:
WITH batch_details AS (
SELECT
-- Use conditional aggregation to get both batch footages in one pass.
SUM(CASE WHEN item_type = 'prime_eligible' THEN square_footage ELSE 0 END) AS prime_footage,
SUM(CASE WHEN item_type = 'not_prime' THEN square_footage ELSE 0 END) AS not_prime_footage,
-- Also get the item counts per batch.
SUM(CASE WHEN item_type = 'prime_eligible' THEN 1 ELSE 0 END) AS prime_item_count,
SUM(CASE WHEN item_type = 'not_prime' THEN 1 ELSE 0 END) AS not_prime_item_count
FROM inventory
)
-- Calculate and union the results for both item types.
SELECT
'prime_eligible' AS item_type,
(FLOOR(500000 / prime_footage) * prime_item_count) AS item_count
FROM batch_details
UNION ALL
SELECT
'not_prime' AS item_type,
-- The modulo operator (%) efficiently finds the remaining space.
(FLOOR((500000 % prime_footage) / not_prime_footage) * not_prime_item_count) AS item_count
FROM batch_details
-- Order the final output as requested.
ORDER BY item_type DESC;
-- Approach 02:
WITH cte AS (
SELECT
item_type,
SUM(square_footage) AS total_sf,
COUNT(item_id) AS number_of_items
FROM
inventory
GROUP BY
item_type
)
SELECT
item_type,
FLOOR(500000 / total_sf) * number_of_items AS item_count
FROM
cte
WHERE
item_type = 'prime_eligible'
UNION ALL
SELECT
item_type,
FLOOR(
(
500000 - (
(
SELECT
FLOOR(500000 / total_sf)
FROM
cte
WHERE
item_type = 'prime_eligible'
) * (
SELECT
total_sf
FROM
cte
WHERE
item_type = 'prime_eligible'
)
)
) / total_sf
) * number_of_items AS item_count
FROM
cte
WHERE
item_type = 'not_prime'
ORDER BY
item_type DESC;