小工具知识网

当前位置: 首页 > 行业知识 > 【力扣白嫖日记】1174.即时食物配送II

行业知识

【力扣白嫖日记】1174.即时食物配送II

2026-11-18 20:13:57 来源:互联网转载

前言

���习sql语句,所有题目来自于力扣(https://leetcode.cn/problemset/database/)的免费数据库练习题。

今日题目:

1174.即时食物配送II

表:Person

列名类型
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

delivery_id 是该表中具有唯一值的列。该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。

如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。「首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。

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


我那不值一提的想法:

  • 首先梳理表内容,题干一共给了一张配送表,记录了配送id,顾客id,订单日期指定配送日期。
  • 其次分析需求,需要找到即时订单占全部用户首次订单的比列即时订单,就是订单日期和配送日期相同,否则则称为计划订单
  • 我们需要求的值有首次订单日期
    with min_temp as     (select customer_id,min(order_date) as min_datefrom Deliverygroup by customer_id    )
    • 首次订单总客户
      select count(*) from min_temp
      • 即时订单客户
        select count(*) from Delivery d right join min_temp m on d.order_date = m.min_datewhere d.order_date = d.customer_pref_delivery_date and d.customer_id = m.customer_id
        • 即时订单客户数量/首次订单总客户(完整代码)
          with min_temp as     (select customer_id,min(order_date) as min_datefrom Deliverygroup by customer_id    )select round (count(*) * 100 / (select count(*) from min_temp),2) as immediate_percentagefrom Delivery d right join min_temp m on d.order_date = m.min_datewhere d.order_date = d.customer_pref_delivery_date and d.customer_id = m.customer_id

          结果:


          总结:

          能运行就行。


上一篇:查找服务器排名软件的渠道

下一篇:怎么与服务器连接电脑?