-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alessia_Monfardino - Whole_foods_analysis.sql
305 lines (299 loc) · 13.2 KB
/
Alessia_Monfardino - Whole_foods_analysis.sql
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
-- Products have different units of measurement and the most common on is oz (60% of products). To compare the same units of measurement, I created a column with "price_per_oz", where I devide the regular price by the volume. --
-- I created 4 columns: sum_badges, subcatgory, average_price_per_oz and products_with badge (which is a count of products with that specific amount of badges in the subcategory). I then displayed only the subcategories that contain more than 20 products to provide a statistically meaningful analysis. I made sure select distinct product id to avoid analyzing duplicates --
USE bos_ddmban_sql_analysis;
WITH new_table AS
(SELECT *,
FORMAT(regular_price / volume, 2) AS 'price_per_oz'
FROM bmbandd_data
WHERE volume_of_measurement = 'oz')
SELECT
sum_badges,
subcategory,
CASE WHEN sum_badges = 0 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 1 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 2 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 3 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 4 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 5 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 6 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 7 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 8 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 9 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 10 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 11 THEN FORMAT (AVG(`price_per_oz`), 2)
WHEN sum_badges = 12 THEN FORMAT (AVG(`price_per_oz`), 2)
END AS 'average_price_per_oz',
COUNT(wf_product_id) AS 'products_with_badge'
FROM new_table
GROUP BY sum_badges, subcategory
-- only subcategories with more than 20 products to make the analysis consistent --
HAVING subcategory IN
(SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
ORDER BY sum_badges, subcategory
;
-- Union the queries with the different dietary preferences (badge) while only considering the subcategories with more than 20 products to keep the statistical analysis consistent. I then calculated the standard deviation for all the considered subcategories, both in the case of existing or non existing badge. I made sure select distinct product id to avoid analyzing duplicates--
USE bos_ddmban_sql_analysis;
WITH new_table AS (
SELECT *,
FORMAT(regular_price / volume, 2) AS 'price_per_weight'
FROM bmbandd_data
WHERE volume_of_measurement = 'oz')
SELECT subcategory,
'is_vegan' AS badge,
CASE WHEN is_vegan = 1 THEN 'vegan'ELSE 'not_vegan'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_vegan
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20
)
UNION
SELECT subcategory,
'is_keto_friendly' AS badge,
CASE WHEN is_keto_friendly = 1 THEN 'keto_friendly' ELSE 'not_keto_friendly'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_keto_friendly
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_paleo_friendly' AS badge,
CASE WHEN is_paleo_friendly = 1 THEN 'paleo_friendly' ELSE 'not_paleo_friendly'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_paleo_friendly
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_gluten_free' AS badge,
CASE WHEN is_gluten_free = 1 THEN 'gluten_free' ELSE 'not_gluten_free'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_gluten_free
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_vegetarian' AS badge,
CASE WHEN is_vegetarian = 1 THEN 'vegetarian' ELSE 'not_vegetarian'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_vegetarian
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_kosher' AS badge,
CASE WHEN is_kosher = 1 THEN 'kosher' ELSE 'not_kosher'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_kosher
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_sugar_conscious' AS badge,
CASE WHEN is_sugar_conscious = 1 THEN 'sugar_conscious' ELSE 'not_sugar_conscious'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_sugar_conscious
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_dairy_free' AS badge,
CASE WHEN is_dairy_free = 1 THEN 'dairy_free' ELSE 'not_dairy_free'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_dairy_free
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_high_fiber' AS badge,
CASE WHEN is_high_fiber = 1 THEN 'high_fiber' ELSE 'not_high_fiber'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_high_fiber
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_engine_2' AS badge,
CASE WHEN is_engine_2 = 1 THEN 'engine_2' ELSE 'not_engine_2'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_engine_2
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_low_sodium' AS badge,
CASE WHEN is_low_sodium = 1 THEN 'low_sodium' ELSE 'low_sodium'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_low_sodium
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_low_fat' AS badge,
CASE WHEN is_low_fat = 1 THEN 'low_fat' ELSE 'not_low_fat'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_low_fat
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_wheat_free' AS badge,
CASE WHEN is_wheat_free = 1 THEN 'wheat_free' ELSE 'not_wheat_free'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_wheat_free
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_organic' AS badge,
CASE WHEN is_organic = 1 THEN 'organic' ELSE 'not_organic'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_organic
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_whole_foods_diet' AS badge,
CASE WHEN is_whole_foods_diet = 1 THEN 'whole_foods_diet' ELSE 'not_whole_foods_diet'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_whole_foods_diet
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_others' AS badge,
CASE WHEN is_others = 1 THEN 'others' ELSE 'not_others'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_others
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
UNION
SELECT subcategory,
'is_no_badge' AS badge,
CASE WHEN is_no_badge = 1 THEN 'no_badge' ELSE 'yes_badge'
END AS 'badge_status',
FORMAT(AVG(`price_per_weight`), 2) AS 'price_per_oz',
ROUND(STDDEV_SAMP(`price_per_weight`), 2) AS standard_deviation,
COUNT(*) AS n_of_products
FROM new_table
GROUP BY subcategory, is_no_badge
HAVING subcategory IN (
SELECT subcategory
FROM new_table
GROUP BY subcategory
HAVING COUNT(DISTINCT wf_product_id) > 20)
;