Skip to content

Commit 05f6b94

Browse files
author
Shuo
authored
Merge pull request #763 from openset/develop
Add: desc
2 parents 2bf8062 + 6117b66 commit 05f6b94

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed

problems/ads-performance/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,60 @@
1111

1212
## [1322. Ads Performance (Easy)](https://leetcode.com/problems/ads-performance "")
1313

14+
<p>Table: <code>Ads</code></p>
15+
<pre>
16+
+---------------+---------+
17+
| Column Name | Type |
18+
+---------------+---------+
19+
| ad_id | int |
20+
| user_id | int |
21+
| action | enum |
22+
+---------------+---------+
23+
(ad_id, user_id) is the primary key for this table.
24+
Each row of this table contains the ID of an Ad, the ID of a user and the action taken by this user regarding this Ad.
25+
The action column is an ENUM type of ('Clicked', 'Viewed', 'Ignored').
26+
</pre>
27+
28+
A company is running Ads and wants to calculate the performance of each Ad.
1429

30+
Performance of the Ad is measured using Click-Through Rate (CTR) where:
31+
32+
[[image-blog:Leetcode: Ads Performance][https://raw.githubusercontent.com/dennyzhang/code.dennyzhang.com/master/problems/ads-performance/ctrformula.png]]
33+
34+
Write an SQL query to find the ctr of each Ad.
35+
36+
Round ctr to 2 decimal points. Order the result table by ctr in descending order and by ad_id in ascending order in case of a tie.
37+
38+
The query result format is in the following example:
39+
<pre>
40+
Ads table:
41+
+-------+---------+---------+
42+
| ad_id | user_id | action |
43+
+-------+---------+---------+
44+
| 1 | 1 | Clicked |
45+
| 2 | 2 | Clicked |
46+
| 3 | 3 | Viewed |
47+
| 5 | 5 | Ignored |
48+
| 1 | 7 | Ignored |
49+
| 2 | 7 | Viewed |
50+
| 3 | 5 | Clicked |
51+
| 1 | 4 | Viewed |
52+
| 2 | 11 | Viewed |
53+
| 1 | 2 | Clicked |
54+
+-------+---------+---------+
55+
Result table:
56+
+-------+-------+
57+
| ad_id | ctr |
58+
+-------+-------+
59+
| 1 | 66.67 |
60+
| 3 | 50.00 |
61+
| 2 | 33.33 |
62+
| 5 | 0.00 |
63+
+-------+-------+
64+
for ad_id = 1, ctr = (2/(2+1)) * 100 = 66.67
65+
for ad_id = 2, ctr = (1/(1+2)) * 100 = 33.33
66+
for ad_id = 3, ctr = (1/(1+1)) * 100 = 50.00
67+
for ad_id = 5, ctr = 0.00, Note that ad_id has no clicks or views.
68+
Note that we don't care about Ignored Ads.
69+
Result table is ordered by the ctr. in case of a tie we order them by ad_id
70+
</pre>

problems/list-the-products-ordered-in-a-period/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,80 @@
1111

1212
## [1327. List the Products Ordered in a Period (Easy)](https://leetcode.com/problems/list-the-products-ordered-in-a-period "")
1313

14+
SQL Schema
15+
<p>Table: <code>Products</code></p>
16+
<pre>
17+
+------------------+---------+
18+
| Column Name | Type |
19+
+------------------+---------+
20+
| product_id | int |
21+
| product_name | varchar |
22+
| product_category | varchar |
23+
+------------------+---------+
24+
product_id is the primary key for this table.
25+
This table contains data about the company's products.
26+
</pre>
1427

28+
<p>Table: <code>Orders</code></p>
29+
<pre>
30+
+---------------+---------+
31+
| Column Name | Type |
32+
+---------------+---------+
33+
| product_id | int |
34+
| order_date | date |
35+
| unit | int |
36+
+---------------+---------+
37+
There is no primary key for this table. It may have duplicate rows.
38+
product_id is a foreign key to Products table.
39+
unit is the number of products ordered in order_date.
40+
</pre>
41+
42+
Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 and their amount.
43+
44+
Return result table in any order.
45+
46+
The query result format is in the following example:
47+
<pre>
48+
Products table:
49+
+-------------+-----------------------+------------------+
50+
| product_id | product_name | product_category |
51+
+-------------+-----------------------+------------------+
52+
| 1 | Leetcode Solutions | Book |
53+
| 2 | Jewels of Stringology | Book |
54+
| 3 | HP | Laptop |
55+
| 4 | Lenovo | Laptop |
56+
| 5 | Leetcode Kit | T-shirt |
57+
+-------------+-----------------------+------------------+
58+
59+
Orders table:
60+
+--------------+--------------+----------+
61+
| product_id | order_date | unit |
62+
+--------------+--------------+----------+
63+
| 1 | 2020-02-05 | 60 |
64+
| 1 | 2020-02-10 | 70 |
65+
| 2 | 2020-01-18 | 30 |
66+
| 2 | 2020-02-11 | 80 |
67+
| 3 | 2020-02-17 | 2 |
68+
| 3 | 2020-02-24 | 3 |
69+
| 4 | 2020-03-01 | 20 |
70+
| 4 | 2020-03-04 | 30 |
71+
| 4 | 2020-03-04 | 60 |
72+
| 5 | 2020-02-25 | 50 |
73+
| 5 | 2020-02-27 | 50 |
74+
| 5 | 2020-03-01 | 50 |
75+
+--------------+--------------+----------+
76+
77+
Result table:
78+
+--------------------+---------+
79+
| product_name | unit |
80+
+--------------------+---------+
81+
| Leetcode Solutions | 130 |
82+
| Leetcode Kit | 100 |
83+
+--------------------+---------+
84+
85+
Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
86+
Products with product_id = 2 is ordered in February a total of 80.
87+
Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
88+
Products with product_id = 4 was not ordered in February 2020.
89+
Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
90+
</pre>

problems/number-of-transactions-per-visit/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,84 @@
1111

1212
## [1336. Number of Transactions per Visit (Medium)](https://leetcode.com/problems/number-of-transactions-per-visit "")
1313

14+
<p>Table: <code>Visits</code></p>
15+
<pre>
16+
+---------------+---------+
17+
| Column Name | Type |
18+
+---------------+---------+
19+
| user_id | int |
20+
| visit_date | date |
21+
+---------------+---------+
22+
(user_id, visit_date) is the primary key for this table.
23+
Each row of this table indicates that user_id has visited the bank in visit_date.
24+
</pre>
1425

26+
<p>Table: <code>Transactions</code></p>
27+
<pre>
28+
+------------------+---------+
29+
| Column Name | Type |
30+
+------------------+---------+
31+
| user_id | int |
32+
| transaction_date | date |
33+
| amount | int |
34+
+------------------+---------+
35+
There is no primary key for this table, it may contain duplicates.
36+
Each row of this table indicates that user_id has done a transaction of amount in transaction_date.
37+
It is guaranteed that the user has visited the bank in the transaction_date.(i.e The Visits table contains (user_id, transaction_date) in one row)
38+
</pre>
39+
40+
Write an SQL query to find how many users visited the bank and didn't do any transactions, how many visited the bank and did one transaction and so on.
41+
42+
The result table will contain two columns transactions_count which is the number of transactions done in one visit and visits_count which is the corresponding number of users who did transactions_count in one visit to the bank. transactions_count should take all values from 0 to max(transactions_count) done by one or more users.
43+
44+
Order the result table by transactions_count.
45+
46+
The query result format is in the following example:
47+
48+
<pre>
49+
Visits table:
50+
+---------+------------+
51+
| user_id | visit_date |
52+
+---------+------------+
53+
| 1 | 2020-01-01 |
54+
| 2 | 2020-01-02 |
55+
| 12 | 2020-01-01 |
56+
| 19 | 2020-01-03 |
57+
| 1 | 2020-01-02 |
58+
| 2 | 2020-01-03 |
59+
| 1 | 2020-01-04 |
60+
| 7 | 2020-01-11 |
61+
| 9 | 2020-01-25 |
62+
| 8 | 2020-01-28 |
63+
+---------+------------+
64+
Transactions table:
65+
+---------+------------------+--------+
66+
| user_id | transaction_date | amount |
67+
+---------+------------------+--------+
68+
| 1 | 2020-01-02 | 120 |
69+
| 2 | 2020-01-03 | 22 |
70+
| 7 | 2020-01-11 | 232 |
71+
| 1 | 2020-01-04 | 7 |
72+
| 9 | 2020-01-25 | 33 |
73+
| 9 | 2020-01-25 | 66 |
74+
| 8 | 2020-01-28 | 1 |
75+
| 9 | 2020-01-25 | 99 |
76+
+---------+------------------+--------+
77+
Result table:
78+
+--------------------+--------------+
79+
| transactions_count | visits_count |
80+
+--------------------+--------------+
81+
| 0 | 4 |
82+
| 1 | 5 |
83+
| 2 | 0 |
84+
| 3 | 1 |
85+
+--------------------+--------------+
86+
Users 1, 2, 12 and 19 visited the bank in 2020-01-01, 2020-01-02, 2020-01-01 and 2020-01-03 respectively, and didn't do any transactions.
87+
So we have visits_count = 4 for transactions_count = 0.
88+
Users 2, 7 and 8 visited the bank in 2020-01-03, 2020-01-11 and 2020-01-28 respectively, and did one transaction.
89+
User 1 Also visited the bank in 2020-01-02 and 2020-01-04 and did one transaction each day.
90+
So we have total visits_count = 5 for transactions_count = 1.
91+
For transactions_count = 2 we don't have any users who visited the bank and did two transactions.
92+
For transactions_count = 3 we have user 9 who visited the bank in 2020-01-25 and did three transactions.
93+
Note that we stopped at transactions_count = 3 as this is the maximum number of transactions done by one user in one visit to the bank.
94+
</pre>

0 commit comments

Comments
 (0)