943,794 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 25668
  • MS SQL RSS
Feb 14th, 2006
0

Range of records in Top clause

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
noman78 is offline Offline
40 posts
since Nov 2005
Feb 16th, 2006
1

Re: Range of records in Top clause

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).


MS SQL Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 6
Junior Poster
Letscode is offline Offline
175 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: need help optimizing
Next Thread in MS SQL Forum Timeline: effective user database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC