-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank.sql
62 lines (54 loc) · 2.88 KB
/
rank.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
/*
BACKGROUND:
The following schema is a subset of a relational database of a grocery store
chain. This chain sells many products of different product classes to its
customers across its different stores. It also conducts many different
promotion campaigns.
The relationship between the four tables we want to analyze is depicted below:
# sales # products
+------------------+---------+ +---------------------+---------+
| product_id | INTEGER |>--------| product_id | INTEGER |
| store_id | INTEGER | +---<| product_class_id | INTEGER |
| customer_id | INTEGER | | | brand_name | VARCHAR |
+---<| promotion_id | INTEGER | | | product_name | VARCHAR |
| | store_sales | DECIMAL | | | is_low_fat_flg | TINYINT |
| | store_cost | DECIMAL | | | is_recyclable_flg | TINYINT |
| | units_sold | DECIMAL | | | gross_weight | DECIMAL |
| | transaction_date | DATE | | | net_weight | DECIMAL |
| +------------------+---------+ | +---------------------+---------+
| |
| # promotions | # product_classes
| +------------------+---------+ | +---------------------+---------+
+----| promotion_id | INTEGER | +----| product_class_id | INTEGER |
| promotion_name | VARCHAR | | product_subcategory | VARCHAR |
| media_type | VARCHAR | | product_category | VARCHAR |
| cost | DECIMAL | | product_department | VARCHAR |
| start_date | DATE | | product_family | VARCHAR |
| end_date | DATE | +---------------------+---------+
+------------------+---------+
*/
/*
PROMPT:
-- What are the top five (ranked in decreasing order)
-- single-channel media types that correspond to the most money
-- the grocery chain had spent on its promotional campaigns?
Single Media Channel Types are promotions that contain only one media type.
EXPECTED OUPTUT:
Note: Please use the column name(s) specified in the expected output in your solution.
+---------------------------+------------+
| single_channel_media_type | total_cost |
+---------------------------+------------+
| In-Store Coupon | 70800.0000 |
| Street Handout | 70627.0000 |
| Radio | 60192.0000 |
| Sunday Paper | 56994.0000 |
| Product Attachment | 50815.0000 |
+---------------------------+------------+
-------------- PLEASE WRITE YOUR SQL SOLUTION BELOW THIS LINE ----------------
*/
SELECT media_type as single_channel_media_type, SUM(cost) as total_cost
FROM promotions
WHERE media_type NOT LIKE ('%, %')
GROUP BY media_type
ORDER BY total_cost DESC
LIMIT 5;