|
| 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> </p> |
| 17 | + |
| 18 | +<p>If the customer'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 result format is in the following example.</p> |
| 25 | + |
| 26 | +<p> </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> |
0 commit comments