MYSQL窗口函数

时间:2018-06-25 08:57:44

标签: mysql rank

订单在最终交付给客户之前具有多个order_status。有时,客户从不同的来源下了多个订单。我们接受一个订单,但拒绝另一个订单。我想找出取消重复订单时接受订单的最新订单状态。

LEFT JOIN(
            SELECT order_id AS order_id,order_status,max(addtime(`timestamp`,'05:30:00')) AS timestamp
            FROM pe2.order_history
            GROUP BY 1,2
) oh ON oh.order_id = ord.order_id AND oh.timestamp <= c.time. 
c.time. is duplicate order cancellation time.

1 个答案:

答案 0 :(得分:0)

我们获得了所需的客户订单,然后获得了同一订单的最新状态,该状态在取消重复订单之前:

SELECT order.*,
(SELECT order_status FROM order_history AS oh 
WHERE order_id = order.id AND timestamp <= cancel_time
ORDER BY timestamp DESC LIMIT 1) AS last_status
FROM order
WHERE order.ID = accepted_order