如何选择最后3行但最后3行

时间:2014-11-25 10:53:56

标签: sql iso

SELECT doesntmatterwhat
FROM whatever
OFFSET 3 LIMIT 2

我如何重现这一点,但没有使用LIMITOFFSET来选择最后3行而是最后一行

2 个答案:

答案 0 :(得分:1)

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

不使用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;

答案 1 :(得分:1)

这是我正在寻找的解决方案。谢谢你的帮助。

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