oracle - How can I get the PK of the Max Row in SQL -
i have sql query gets order items. did this:
select max(ab.num) anzahl ( select auf.anr anr,count(*) num auftrag auf, table(auf.positionen) po group auf.anr ) ab ;
my result this:
i want know how order-id [auf.anr] of order. how can modify query desired result?
this order table (auftrag):
one way use row_number
analytic function:
with cte ( select auf.anr anr, count(*) num auftrag auf, table(auf.positionen) po group auf.anr ) select anr (select anr, row_number() on (order num desc) rn cte) rn = 1
... or, using method juan carlos proposing (using rownum
), syntax:
with cte ( select auf.anr anr, count(*) num auftrag auf, table(auf.positionen) po group auf.anr ) select anr (select * cte order num desc) rownum = 1
Comments
Post a Comment