我知道如何获得小组的最后一个记录,但我还需要获得倒数第二个记录。我怎样才能做到这一点?这是我获取最后一条记录的代码。
select job qjob, max(id) qid from sqbclog group by job
答案 0 :(得分:4)
SELECT *
FROM (
select job AS qjob
, id AS qid
,ROW_NUMBER() OVER (PARTITION BY JOB ORDER BY ID DESC) AS RN
from sqbclog
)Sub
WHERE rn <= 2
此查询将返回最后两条记录但如果您只需要第二条最后一条记录,那么在where子句中使用where en = 2,否则保持原样。
答案 1 :(得分:0)
这应该这样做!
select *
from sqbclog
where id not in (
select top (
(select count(*) from sqbclog ) - 2
) id
from sqbclog
)
答案 2 :(得分:0)
select job qjob, max(id) -1 qid
from sqbclog
group by job
答案 3 :(得分:0)
如果我已正确理解您的原始查询,并且实际上确实为您提供了最后一行,那么TOP 2应该为您提供最后两行:
SELECT TOP 2 job qjob,max(id)qid 来自sqbclog 按工作分组