Solved

ansi sql equivalent to t-sql "top"

Posted on 2012-03-13
29
834 Views
Last Modified: 2012-07-24
How would you go about writing the ansi equivalent to t-sql's select top n * from tableName?
0
Comment
Question by:_Wade_
  • 10
  • 8
  • 6
  • +1
29 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 37715662
LIMIT 10
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37715663
which level of ansi sql?

in sql2003 you get window functions like row_number()
sql2008 has fetch first
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37715671
LIMIT 10 is mysql flavor

I would say,

Select top 10 fieldname from your table
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37715676
anujnb,

 I don't think the LIMIT clause is part of any ANSI standard as yet.
 If it is, can you specify which one?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37715683
sammySeltzer,

the asker was looking for an ANSI equivalent to sqlserver TOP
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37715685
LIMIT is also postgres db dialect
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37715692
ok, I see sdstuber
0
 
LVL 15

Expert Comment

by:Anuj
ID: 37715707
Yes, that's right LIMIT is of mysql and it will not work for SQL Server, as @sdstuber suggested you have to use window functions like ROW_NUMBER(), RANK() etc. but these are supported only on SQL Server 2005 and above, for older versions you have to use TOP clause.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37715726
LIMIT is not part of ANSI/ISO SQL   depending on the version (up to 2008)

If the asker is looking for sql92  standards then there isn't a way except to procedurally iterate N times through a result set.

If the asker is looking for a later sql standard, then there might be options, which ones, again, depends on the version of ansi defined.

Then, of course, the real trick is whether that standard has been implemented in the target database

I'm sure we could all name umpteen different ways to do it and umpteen databases where each of those methods would fail.  However, none of that is particularly relevant to the question which is what are the ANSI methods.
0
 

Author Comment

by:_Wade_
ID: 37715777
I'm querying a DB2 database from .NET with oledb. Offhand I don't know what version of ANSI SQL it conforms to.

I've tried some of the suggestions though and none work so far. I might be using the wrong syntax.

select * from bsydtad.docdvlf8 where now_number() = 4 ...didn't work

How do I use "LIMIT"?
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 37715797
DB2 supports FETCH FIRST


select  * from tableName
fetch first 10 rows only
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37715799
LIMIT will not work for DB2.

It is more like

select * from bsydtad.docdvlf8 where someid = someid order by someid
fetch 10 rows only;

something like that.
0
 
LVL 15

Accepted Solution

by:
Anuj earned 250 total points
ID: 37715800
For DB@ its Fetch First

  SELECT *
  FROM Table
  ORDER BY EmpID
  FETCH FIRST 10 ROWS ONLY
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 73

Expert Comment

by:sdstuber
ID: 37715810
db2 does support row_number too,

using it would look something like this...


select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37715812
i missed the first in error, sorry.

typing fast
0
 

Author Comment

by:_Wade_
ID: 37715814
ahh...

SELECT column_name(s)
FROM table_name
LIMIT number

Yeah that's MySql
0
 

Author Comment

by:_Wade_
ID: 37715829
select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4

..in this one is the "something" in over(order by something) supposed to be a unique field?>
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37715847
it doesn't have to be unique, but if it is, it'll ensure your ordering is repeatable.


for example

X Y
2 a
3 a
5 a
7 a
11 a
13 a


if you order by Y, then your results could be anything.  If you order by X then your results will be consistent and meaningful

the ordering is required so the function can pick a row to be #1, #2, #3 etc
0
 

Author Comment

by:_Wade_
ID: 37716406
When I try that I'm getting the following error back. Apparently the row_number() is not supported in the version of DB2 we have. Is there another way?

ERROR: [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0255 - Function
not supported for query.
Error Code: -255

Query = select
* from (
select d.*, row_number() over(order by doc_id)
 as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37716420
use FETCH FIRST as shown above

http:#a37715797
0
 

Author Comment

by:_Wade_
ID: 37716696
Thanks, sorry didn't try that one at first.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37716819
wouldn't a split be in order?

I posted the same thing earlier.  

Also, the question asked was about ANSI SQL not db2
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37718231
Actually, everyone posted similar solutions ; go figure!
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37718385
I agree, the db2 bit can be split, I happened to posted first, but they all came in about the same time.  Picking only the last one isn't appropriate.

However, while one of those was the accepted answer, none of db2 references really answer the question that was asked.  Those are really just supplemental info.

So, my point was, if the decision is to ignore the original question about ANSI SQL then I think a split is in order rather than picking the last post that mentioned the db2 feature.

Or, reevaluate the whole thread and split differently among the posts that actually discussed the original question.
0
 

Author Comment

by:_Wade_
ID: 37722656
I agree sdstuber. I've been under the weather these past few days and have some projects due and I awarded the points very hastily without reviewing the whole post again first. I'll have a look again and split the points. You gave a lot of useful feedback.
0
 

Author Comment

by:_Wade_
ID: 37722667
_alias99,

Can the thread be reopened please for point reallocation?
0
 

Author Closing Comment

by:_Wade_
ID: 38219717
Sorry this took so long...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

10 Experts available now in Live!

Get 1:1 Help Now