Solved

ANSI Compliant keyword for TOP N clause

Posted on 2004-09-02
5
293 Views
Last Modified: 2012-08-14
Need to find ANSI-92 compliant keyword for the TOP (n) keyword used in SQL Server Transact SQL?
0
Comment
Question by:dbatrainer
  • 3
5 Comments
 
LVL 10

Expert Comment

by:Jay Toops
Comment Utility
select foo, bar
  from yourtable X
 where ( select count(*)
           from yourtable  
          where foo > X.foo ) < 10

Jay
0
 
LVL 11

Expert Comment

by:SweatCoder
Comment Utility
use LIMIT in MySQL and maybe others as well.
0
 
LVL 10

Expert Comment

by:Jay Toops
Comment Utility
Which Database are you using??

Jay
0
 

Author Comment

by:dbatrainer
Comment Utility
comment to JLtoops from dbatrainer.

You're right a subquery would work, but it's resource intensive and I was hoping to find a universal ANSI compliant clause that works the same as TOP (n) for a hetergenous environment including SQL Server 2000, Oracle, Dbase, and others. One that might also allow (with ties).

Thanks for the reply
0
 
LVL 10

Accepted Solution

by:
Jay Toops earned 500 total points
Comment Utility
There isn't one at least in ANSI-92 ..
thats why i gave you the count query.
thats the most UNIVERSALLY likely to work
... unfortunately its a resource pig.

the other way i thought of was to use a cursor.
But I very much doubt that it would be less expensive than
the query i just gave you.

Jay
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now