Range of records in Top clause

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 40
Reputation: noman78 is an unknown quantity at this point 
Solved Threads: 0
noman78's Avatar
noman78 noman78 is offline Offline
Light Poster

Range of records in Top clause

 
0
  #1
Feb 14th, 2006
Hello
have a good day
is there a way to retrive top records by range , i mean
like this
"select top 15 to 20 record from Table name any condition"
i dont mean top 15 or top 20, i mean records from 15-20
take care
thanking u in advance
Bye
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: Range of records in Top clause

 
0
  #2
Feb 16th, 2006
Hey noman,

You can do this real easy in MySQL. Use the LIMIT and OFFSET keyword. Thats it you are done.

But in MSSQL there is no LIMIT and OFFSET function. Unfortunately there is no other in-build clause provided in MS SQL to do this.But the same feature can be achieved in MS SQL with a little trick.

Suppose you want to write a query to return values from 6 - 8 (6,7,8) ,this can be achieved by TOP clause in MSSQL.

This is very simple,
Results 6-8 would be the first 3 rows of a TOP 3 statement, with a nested query with a TOP 8 statement in the opposite order.

Consider the employees table in NorthWind database,

This query will fetch you rows from 6-8(6,7,8).


  1. SELECT * FROM (SELECT TOP 3 *
  2. FROM (SELECT TOP 8 *
  3. FROM [Employees]
  4. ORDER BY [EmployeeID] ASC) AS tbl1
  5. ORDER BY EmployeeID DESC) AS tbl2
  6. ORDER BY EmployeeID ASC

Note:
1. Use the Alias tbl1,tbl2
2. Tweak the ORDER BY for complex results.
Save White Tiger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC