Solved Using SQL to select only 2nd record (1 Viewer)

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:57
Joined
Feb 19, 2002
Messages
43,275
How about select Top 2 and sort descending? Then just process the first record.
 

ebs17

Well-known member
Local time
Today, 10:57
Joined
Feb 7, 2020
Messages
1,946
Yes, ech TOP needs its own ORDER BY.
I already wrote that above.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:57
Joined
Feb 28, 2001
Messages
27,186
If there is an ID field,

Code:
SELECT TOP 1 T.X, T.Y, T.Z FROM TBL1 AS T
WHERE T.ID NOT IN
(SELECT TOP 1 TS.ID FROM TBL1 AS TS ORDER BY TS.ID)
ORDER BY T.ID ;

This ONLY works if (a) you have a key field like ID and (b) your "TOP" criteria was based on sorting on the ID field.

As was pointed out, you cannot have a "TOP" unless you specify an ordering. Otherwise, TOP has no meaning. More precisely, TOP without an order is essentially a random selection since tables are unordered sets.

If the ordering is based on a no-dups field, the above should do the trick. Basically, get rid of the first record that would be found by the TOP modifier and take the next top.

EDIT: If you are doing your ordering on a data field that isn't a primary key, the question becomes ambiguous if the ordering field is not unique.
EDIT 2: Fixed placement of ORDER BY clauses.
 
Last edited:

Users who are viewing this thread

Top Bottom