{ "data": { "question": { "questionId": "1292", "questionFrontendId": "1174", "categoryTitle": "Database", "boundTopicId": 33159, "title": "Immediate Food Delivery II", "titleSlug": "immediate-food-delivery-ii", "content": "

Table: Delivery

\n\n
\n+-----------------------------+---------+\n| Column Name                 | Type    |\n+-----------------------------+---------+\n| delivery_id                 | int     |\n| customer_id                 | int     |\n| order_date                  | date    |\n| customer_pref_delivery_date | date    |\n+-----------------------------+---------+\ndelivery_id is the column of unique values of this table.\nThe 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).\n
\n\n

 

\n\n

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.

\n\n

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.

\n\n

Write a solution to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.

\n\n

The result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nDelivery table:\n+-------------+-------------+------------+-----------------------------+\n| delivery_id | customer_id | order_date | customer_pref_delivery_date |\n+-------------+-------------+------------+-----------------------------+\n| 1           | 1           | 2019-08-01 | 2019-08-02                  |\n| 2           | 2           | 2019-08-02 | 2019-08-02                  |\n| 3           | 1           | 2019-08-11 | 2019-08-12                  |\n| 4           | 3           | 2019-08-24 | 2019-08-24                  |\n| 5           | 3           | 2019-08-21 | 2019-08-22                  |\n| 6           | 2           | 2019-08-11 | 2019-08-13                  |\n| 7           | 4           | 2019-08-09 | 2019-08-09                  |\n+-------------+-------------+------------+-----------------------------+\nOutput: \n+----------------------+\n| immediate_percentage |\n+----------------------+\n| 50.00                |\n+----------------------+\nExplanation: \nThe customer id 1 has a first order with delivery id 1 and it is scheduled.\nThe customer id 2 has a first order with delivery id 2 and it is immediate.\nThe customer id 3 has a first order with delivery id 5 and it is scheduled.\nThe customer id 4 has a first order with delivery id 7 and it is immediate.\nHence, half the customers have immediate first orders.\n
\n", "translatedTitle": "即时食物配送 II", "translatedContent": "

配送表: Delivery

\n\n
\n+-----------------------------+---------+\n| Column Name                 | Type    |\n+-----------------------------+---------+\n| delivery_id                 | int     |\n| customer_id                 | int     |\n| order_date                  | date    |\n| customer_pref_delivery_date | date    |\n+-----------------------------+---------+\ndelivery_id 是该表中具有唯一值的列。\n该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。\n
\n\n

 

\n\n

如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。

\n\n

首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。

\n\n

编写解决方案以获取即时订单在所有用户的首次订单中的比例。保留两位小数。

\n\n

结果示例如下所示:

\n\n

 

\n\n

示例 1:

\n\n
\n输入:\nDelivery 表:\n+-------------+-------------+------------+-----------------------------+\n| delivery_id | customer_id | order_date | customer_pref_delivery_date |\n+-------------+-------------+------------+-----------------------------+\n| 1           | 1           | 2019-08-01 | 2019-08-02                  |\n| 2           | 2           | 2019-08-02 | 2019-08-02                  |\n| 3           | 1           | 2019-08-11 | 2019-08-12                  |\n| 4           | 3           | 2019-08-24 | 2019-08-24                  |\n| 5           | 3           | 2019-08-21 | 2019-08-22                  |\n| 6           | 2           | 2019-08-11 | 2019-08-13                  |\n| 7           | 4           | 2019-08-09 | 2019-08-09                  |\n+-------------+-------------+------------+-----------------------------+\n输出:\n+----------------------+\n| immediate_percentage |\n+----------------------+\n| 50.00                |\n+----------------------+\n解释:\n1 号顾客的 1 号订单是首次订单,并且是计划订单。\n2 号顾客的 2 号订单是首次订单,并且是即时订单。\n3 号顾客的 5 号订单是首次订单,并且是计划订单。\n4 号顾客的 7 号订单是首次订单,并且是即时订单。\n因此,一半顾客的首次订单是即时的。\n
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 77, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef immediate_food_delivery(delivery: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"22K\", \"totalSubmission\": \"39.6K\", \"totalAcceptedRaw\": 21991, \"totalSubmissionRaw\": 39586, \"acRate\": \"55.6%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Delivery\":[\"delivery_id\",\"customer_id\",\"order_date\",\"customer_pref_delivery_date\"]},\"rows\":{\"Delivery\":[[1,1,\"2019-08-01\",\"2019-08-02\"],[2,2,\"2019-08-02\",\"2019-08-02\"],[3,1,\"2019-08-11\",\"2019-08-12\"],[4,3,\"2019-08-24\",\"2019-08-24\"],[5,3,\"2019-08-21\",\"2019-08-22\"],[6,2,\"2019-08-11\",\"2019-08-13\"],[7,4,\"2019-08-09\",\"2019-08-09\"]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)\"],\"mssql\":[\"Create table Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)\"],\"oraclesql\":[\"Create table Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"immediate_food_delivery\",\"pythondata\":[\"Delivery = pd.DataFrame([], columns=['delivery_id', 'customer_id', 'order_date', 'customer_pref_delivery_date']).astype({'delivery_id':'Int64', 'customer_id':'Int64', 'order_date':'datetime64[ns]', 'customer_pref_delivery_date':'datetime64[ns]'})\"],\"postgresql\":[\"\\nCreate table If Not Exists Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)\"],\"database_schema\":{\"Delivery\":{\"delivery_id\":\"INT\",\"customer_id\":\"INT\",\"order_date\":\"DATE\",\"customer_pref_delivery_date\":\"DATE\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date)", "Truncate table Delivery", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('1', '1', '2019-08-01', '2019-08-02')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('2', '2', '2019-08-02', '2019-08-02')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('3', '1', '2019-08-11', '2019-08-12')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('4', '3', '2019-08-24', '2019-08-24')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('5', '3', '2019-08-21', '2019-08-22')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('6', '2', '2019-08-11', '2019-08-13')", "insert into Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values ('7', '4', '2019-08-09', '2019-08-09')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"

PostgreSQL 16<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"Delivery\":[\"delivery_id\",\"customer_id\",\"order_date\",\"customer_pref_delivery_date\"]},\"rows\":{\"Delivery\":[[1,1,\"2019-08-01\",\"2019-08-02\"],[2,2,\"2019-08-02\",\"2019-08-02\"],[3,1,\"2019-08-11\",\"2019-08-12\"],[4,3,\"2019-08-24\",\"2019-08-24\"],[5,3,\"2019-08-21\",\"2019-08-22\"],[6,2,\"2019-08-11\",\"2019-08-13\"],[7,4,\"2019-08-09\",\"2019-08-09\"]]}}", "__typename": "QuestionNode" } } }