每个订单的最新状态以及何时设置为该值

时间:2018-11-09 06:16:26

标签: sql window-functions

我正在尝试查找每个订单的最新状态以及将其设置为该值时的状态。我在下面尝试了查询,但是发生了this。我只希望它显示最新状态,而不是全部显示。非常感谢!

select order_id, updated_at, order_status
from (select order_id, 
        updated_at, 
        order_status,
        row_number() over(partition by order_id, order_status order by updated_at desc)
          as rn
     from fishtownanalytics.order_status_history) as x
where rn = 1

1 个答案:

答案 0 :(得分:1)

只需从分区中删除状态,它将起作用

select order_id, updated_at, order_status
from (select order_id, 
        updated_at, 
        order_status,
        row_number() over(partition by order_id  order by updated_at desc)
          as rn
     from fishtownanalytics.order_status_history) as x
where rn = 1