选择表的第二行 [英] to select the second row of a table
问题描述
亲爱的所有人...
如何使用SQL选择表列的倒数第二行
提前thanx
dear all...
how can i select the second last row of the column of a table using a SQL
thanx in advance
推荐答案
请检查以下代码.
Hi,
Please check the below code.
SELECT * FROM (SELECT RowNumber = ROW_NUMBER() OVER (ORDER BY c.TableColumn_PK ASC),c.*
FROM TableName c) AS CD WHERE RowNumber = ((SELECT COUNT(*) FROM TableName)-1)
只需将"TableColumn_PK"替换为主键,将"TableName"替换为表名即可.
我测试了会很好用的.
干杯....
更新:为此,也有很多解决方案.尝试找出其他人.
更新:
当您通过声明您正在使用sql server并且想要日期差来发布答案时.因此,请使用下面给出的代码.
Just replace "TableColumn_PK" with your primary key and "TableName" with your table name.
I tested it. It will work fine.
Cheers....
Update: There are a lot of solutions too for this. Try to find out others.
Update:
As you posted an answer by stating that you are using sql server and you want the date difference. So, please use the code given below.
DECLARE @VAR TABLE (KeyID BIGINT IDENTITY(1,1),RANK BIGINT,LastTwoDates DATETIME)
INSERT INTO @VAR
SELECT TOP(2) RANK() OVER (ORDER BY dLastLogindate) AS RANK, dLastLogindate
FROM tbUS_Recruiter
ORDER BY RANK DESC
IF ((SELECT COUNT(*) FROM @VAR) = 2)
BEGIN
SELECT DATEDIFF(DAY,(SELECT LastTwoDates FROM @VAR WHERE KeyID = 2),(SELECT LastTwoDates FROM @VAR WHERE KeyID = 1)) AS DayCount,
DATEDIFF(HOUR,(SELECT LastTwoDates FROM @VAR WHERE KeyID = 2),(SELECT LastTwoDates FROM @VAR WHERE KeyID = 1)) AS HourCount,
DATEDIFF(MINUTE,(SELECT LastTwoDates FROM @VAR WHERE KeyID = 2),(SELECT LastTwoDates FROM @VAR WHERE KeyID = 1)) AS MinutesCount,
DATEDIFF(SECOND,(SELECT LastTwoDates FROM @VAR WHERE KeyID = 2),(SELECT LastTwoDates FROM @VAR WHERE KeyID = 1)) AS SecondCount
END
只需将"dLastLogindate"字段更改为日期字段,然后将"tbUS_Recruiter"更改为表名.会很好用的.
代码已检查.
干杯...
Just change "dLastLogindate" field into your date field and "tbUS_Recruiter" as your table name. It will work fine.
Code checked.
Cheers...
这篇关于选择表的第二行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!