|
Hi, I can provide a solution, if you are already on release V5R4. If you are not yet on release V5R4, I'm not aware of an easier way than writing and RPG program with embedded SQL or an stored procedure or UDTF with SQL. On release V5R4 you may try the following: with x as (select Row_Number() over(Partition By Region Order By Sales Desc) RowNbr, Region, Sales From Table Where ....) Select * from x Where RowNbr <= 5; Mit freundlichen Grüßen / Best regards Birgitta Hauser "Shoot for the moon, even if you miss, you'll land among the stars." (Les Brown) "If you think education is expensive, try ignorance." (Derek Bok) "What is worse than training your staff and losing them? Not training them and keeping them!" -----Ursprüngliche Nachricht----- Von: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von Mike Pantzopoulos - (H/O) Gesendet: Wednesday, February 14, 2007 04:26 An: midrange-l@xxxxxxxxxxxx Betreff: Fetch n rows from each group by. I have used the FETCH n ROWS ONLY clause to return the first n records in a select statement, but I now want to return the first n from each group, and I'm damned if I can find any reference material for this. Can it be done? In essence I want to return the top 5 agents from each region based on sales. The following pseudo-SQL would show all the agents in reverse sales order within region: Select region, sales from file order by region, sales desc If I want to select the first 5 in the entire return set I use: Select region, sales from file order by region, sales desc fetch first 5 rows only How do I write the SQL to return only the first 5 in each region without having to use RPG? TIA **************************************************************************** ***************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient, any use, disclosure or copying of this message is unauthorised. If you have received this message in error, please reply using the sender's email address. This footnote confirms that this email message has been scanned for computer viruses. EIG-Ansvar Limited does not accept liability for any loss or damage, whether caused by our own negligence or not, that results from a computer virus or defect in the transmission of this email or any attached file. EIG-Ansvar Limited - Australia (A.B.N. 21 007 216 506) Email : insure@xxxxxxxxxxxxxxxx Eig-Ansvar Limited - New Zealand Email : insure@xxxxxxxxxxxxxxxx **************************************************************************** *****************************
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.