题目
配送表: Delivery
+-----------------------------+---------+
| Column Name | Type |
+-----------------------------+---------+
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
+-----------------------------+---------+
delivery_id 是表的主键。
该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。
如果顾客期望的配送日期和下单日期相同,则该订单称为 「即时订单」,否则称为「计划订单」。
「首次订单」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。
写一条 SQL 查询语句获取即时订单在所有用户的首次订单中的比例。保留两位小数。
查询结果如下所示:
Delivery 表:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 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 |
+-------------+-------------+------------+-----------------------------+
Result 表:
+----------------------+
| immediate_percentage |
+----------------------+
| 50.00 |
+----------------------+
1 号顾客的 1 号订单是首次订单,并且是计划订单。
2 号顾客的 2 号订单是首次订单,并且是即时订单。
3 号顾客的 5 号订单是首次订单,并且是计划订单。
4 号顾客的 7 号订单是首次订单,并且是即时订单。
因此,一半顾客的首次订单是即时的。
解答
先得到首次订单
还是利用分组rank的方法
select *, @rank:=if(@pre_cid = D.customer_id, @rank +1, 1), @pre_id:=D.customer_id
from Delivery as D, (select @rank:=0, @pre_cid:=NULL) as init
order by D.customer_id, D.order_date ASC;
对于上述临时表选取rank为1的即为首次订单
select tmp.customer_id, tmp.order_date, tmp.customer_pref_delivery_date
from (select *, @rank:=if(@pre_cid = D.customer_id, @rank +1, 1) as rank, @pre_id:=D.customer_id
from Delivery as D, (select @rank:=0, @pre_cid:=NULL) as init
order by D.customer_id, D.order_date ASC) as tmp
where tmp.rank =1;
两个date相等的订单数量除以总数量即可
select round(sum(tmp.order_date = tmp.customer_pref_delivery_date)/count(tmp.customer_id)*100,2) as immediate_percentage
from (select *, @rank:=if(@pre_cid = D.customer_id, @rank +1, 1) as rank, @pre_id:=D.customer_id
from Delivery as D, (select @rank:=0, @pre_cid:=NULL) as init
order by D.customer_id, D.order_date ASC) as tmp
where tmp.rank =1;
其实这里首次订单 分组 求min即可
但这样更便于推广到 第二次订单之类
别的方法
方法一、
选出每个id首次订单的日期
select customer_id,min(order_date) order_date
from Delivery
group by customer_id
内连接选出首次订单的信息
select *
from Delivery d join (
select customer_id,min(order_date) order_date
from Delivery
group by customer_id
) w on d.customer_id=w.customer_id and d.order_date=w.order_date
首次订单中即时订单的数量
select count(*)
from Delivery d join (
select customer_id,min(order_date) order_date
from Delivery
group by customer_id
) w on d.customer_id=w.customer_id and d.order_date=w.order_date
where d.order_date=d.customer_pref_delivery_date;
除以总id数即可
select round(100*count(*)/@num_cus,2) immediate_percentage
from Delivery d join (
select customer_id,min(order_date) order_date
from Delivery
group by customer_id
) w on d.customer_id=w.customer_id and d.order_date=w.order_date
,(select @num_cus:=count(distinct customer_id) from Delivery) t
where d.order_date=d.customer_pref_delivery_date;
方法二、
如果最小的订单日期等于最小的期望配送日期 则为首次订单中的即时订单
select customer_id as id, case
if(min(order_date) = min(customer_pref_delivery_date), 1, 0) as tag
from Delivery d1
group by customer_id
然后tag的和除以总数量即可
select round(100* sum(tag)/count(id),2) as immediate_percentage
from
(
select customer_id as id, case
when min(order_date) = min(customer_pref_delivery_date)
then 100.00
else 0 end as tag
from Delivery d1
group by customer_id
) d2;