Skip to content
Advertisement

I tried solving this problem by using MAX function but get the result . Is there a way to solve it by using any other function?

I need to get “second updated_time” for each order and sorting them by “update_time”. I wrote the following query, which is not giving any output.

Suppose, we have to calculate get the time difference between second updated time and first updated time against each order_id ,if we sort the table on update_time (same question but extended) Server is Mysql. The TIMEDIFF() function will be applied, but how? It am trying this:

select order_history.,order_history.TIMEDIFF( from (select order_history., row_number() OVER (partition by order_id order by update_time) as seqnum from order_history ) order_history where seqnum = 2 order by update_time;

select update_time <MAX(update_time) from order_history order by update_time desc;

[The attached image shows the orders_history (table for the problem)]

Advertisement

Answer

I need to get “second updated_time” for each order and sorting them by “update_time”.

This sounds like:

select oh.*
from (select oh.*,
             row_number() over (partition by order_id order by update_toime) as seqnum
      from order_history oh
     ) oh
where seqnum = 2
order by update_time;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement