1174
获取即时订单在所有用户的首次订单中的比例。
保留两位小数。
配送表: Delivery
+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+
select round(100*sum(order_date = customer_pref_delivery_date)/count(*),2) immediate_percentage
from (select *,
row_number()over(partition by customer_id order by order_date) posn
from Delivery) d
where posn = 1
注意:顾客可能有几个订单在同一最早日期创建。因此,选择排序窗口函数时,row-number()over()最佳。
row_number()over()是连续型排序:9,9,8,7 排序为:1,2,3,4
而 rank()over()是跳跃型排序:9,9,8,7 排序为:1,1,3,4
dense_rank()over()是并列连续型排序:9,9,8,7 排序为:1,1,2,3
标签:customer,+-----------------------------+---------+,over,50,力扣,sql,date,排序,order From: https://blog.csdn.net/yangztcly/article/details/136878405