I have 2 tables: order and transportation.
ORDER
id
TRANSPORTATION
id
order_id
date
status (could be 'ok', 'ca', 'ko')
1 order can have more than 1 transportation. I want all orders which its latest transportation status is 'OK'
.
If I do:
select ord.*
from orders ord
join transportation tr
on ord.id = tr.order_id
where tr.date = (select max(date) from transportation where status like 'OK');
I will get the latest date of ALL transportations but I only want the latest date of all transportations of that order in specific.
For example, if I have these orders with these transportations and I want the last transportations of each order which status are 'ok':
order_id, transportation_id, date, status
001, 001, 01/01/19, ok
001, 002, 01/01/20, ca
002, 003, 01/01/19, ca
002, 004, 01/01/18, ok
003, 005, 01/01/17, ok
003, 006, 01/01/16, ca
I would expect these results:
003, 005, 01/01/17, ok