Question Answered
Main Topics
Browse All TopicsHow do you select the Top n records in a query or Stored Procedure.
my current Stored Procedure is as follows:
create Procedure nspTopDTMAchines (TopRecords Integer)
Returns (MachineName Varchar(10), MachDownTime Integer)
as
begin
for
Select dtd.MachineName, Sum(dtd.DownTime)
FROM nvDownTimeDetails dtd
GROUP BY dtd.MachineName
ORDER BY 2 DESC
Into :MachineName, :MachDownTime
do suspend;
end;
I need to use the parameter TopRecords to determine how many Records to return.
Thanks,
matchbx
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yaffil and Firebird can use the following pattern:
SELECT [FIRST(10)] [SKIP(2)] <rows> FROM ....
IB 7.x (and maybe 6.5) has a similar compatibility bat looks something alike SELECT .......... ROWS 3 TO 13
It is now discussed if FB 2.x will have SQL standard ROWS option for select (IB's one is not, nor is FIRST/SKIP)
Also remember that those boundaries are applied after select is started fetching - hence it will not change its plan, it will not speed up the very select (but may speed up network transmition just because there will be less data to transfer)
PS: if q. is answered - plz close the issue.
http://www.experts-exchang
Currently You answer is marked as UNanswered.
Points are given back.
as: upgrade at least for 5.6! 5.x is known for its bugs, even i stumbled acrross some of them.
Then give a try to Yaffil. Amount of bugs found in IB6.0 and prior is just shoking.
Indeed, last reason for merging Firebird and Yaffil was that their teams together hunted for ForeignKey corruption bug, that was present at least in IB 4.0
And Yaffil has special measures to be compatible with 5.x generation.
Now one more tweak - use generator - it may work bad for concurrent transactions but sometimes it helps.
1st i set generator to some value:
select gen_id(G_RUL, -gen_id(g_rul, 0) /* drom to zero */ + /* new value*/ 10) from rdb$database
( SET GENERATOR might be taken for metadata change and force backup/restore)
Now:
select * from LOG
where /* some conditions AND */ gen_id(G_RUL, -1) >= 0
PS: to be true, this hack filters ouptput, but IB still tries on record after another, so, say, for 1000000 rows table it will be rather long anyway.
The records would not be sent to client, but will be tried by server kernel.
One more rook is that i do not know, what is executed first - order by or checking for where
Business Accounts
Answer for Membership
by: matchbx27Posted on 2003-11-19 at 12:04:02ID: 9781888
Here's the Solution:
create Procedure nspTopDTMAchines (TopRecords Integer)
Returns (MachineName Varchar(10), MachDownTime Integer)
as
DECLARE VARIABLE iCount INTEGER;
begin
iCount = 0;
for
Select dtd.MachineName, Sum(dtd.DownTime)
FROM nvDownTimeDetails dtd
GROUP BY dtd.MachineName
ORDER BY 2 DESC
Into :MachineName, :MachDownTime
DO
BEGIN
iCount = iCount + 1;
IF (iCount <= :TopRecords) THEN
SUSPEND;
ELSE
EXIT;
END
suspend;
end;