Oracle sql最后取消记录

时间:2013-03-04 12:49:26

标签: oracle plsql

我有以下数据:

顺序

order_id    order_name        order_date         order_status 

1            iphone        20130102 13:20:00        cancelled
1            blackberry    20130102 13:00:00        cancelled
1            ipad          20130102 13:00:00        cancelled

person_id     person_name       order_id

1             harshini          1

我想在20130102 13:00:00到2013 13:20:00之间根据order_date查询以下数据。这意味着最后取消订单。

person_name     order_name   order_date     

harshini        blackberry   20130102 13:00:00

2 个答案:

答案 0 :(得分:0)

试试这个......
select p.person_name ,o.order_name,o.order_status from order_1 o,person p where orderdate=(select max(orderdate) from order_1)

答案 1 :(得分:0)

select person_name, order_name, order_date 
from(
  select 
   o.order_id, o.order_name, o.order_date, o.order_status,
   p.person_id, p.person_name, p.order_id,
   row_number() over (/*partition by person_id*/ order by order_date desc) as rnk
  from order o join person p on (o.order_id = p.order_id)
  where o.order_status = 'canceled'
   and o.order_date between 
       to_date('20130102 13:00:00','yyyymmdd hh24:mi:ss') and
       to_date('20130102 13:00:00','yyyymmdd hh24:mi:ss')
)
where rnk = 1;

但请参阅评论,您的设计中存在一些问题。