select A,B,C from(
select cinvcode A,ddate B,dnmaketime C,
row_number() over(partition by cinvcode order by ddate desc,dnmaketime desc) as rn
from rdrecords10 a
inner join rdrecord10 b on a.ID=b.ID
) t where rn=1
WITH MO AS (
select cinvcode A,ddate B,dnmaketime C
from rdrecords10 a
inner join rdrecord10 b on a.ID=b.ID )
SELECT * FROM MO H
WHERE 1=1
AND NOT EXISTS(SELECT 1 FROM MO Y WHERE H.A = Y.A AND (H.B<Y.B OR H.C<Y.C))