Skip to content
Advertisement

Oracle Select max where a certain key is matched

i’m working with oracle, plSql, i need to query a table and select the max id where a key is matched, now i have this query

select t.* from (
select  distinct (TO_CHAR(I.DATE, 'YYMMDD') || I.AUTH_CODE || I.AMOUNT || I.CARD_NUMBER)  as kies, I.SID as ids
from transactions I) t group by kies, ids order by ids desc;

It’s displaying this data

enter image description here

If i remove the ID from the query, it displays the distinct keys (in the query i use the alias KIES because keys was in blue, so i thought it might be a reserved word)

enter image description here

How can i display the max id (last one inserted) for every different key without displaying all the data like in the first image??

greetings.

Advertisement

Answer

Do you just want aggregation?

select thekey, max(sid)
from (select t.*,
             (TO_CHAR(t.DATE, 'YYMMDD') || t.AUTH_CODE || t.AMOUNT || t.CARD_NUMBER) as thekey,
             t.SID 
      from transactions t
     ) t
group by thekey
order by max(ids) desc;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement