You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OVER (PARTITION BY p.pub_idORDER BYs.transaction_date) as running_total
203
+
FROM pubs p
204
+
JOIN sales s ONp.pub_id=s.pub_id
205
+
JOIN beverages b ONs.beverage_id=b.beverage_id
206
+
ORDER BYp.pub_id, s.transaction_date;
207
+
```
141
208
### 10. For each country, what is the average price per unit of beverages in each category, and what is the overall average price per unit of beverages across all categories?
142
209
143
-
``` SQL code ```
210
+
```sql
211
+
SELECT
212
+
b.beverage_nameAS Beverage_Name,
213
+
p.countryAS Country,
214
+
b.categoryAS Category,
215
+
ROUND(AVG(b.price_per_unit), 2) AS Avg_Price,
216
+
ROUND((SELECTAVG(price_per_unit) FROM beverages), 2) AS Overall_Average_Price_Per_Unit
217
+
FROM pubs AS p
218
+
LEFT JOIN sales AS s
219
+
ONs.pub_id=p.pub_id
220
+
LEFT JOIN beverages AS b
221
+
ONb.beverage_id=s.beverage_id
222
+
GROUP BY Beverage_Name, Country, Category;
223
+
```
144
224
### 11. For each pub, what is the percentage contribution of each category of beverages to the total sales amount, and what is the pub's overall sales amount?
145
225
146
-
``` SQL code ```
226
+
```sql
227
+
WITH PubCategorySales AS (
228
+
SELECT
229
+
p.pub_id,
230
+
p.pub_name,
231
+
b.category,
232
+
SUM(b.price_per_unit*s.quantity) as total_category_sales
233
+
FROM pubs p
234
+
JOIN sales s ONp.pub_id=s.pub_id
235
+
JOIN beverages b ONs.beverage_id=b.beverage_id
236
+
GROUP BYp.pub_id, p.pub_name, b.category
237
+
),
238
+
239
+
PubTotalSales AS (
240
+
SELECT
241
+
p.pub_id,
242
+
SUM(b.price_per_unit*s.quantity) as total_sales_amount
243
+
FROM pubs p
244
+
JOIN sales s ONp.pub_id=s.pub_id
245
+
JOIN beverages b ONs.beverage_id=b.beverage_id
246
+
GROUP BYp.pub_id
247
+
)
248
+
249
+
SELECT
250
+
p.pub_id,
251
+
p.pub_name,
252
+
p.city,
253
+
p.state,
254
+
p.country,
255
+
pcs.category,
256
+
ROUND((pcs.total_category_sales/pts.total_sales_amount) *100, 2) as category_contribution_percentage
0 commit comments