Skip to content

Commit 80abf70

Browse files
authored
Merge pull request #53 from iamAntimPal/Branch-1
1341. Movie Rating
2 parents 786e256 + 748fc9e commit 80abf70

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
321. Restaurant Growth
2+
Solved
3+
Medium
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Customer
9+
10+
+---------------+---------+
11+
| Column Name | Type |
12+
+---------------+---------+
13+
| customer_id | int |
14+
| name | varchar |
15+
| visited_on | date |
16+
| amount | int |
17+
+---------------+---------+
18+
In SQL,(customer_id, visited_on) is the primary key for this table.
19+
This table contains data about customer transactions in a restaurant.
20+
visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
21+
amount is the total paid by a customer.
22+
23+
24+
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
25+
26+
Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places.
27+
28+
Return the result table ordered by visited_on in ascending order.
29+
30+
The result format is in the following example.
31+
32+
33+
34+
Example 1:
35+
36+
Input:
37+
Customer table:
38+
+-------------+--------------+--------------+-------------+
39+
| customer_id | name | visited_on | amount |
40+
+-------------+--------------+--------------+-------------+
41+
| 1 | Jhon | 2019-01-01 | 100 |
42+
| 2 | Daniel | 2019-01-02 | 110 |
43+
| 3 | Jade | 2019-01-03 | 120 |
44+
| 4 | Khaled | 2019-01-04 | 130 |
45+
| 5 | Winston | 2019-01-05 | 110 |
46+
| 6 | Elvis | 2019-01-06 | 140 |
47+
| 7 | Anna | 2019-01-07 | 150 |
48+
| 8 | Maria | 2019-01-08 | 80 |
49+
| 9 | Jaze | 2019-01-09 | 110 |
50+
| 1 | Jhon | 2019-01-10 | 130 |
51+
| 3 | Jade | 2019-01-10 | 150 |
52+
+-------------+--------------+--------------+-------------+
53+
Output:
54+
+--------------+--------------+----------------+
55+
| visited_on | amount | average_amount |
56+
+--------------+--------------+----------------+
57+
| 2019-01-07 | 860 | 122.86 |
58+
| 2019-01-08 | 840 | 120 |
59+
| 2019-01-09 | 840 | 120 |
60+
| 2019-01-10 | 1000 | 142.86 |
61+
+--------------+--------------+----------------+
62+
Explanation:
63+
1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86
64+
2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120
65+
3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120
66+
4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86
67+
68+
69+
Solution 1:
70+
71+
# Write your MySQL query statement below
72+
WITH
73+
t AS (
74+
SELECT
75+
visited_on,
76+
SUM(amount) OVER (
77+
ORDER BY visited_on
78+
ROWS 6 PRECEDING
79+
) AS amount,
80+
RANK() OVER (
81+
ORDER BY visited_on
82+
ROWS 6 PRECEDING
83+
) AS rk
84+
FROM
85+
(
86+
SELECT visited_on, SUM(amount) AS amount
87+
FROM Customer
88+
GROUP BY visited_on
89+
) AS tt
90+
)
91+
SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
92+
FROM t
93+
WHERE rk > 6;
94+
95+
96+
Solution 2:
97+
# Write your MySQL query statement below
98+
SELECT
99+
a.visited_on,
100+
SUM(b.amount) AS amount,
101+
ROUND(SUM(b.amount) / 7, 2) AS average_amount
102+
FROM
103+
(SELECT DISTINCT visited_on FROM customer) AS a
104+
JOIN customer AS b ON DATEDIFF(a.visited_on, b.visited_on) BETWEEN 0 AND 6
105+
WHERE a.visited_on >= (SELECT MIN(visited_on) FROM customer) + 6
106+
GROUP BY 1
107+
ORDER BY 1;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
1341. Movie Rating
2+
"""
3+
Table: Movies
4+
5+
+---------------+---------+
6+
| Column Name | Type |
7+
+---------------+---------+
8+
| movie_id | int |
9+
| title | varchar |
10+
+---------------+---------+
11+
movie_id is the primary key (column with unique values) for this table.
12+
title is the name of the movie.
13+
14+
15+
Table: Users
16+
17+
+---------------+---------+
18+
| Column Name | Type |
19+
+---------------+---------+
20+
| user_id | int |
21+
| name | varchar |
22+
+---------------+---------+
23+
user_id is the primary key (column with unique values) for this table.
24+
The column 'name' has unique values.
25+
Table: MovieRating
26+
27+
+---------------+---------+
28+
| Column Name | Type |
29+
+---------------+---------+
30+
| movie_id | int |
31+
| user_id | int |
32+
| rating | int |
33+
| created_at | date |
34+
+---------------+---------+
35+
(movie_id, user_id) is the primary key (column with unique values) for this table.
36+
This table contains the rating of a movie by a user in their review.
37+
created_at is the user's review date.
38+
39+
40+
Write a solution to:
41+
42+
Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
43+
Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name.
44+
The result format is in the following example.
45+
46+
47+
48+
Example 1:
49+
50+
Input:
51+
Movies table:
52+
+-------------+--------------+
53+
| movie_id | title |
54+
+-------------+--------------+
55+
| 1 | Avengers |
56+
| 2 | Frozen 2 |
57+
| 3 | Joker |
58+
+-------------+--------------+
59+
Users table:
60+
+-------------+--------------+
61+
| user_id | name |
62+
+-------------+--------------+
63+
| 1 | Daniel |
64+
| 2 | Monica |
65+
| 3 | Maria |
66+
| 4 | James |
67+
+-------------+--------------+
68+
MovieRating table:
69+
+-------------+--------------+--------------+-------------+
70+
| movie_id | user_id | rating | created_at |
71+
+-------------+--------------+--------------+-------------+
72+
| 1 | 1 | 3 | 2020-01-12 |
73+
| 1 | 2 | 4 | 2020-02-11 |
74+
| 1 | 3 | 2 | 2020-02-12 |
75+
| 1 | 4 | 1 | 2020-01-01 |
76+
| 2 | 1 | 5 | 2020-02-17 |
77+
| 2 | 2 | 2 | 2020-02-01 |
78+
| 2 | 3 | 2 | 2020-03-01 |
79+
| 3 | 1 | 3 | 2020-02-22 |
80+
| 3 | 2 | 4 | 2020-02-25 |
81+
+-------------+--------------+--------------+-------------+
82+
Output:
83+
+--------------+
84+
| results |
85+
+--------------+
86+
| Daniel |
87+
| Frozen 2 |
88+
+--------------+
89+
Explanation:
90+
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
91+
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.
92+
93+
"""
94+
95+
# Write your MySQL query statement below
96+
(
97+
SELECT name AS results
98+
FROM
99+
Users
100+
JOIN MovieRating USING (user_id)
101+
GROUP BY user_id
102+
ORDER BY COUNT(1) DESC, name
103+
LIMIT 1
104+
)
105+
UNION ALL
106+
(
107+
SELECT title
108+
FROM
109+
MovieRating
110+
JOIN Movies USING (movie_id)
111+
WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02'
112+
GROUP BY movie_id
113+
ORDER BY AVG(rating) DESC, title
114+
LIMIT 1
115+
);

0 commit comments

Comments
 (0)