如何检索倒数第二行?
How can I retrieve second last row?
我有一个 table 有很多记录,我只想知道我在倒数第二个创建的记录。
例如:我有一个 table customer
,其中 customerID
是随机数。
现在我想select倒数第二行。
customerID customer_name cont_no
---------------------------------------
7 david sam 5284
1 shinthol 1
11 lava 12548
2 thomas 1
3 peeter 1
4 magge 1
5 revas 1
6 leela 123975
输出行:
customerID customer_name cont_no
5 revas 1
我不要第二高...
我要倒数第二行。
如你所问,我可以举个例子。
想象一下,你有一整袋苹果。你怎么能拿倒数第二个苹果?你怎么知道哪一个是倒数第二个?如果您不以任何方式对它们进行排序,您将无法做到这一点。
目前您的数据未排序,因此您无法按预期实现它。您可以按照以下方式进行操作,只有在您具有 Id
、date created
等任何排序条件之后。
SELECT TOP 1 *
FROM(
SELECT TOP 2 *
FROM Tbl
ORDER BY SortingCol DESC -- here you need to pass column which will provide expected sorting
) t
ORDER BY SortingCol
试试这个
;WITH tbl_rn AS (
select
RowNum = row_number() OVER (ORDER BY @@rowcount),
customerID,
customer_name,
cont_no
from tbl
)
select
customerID,
customer_name,
cont_no
from tbl_rn
where RowNum = (select max(RowNum) - 1 from tbl_rn)
此处 RowNum
是一列,通过对 table 中的行进行编号而不对其进行排序。
max(RowNum) - 1
会给出倒数第二个
作为答案发布,因为它是一个很大的评论
大卫:好吧,我下次会做,但我现在可以为这个问题做些什么 thousand.is 中有很多记录,有什么办法可以做到这一点? @Deepanshu Kalara
我:@david sam,我认为现在没有办法做到这一点。
最好的办法是复制 excel 中的那几千条记录,并希望它们按照您插入的顺序排列。在那里创建一个手动列,就像你有自动增量一样。并通过在 table 本身中插入该列来更正您的 table 结构,正如您所说的那样。
Datas should be sorted before they can be effectively search.
我建议在您的 table id 中添加一个额外字段 autoincrement
。
没什么大不了的,如下:
查询:
SELECT TOP (1) customerID, customer_name, cont_no, id
FROM (SELECT TOP (2) customerID, customer_name, cont_no, id
FROM customer
ORDER BY id DESC) AS t
ORDER BY id
First top 2 Data is selected in a descending (DESC) manner where you get
results based on id value as :
8,7 (8 values are available in example shown)
Next select the top 1 value in ASC (ascending manner)
输出:
您可能已经知道,您需要一列作为排序依据才能完成此任务。为此使用 OVER 子句。
;WITH CTE as
(
SELECT
customerid, customer_name, cont_no,
row_number() over (order by newlymadesortcolumn desc) rn
FROM customer
)
SELECT customerid, customer_name, cont_no
FROM CTE
WHERE rn = 2
select identity(int,1,1) as Id, * into #temp from customer
select * 来自 #temp 其中 Id = (select max(Id) as count from #temp group by Id) - 1
下降 table #temp
使用 SQL Server 2012 或更高版本,您可以在一行代码中完成:
LAG([MyValue],1) OVER (PARTITION BY [Category] ORDER BY [AnyColumnForOrdinal] ASC)
我知道这太晚了,但你可以试试这个。
SELECT TOP 1 * FROM (SELECT * FROM dbo.customer
EXCEPT SELECT TOP (SELECT (COUNT(*)-2) FROM dbo.customer ) * FROM dbo.customer) A
我有一个 table 有很多记录,我只想知道我在倒数第二个创建的记录。
例如:我有一个 table customer
,其中 customerID
是随机数。
现在我想select倒数第二行。
customerID customer_name cont_no
---------------------------------------
7 david sam 5284
1 shinthol 1
11 lava 12548
2 thomas 1
3 peeter 1
4 magge 1
5 revas 1
6 leela 123975
输出行:
customerID customer_name cont_no
5 revas 1
我不要第二高...
我要倒数第二行。
如你所问,我可以举个例子。
想象一下,你有一整袋苹果。你怎么能拿倒数第二个苹果?你怎么知道哪一个是倒数第二个?如果您不以任何方式对它们进行排序,您将无法做到这一点。
目前您的数据未排序,因此您无法按预期实现它。您可以按照以下方式进行操作,只有在您具有 Id
、date created
等任何排序条件之后。
SELECT TOP 1 *
FROM(
SELECT TOP 2 *
FROM Tbl
ORDER BY SortingCol DESC -- here you need to pass column which will provide expected sorting
) t
ORDER BY SortingCol
试试这个
;WITH tbl_rn AS (
select
RowNum = row_number() OVER (ORDER BY @@rowcount),
customerID,
customer_name,
cont_no
from tbl
)
select
customerID,
customer_name,
cont_no
from tbl_rn
where RowNum = (select max(RowNum) - 1 from tbl_rn)
此处 RowNum
是一列,通过对 table 中的行进行编号而不对其进行排序。
max(RowNum) - 1
会给出倒数第二个
作为答案发布,因为它是一个很大的评论
大卫:好吧,我下次会做,但我现在可以为这个问题做些什么 thousand.is 中有很多记录,有什么办法可以做到这一点? @Deepanshu Kalara
我:@david sam,我认为现在没有办法做到这一点。 最好的办法是复制 excel 中的那几千条记录,并希望它们按照您插入的顺序排列。在那里创建一个手动列,就像你有自动增量一样。并通过在 table 本身中插入该列来更正您的 table 结构,正如您所说的那样。
Datas should be sorted before they can be effectively search.
我建议在您的 table id 中添加一个额外字段 autoincrement
。
没什么大不了的,如下:
查询:
SELECT TOP (1) customerID, customer_name, cont_no, id
FROM (SELECT TOP (2) customerID, customer_name, cont_no, id
FROM customer
ORDER BY id DESC) AS t
ORDER BY id
First top 2 Data is selected in a descending (DESC) manner where you get results based on id value as :
8,7 (8 values are available in example shown)
Next select the top 1 value in ASC (ascending manner)
输出:
您可能已经知道,您需要一列作为排序依据才能完成此任务。为此使用 OVER 子句。
;WITH CTE as
(
SELECT
customerid, customer_name, cont_no,
row_number() over (order by newlymadesortcolumn desc) rn
FROM customer
)
SELECT customerid, customer_name, cont_no
FROM CTE
WHERE rn = 2
select identity(int,1,1) as Id, * into #temp from customer
select * 来自 #temp 其中 Id = (select max(Id) as count from #temp group by Id) - 1
下降 table #temp
使用 SQL Server 2012 或更高版本,您可以在一行代码中完成:
LAG([MyValue],1) OVER (PARTITION BY [Category] ORDER BY [AnyColumnForOrdinal] ASC)
我知道这太晚了,但你可以试试这个。
SELECT TOP 1 * FROM (SELECT * FROM dbo.customer
EXCEPT SELECT TOP (SELECT (COUNT(*)-2) FROM dbo.customer ) * FROM dbo.customer) A