Skip to content

Commit 7a2bcd4

Browse files
committed
task: #1174
1 parent 69dde79 commit 7a2bcd4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Have a good contributing!
7979
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
8080
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
8181
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
82+
- [1174. Immediate Food Delivery II](./leetcode/medium/1174.%20Immediate%20Food%20Delivery%20II.sql)
8283
- [1193. Monthly Transactions I](./leetcode/medium/1193.%20Monthly%20Transactions%20I.sql)
8384
- [1341. Movie Rating](./leetcode/medium/1341.%20Movie%20Rating.sql)
8485
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Question 1174. Immediate Food Delivery II
3+
Link: https://leetcode.com/problems/immediate-food-delivery-ii/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Delivery
6+
7+
+-----------------------------+---------+
8+
| Column Name | Type |
9+
+-----------------------------+---------+
10+
| delivery_id | int |
11+
| customer_id | int |
12+
| order_date | date |
13+
| customer_pref_delivery_date | date |
14+
+-----------------------------+---------+
15+
delivery_id is the column of unique values of this table.
16+
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).
17+
18+
19+
If the customer's preferred delivery date is the same as the order date, then the order is called immediate; otherwise, it is called scheduled.
20+
21+
The first order 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.
22+
23+
Write a solution to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.
24+
*/
25+
26+
WITH first_delivery AS (
27+
SELECT
28+
customer_id,
29+
MIN(order_date) AS first_order_date
30+
FROM Delivery
31+
GROUP BY customer_id
32+
)
33+
34+
SELECT
35+
ROUND(COUNT(
36+
CASE
37+
WHEN d.customer_pref_delivery_date = f.first_order_date
38+
THEN 1
39+
END
40+
) * 100.00 / COUNT(1), 2) AS immediate_percentage
41+
FROM Delivery AS d
42+
LEFT JOIN
43+
first_delivery AS f
44+
ON
45+
d.customer_id = f.customer_id
46+
AND d.order_date = f.first_order_date
47+
WHERE f.first_order_date IS NOT NULL

0 commit comments

Comments
 (0)