Re: Query to return record at Nth physical location in table
- From: "Chris2" <rainofsteel.NOTVALID@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 18 Oct 2006 22:32:24 -0700
"quartz" <quartz@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:15DD8372-88D7-45E5-91D0-900635610FC2@xxxxxxxxxxxxxxxx
Dorian,
Thanks for your post. Then I assume you can't help.
Does anyone else have an answer?
Dorian,
There is no *good* answer to your question.
Sets (i.e. tables), as David Cox mentioned, are unordered.
Using a query to locate every Nth row in a table isn't a question
SQL is meant to answer.
If you have a compelling reason to select every Nth row, you may
consider iterating through a recordset using VBA, but without an
artificial sequence to depend upon in one column (also as
mentioned), even that may be different at any time.
Also, here is an example of how to create an artificial repeating
ascending number sequence.
You can can run a new query against the query below with "WHERE
NumberSeq = <your Nth value>".
(Note that this also depends on the ascending sequence in TitleID to
work at all.)
CREATE TABLE Titles
(TitleID AUTOINCREMENT
,TitleName TEXT(5)
,CONSTRAINT pk_Titles
PRIMARY KEY (TitleID)
)
Sample Data:
1, Jim
2, Jan
3, John
4, Al
5, Frank
6, Sam
7, Kate
8, Sally
9, Susie
10, Amy
11, Kevin
12, Phil
Query:
SELECT T1.TitleName
,(SELECT SWITCH(COUNT(T1.TitleID) MOD 3 = 1, 1
,COUNT(T1.TitleID) MOD 3 = 2, 2
,COUNT(T1.TitleID) MOD 3 = 0, 3)
FROM Titles AS T01
WHERE T01.TitleID <= T1.TitleID)
AS NumberSeq
FROM Titles AS T1
Results:
TitleName, NumberSeq
Jim, 1
Jan, 2
John, 3
Al, 1
Frank, 2
Sam, 3
Kate, 1
Sally, 2
Susie, 3
Amy, 1
Kevin, 2
Phil, 3
Sincerely,
Chris O.
.
- Prev by Date: Re: Group by and Sum in same query
- Next by Date: Re: Pull a value from previous record for query
- Previous by thread: Re: Query to return record at Nth physical location in table
- Next by thread: Crosstab Query/Report
- Index(es):
Relevant Pages
|