Solved

Select Top 5 from Table

Posted on 2003-11-19
8
34,411 Views
Last Modified: 2013-12-09
How 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
0
Comment
Question by:matchbx27
8 Comments
 

Author Comment

by:matchbx27
ID: 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;
0
 

Author Comment

by:matchbx27
ID: 9781890
Question Answered
0
 
LVL 2

Expert Comment

by:vowca
ID: 9870188
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)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Expert Comment

by:vowca
ID: 9910571
PS: if q. is answered - plz close the issue.
http://www.experts-exchange.com/Databases/Interbase/Q_20703934.html
Currently You answer is marked as UNanswered.
0
 

Author Comment

by:matchbx27
ID: 9911487
The comments provided by vowca will not work for Interbase 5.5, but I guess thats my fault since I did not specify a database verison.  If you are using Interbase 5.5 you have to use a Stored Procedure.  Please see my comment above.
0
 
LVL 2

Expert Comment

by:vowca
ID: 10422515
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
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10425091
PAQed, with points refunded (250)

PashaMod
Community Support Moderator
0
 

Expert Comment

by:Hornwood
ID: 22711446
Firebird SQL :-

select first 10 * from Table


0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Remote Server returned '400 4.4.7 Message delayed' 3 432
powershell code to list items in dropdown 6 89
free version of MSSQL for over 10GB DB 19 69
SYbase 4 36
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question