DB2 SQL select x number of rows per grouping

Tags:
SQL
How do I select only x number of rows per group in a db2 sql query? For instance, I'm group records by company and division. I only want to retrieve 5 records for each division within each company. If I use "Fetch first 5 rows only", it limits the overall query results to 5 records. Is thera a way to get 5 records for each group?

Answer Wiki

Thanks. We'll let you know when a new response is added.

I would build a cursor of company names and divisions, ordered of course. Using that data I would exact match, eliminating the group clause, my experience was that is inefficeint, using a second cursor extracting only the data you need. This places the count in the programs hands. When done extracting the required information, close the second cursor, and proceed to the next record on the first.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following