2021-05-19

Immediate Food Delivery

需求一:查询语句获取即时订单所占的百分比, 保留两位小数。

展示效果:

immediate_percentage
42.86
Create table If Not Exists 56_Delivery (delivery_id int, customer_id int, order_date date, customer_pref_delivery_date date);
Truncate table 56_Delivery;
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (1, 1, '2019-08-01', '2019-08-02');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (2, 5, '2019-08-02', '2019-08-02');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (3, 1, '2019-08-11', '2019-08-11');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (4, 3, '2019-08-24', '2019-08-26');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (5, 4, '2019-08-21', '2019-08-22');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (6, 2, '2019-08-11', '2019-08-13');
insert into 56_Delivery (delivery_id, customer_id, order_date, customer_pref_delivery_date) values (7, 4, '2019-08-09', '2019-08-09');

最终SQL:

SELECT
	round(sum(number)/count(delivery_id)*100,2) immediate_percentage
FROM(
	SELECT
		delivery_id,
		customer_id,
		order_date,
	IF(order_date = customer_pref_delivery_date,1,0 ) number
	FROM
		56_delivery
) t1

需求二:查询语句获取即时订单在所有用户的首次订单中的比例。保留两位小数。

展示效果:

immediate_percentage
40.00

最终SQL:

SELECT
	round(
	(select 
	count(customer_id)
from 
	56_delivery
where 
	order_date = customer_pref_delivery_date) / 
 	(SELECT
	count(DISTINCT customer_id)
	FROM
	(select 
	customer_id,
	order_date
from 
	56_delivery
GROUP BY
	customer_id ,order_date) t1)*100,2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值