woutwoot
woutwoot

Reputation: 152

How to select the last 3 rows but the last one

SELECT doesntmatterwhat
FROM whatever
OFFSET 3 LIMIT 2

How would I reproduce this, but WITHOUT using LIMIT or OFFSET to select the last 3 rows but the last one

Upvotes: 2

Views: 115

Answers (2)

woutwoot
woutwoot

Reputation: 152

This was the solution I was looking for. Thanks for helping though.

SELECT  doesntmatterwhat
FROM    whatever
ORDER BY 1
OFFSET  3 ROWS
FETCH   FIRST 2 ROWS ONLY;

Upvotes: 1

Matt
Matt

Reputation: 15061

SELECT TOP 2 FROM (
SELECT TOP 3 doesntmatterwhat
FROM whatever
) a
ORDER BY doesntmatterwhat

Not using TOP

SELECT n.doesntmatterwhat
FROM (SELECT n.doesntmatterwhat, row_number() OVER (ORDER BY date DESC) AS sequence
  FROM whatever n
 ) n
WHERE n.sequence>= 2 AND n.sequence<= 3;

Upvotes: 1

Related Questions