Solved

ansi sql equivalent to t-sql "top"

Posted on 2012-03-13
29
889 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 74

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 74

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 74

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 74

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 74

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
 
LVL 74

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 74

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 74

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 74

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 74

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query to calculate avaerage 21 52
Help with Oracle IF statment 5 26
Query for timesheet application 3 18
SSRS: Why is Visual Studio stripping these properties? 2 23
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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