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:

enter image description here

i want know how order-id [auf.anr] of order. how can modify query desired result?

this order table (auftrag):

enter image description here

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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -