Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> SQL: Top-N with a twist?

SQL: Top-N with a twist?

From: Vikas Agnihotri <onlyforposting_at_yahoo.com>
Date: 9 Jul 2001 13:57:50 -0700
Message-ID: <77e87b58.0107091257.7abb5d00@posting.google.com>

Need help with the following:

One table with (partno,empno) of a part number and the employee who sold that part number. One emp can sell many parts and one part can be sold by many emps.

How do I get the partno and the employee who sold the largest number of those parts?

select partno,empno,count(*) from table
order by 3 desc

My final desired output is the first row in the above query for each (partno,empno). In other words, the row with the maximum count(*) for each (partno,empno)

Something like

select partno,min(empno) from
 (select partno,empno,count(*) emp_count from table) where emp_count=(select max(emp_count) from the above inline query) group by partno

What is the most efficient way of doing this? Use temporary tables to store intermediate results? Would Oracle 8i's new 'order by' in inline view feature help here?

Thanks... Received on Mon Jul 09 2001 - 15:57:50 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US