473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Best way to do a SELECT TOP n TO m

Hi there,

I was wondering if there was a better way to do a SELECT where it returns
not the TOP 5 (or whatever) but, say, the TOP 6 to 8?

What I've currently got is...

SELECT TOP 3 * FROM table WHERE
id NOT IN (SELECT TOP 5 id FROM table ORDER BY id)
ORDER BY id

While this is ok for this (simple) query, it can get quite messy if the
query is more complex. There must be a better way of doing it, mustn't
there?

Thanks for any help :o)

Will


Jul 20 '05 #1
5 8298

"Will Clark" <re****************************@noone-lives-here.com> wrote in
message news:bo**********@sparta.btinternet.com...
Hi there,

I was wondering if there was a better way to do a SELECT where it returns
not the TOP 5 (or whatever) but, say, the TOP 6 to 8?

What I've currently got is...

SELECT TOP 3 * FROM table WHERE
id NOT IN (SELECT TOP 5 id FROM table ORDER BY id)
ORDER BY id

While this is ok for this (simple) query, it can get quite messy if the
query is more complex. There must be a better way of doing it, mustn't
there?
That's generally along the lines of what most folks do.


Thanks for any help :o)

Will

Jul 20 '05 #2
> That's generally along the lines of what most folks do.

Thats a shame - I was hoping that there would be a way which didn't involve
running effectively the same SELECT twice on the table...

Oh, well, thanks all the same :o)
Jul 20 '05 #3
Do:

SELECT *
FROM tbl
WHERE (SELECT COUNT(*)
FROM tbl t1
WHERE t1.col <= tbl.col) BETWEEN 6 AND 8 ;

--
-- Anith
( Please reply to newsgroups only )
Jul 20 '05 #4
Will Clark wrote:
That's generally along the lines of what most folks do.

Thats a shame - I was hoping that there would be a way which didn't involve
running effectively the same SELECT twice on the table...


Hey Will!

I need that a lot for web frontend grid paging.

What I do is: First select only the PKs in question into a temp table or
table variable (with own PK) and make sure that sort order and
contraints are right.

Then select the required rows from the temp table and join to the
"production table" to get the fields you need.

Some example (northwind):

DECLARE @page int
DECLARE @pagesize int
SET @page = 3
SET @pagesize = 10

CREATE TABLE #pager (
OrderID int,
IDpager int identity(1,1)
)

INSERT INTO #pager (OrderID)
SELECT OrderID
FROM Orders
ORDER BY Freight DESC

SELECT IDPager, ShipName, ShipAddress, ShipCity
FROM Orders
INNER JOIN #pager ON Orders.OrderID = #pager.ORderID
WHERE IDPager BETWEEN ((@page-1)*@pagesize + 1) AND (@page*@pagesize)

DROP TABLE #pager
Daniel

Jul 20 '05 #5
Thanks for all your help, Greg, Daniel and Anith - its been really useful,
and I've got lots to play around with now :o)
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Irene | last post by:
Hi all, I have set up a simple VB program (and later on an ASP interface) to manage an Athletics database. I'm using Access 2000. To simplify, I have the Athlets, the Competitions and the...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
1
by: Joe Attardi | last post by:
Hi all, On a form on one of my pages I have two <select> elements, and each one is paired up with a radio button. The idea is to choose an item from one list or the other and select the radio...
18
by: Bob Stearns | last post by:
I'm building a web based auction system for multiple clients. I have auctions, items, and invoices I need to assign generated keys for. Since each of these entities is represented by a table, the...
1
by: Muchach | last post by:
Hello, Ok so what I've got going on is a form that is populated by pulling info from database then using php do{} to create elements in form. I have a text box in each table row for the user to...
2
by: rn5a | last post by:
A Form has a select list which is populated from a MS-Access database table. The DB table from where the select list is populated has 2 columns - CountryID & CountryName. When the Form is posted,...
11
by: ankitmathur | last post by:
Hi, I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns. Example: My...
4
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.