dbTalk Databases Forums  

Get n records grouped by a certain field

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Get n records grouped by a certain field in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sashi
 
Posts: n/a

Default Get n records grouped by a certain field - 12-11-2009 , 09:25 AM






Hi all,

Let's say I have the canonical employee table. Let's say I have a
dept_id field.
Let's say that there are 1000+ employees in each department.

Can it be done via sql that I need to see, say 10 employees from each
department?

select emp_last_name, dept_id
from employee
(but only fetch 10 employees for each department and I do want all
departments selected).

Hope that makes sense.

TIA,
Sashi

Reply With Quote
  #2  
Old   
Charles Hooper
 
Posts: n/a

Default Re: Get n records grouped by a certain field - 12-11-2009 , 10:05 AM






On Dec 11, 10:25*am, Sashi <small... (AT) gmail (DOT) com> wrote:
Quote:
Hi all,

Let's say I have the canonical employee table. Let's say I have a
dept_id field.
Let's say that there are 1000+ employees in each department.

Can it be done via sql that I need to see, say 10 employees from each
department?

select emp_last_name, dept_id
from employee
(but only fetch 10 employees for each department and I do want all
departments selected).

Hope that makes sense.

TIA,
Sashi
Yes, it is possible. Take a look at the following example:
http://hoopercharles.wordpress.com/2...parated-lists/

If you read through that example, take a close look at what is shown
for the output of the first SELECT SQL statement. If you were to
slide that SQL statement into an inline view, you could then add a
WHERE clause like this to retrieve the first 10 employees sorted by
name from each of the departments:
WHERE
RN <= 10

Charles Hooper
Co-author of "Expert Oracle Practices: Oracle Database Administration
from the Oak Table"
http://hoopercharles.wordpress.com/
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.

Reply With Quote
  #3  
Old   
Sashi
 
Posts: n/a

Default Re: Get n records grouped by a certain field - 12-11-2009 , 01:15 PM



On Dec 11, 11:05*am, Charles Hooper <hooperc2... (AT) yahoo (DOT) com> wrote:
Quote:
On Dec 11, 10:25*am, Sashi <small... (AT) gmail (DOT) com> wrote:



Hi all,

Let's say I have the canonical employee table. Let's say I have a
dept_id field.
Let's say that there are 1000+ employees in each department.

Can it be done via sql that I need to see, say 10 employees from each
department?

select emp_last_name, dept_id
from employee
(but only fetch 10 employees for each department and I do want all
departments selected).

Hope that makes sense.

TIA,
Sashi

Yes, it is possible. *Take a look at the following example:http://hoopercharles.wordpress.com/2...ing-generating...

If you read through that example, take a close look at what is shown
for the output of the first SELECT SQL statement. *If you were to
slide that SQL statement into an inline view, you could then add a
WHERE clause like this to retrieve the first 10 employees sorted by
name from each of the departments:
* WHERE
* * RN <= 10

Charles Hooper
Co-author of "Expert Oracle Practices: Oracle Database Administration
from the Oak Table"http://hoopercharles.wordpress.com/
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.
Hi Charles, thanks for the reply. That worked well!
Sashi

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.