Skip to content

Commit 5ea54c1

Browse files
committed
[LeetCode Sync] Runtime - 618 ms (57.39%), Memory - 0.0B (100.00%)
1 parent 476c154 commit 5ea54c1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>Table: <code>Delivery</code></p>
2+
3+
<pre>
4+
+-----------------------------+---------+
5+
| Column Name | Type |
6+
+-----------------------------+---------+
7+
| delivery_id | int |
8+
| customer_id | int |
9+
| order_date | date |
10+
| customer_pref_delivery_date | date |
11+
+-----------------------------+---------+
12+
delivery_id is the column of unique values of this table.
13+
The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
14+
</pre>
15+
16+
<p>&nbsp;</p>
17+
18+
<p>If the customer&#39;s preferred delivery date is the same as the order date, then the order is called <strong>immediate;</strong> otherwise, it is called <strong>scheduled</strong>.</p>
19+
20+
<p>The <strong>first order</strong> of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.</p>
21+
22+
<p>Write a solution to find the percentage of immediate orders in the first orders of all customers, <strong>rounded to 2 decimal places</strong>.</p>
23+
24+
<p>The&nbsp;result format is in the following example.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong>
31+
Delivery table:
32+
+-------------+-------------+------------+-----------------------------+
33+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
34+
+-------------+-------------+------------+-----------------------------+
35+
| 1 | 1 | 2019-08-01 | 2019-08-02 |
36+
| 2 | 2 | 2019-08-02 | 2019-08-02 |
37+
| 3 | 1 | 2019-08-11 | 2019-08-12 |
38+
| 4 | 3 | 2019-08-24 | 2019-08-24 |
39+
| 5 | 3 | 2019-08-21 | 2019-08-22 |
40+
| 6 | 2 | 2019-08-11 | 2019-08-13 |
41+
| 7 | 4 | 2019-08-09 | 2019-08-09 |
42+
+-------------+-------------+------------+-----------------------------+
43+
<strong>Output:</strong>
44+
+----------------------+
45+
| immediate_percentage |
46+
+----------------------+
47+
| 50.00 |
48+
+----------------------+
49+
<strong>Explanation:</strong>
50+
The customer id 1 has a first order with delivery id 1 and it is scheduled.
51+
The customer id 2 has a first order with delivery id 2 and it is immediate.
52+
The customer id 3 has a first order with delivery id 5 and it is scheduled.
53+
The customer id 4 has a first order with delivery id 7 and it is immediate.
54+
Hence, half the customers have immediate first orders.
55+
</pre>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Write your MySQL query statement below
2+
3+
# delivery_id is the primary key
4+
# first order = earliest order date.
5+
# immediate = same order and delivery date
6+
# scheduled = otherwise
7+
8+
# return the percentage of immediate orders for first orders only , ROUND (2)
9+
# % means * 100. ROUND (VALUE * 100, 2)
10+
11+
SELECT
12+
ROUND(
13+
SUM(
14+
order_date = customer_pref_delivery_date -- This will evaluate to 0 or 1
15+
) / COUNT(
16+
DISTINCT customer_id
17+
) * 100, 2
18+
) AS immediate_percentage
19+
FROM
20+
Delivery
21+
WHERE (customer_id, order_date) IN (
22+
SELECT customer_id, MIN(order_date)
23+
FROM Delivery
24+
GROUP BY customer_id
25+
);

0 commit comments

Comments
 (0)