You can use this method, but you need a correlated subquery:
SELECT co.order_no, co.chg_order_no,co.line_no, co.amount
FROM CHANGE_ORDER co
WHERE co.chg_order_no = (select MAX(co2.chg_order_no)
from CHANGE_ORDER co2
where co.order_no = co2.order_no
);
Your version returns the maximum chg_order_no
over all the data. However, you only want it for each order_no
.
There are many ways to accomplish this. But Oracle has new functionality that lets you avoid a subquery:
select co.*
from change_order co
order by row_number() over (partition by order_no order by chg_order_no desc)
fetch first 1 row with ties;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…