[英]How can I retrieve second last row?
I have a table with many records and I want to know only the record which I have created at second last. 我有一张包含许多记录的表格,我只想知道我最后创建的记录。
For ex: I have a table customer
in which customerID
are random numbers. 例如:我有一个表customer
,其中customerID
是随机数。
Now I want to select second last row. 现在我想选择倒数第二行。
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
Output row : 输出行:
customerID customer_name cont_no
5 revas 1
I don't want second highest... 我不想要第二高......
I want second last row. 我想要倒数第二排。
As you asked I can give you example. 如你所知,我可以举个例子。
Imagine, that you have full bag of apples. 想象一下,你有一整袋苹果。 How can you take second last apple? 你怎么能拿第二个苹果? How you will know which one is second last? 你怎么知道哪一个是倒数第二? You can't do It while you not sort them in any way. 当你不以任何方式对它们进行排序时,你不能这样做。
For now your data isn't sorted so you can't achieve It as expected. 目前,您的数据未排序,因此无法按预期实现。 You can do It in following, only after you have any sorting criteria like Id
, date created
or etc. 只有在您有任何排序条件(如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
Try this 试试这个
;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)
Here RowNum
is a column by numbering the rows in the table with out ordering it. 这里RowNum
是一个列,它通过对表中的行进行编号RowNum
进行排序。
max(RowNum) - 1
will give the second last max(RowNum) - 1
将给出倒数第二个
Posting as an answer as it is a big comment 发布作为答案,因为它是一个很大的评论
David: ok i will do it next time but what i can do now for this problem there are many recods in thousand.is there any way to do this?? 大卫:好的,我下次会这样做,但我现在可以做的就是这个问题有很多千禧年。有什么方法可以做到这一点? @Deepanshu Kalara @Deepanshu Kalara
Me: @david sam, I dont think there is a way to do this now. 我:@david山姆,我不认为现在有办法做到这一点。 Best bet would be copy those thousand records in excel and hope that they are in order you inserted them. 最好的选择是复制excel中的那千条记录,并希望它们是为了你插入它们。 Create a manual column there like you would have had if you had auto-increment. 在那里创建一个手动列,就像你有自动增量一样。 and correct your table structure by inserting that column in the table itself, as you said you would. 正如你所说的那样,通过在表格中插入该列来更正表格结构。
As you probably already know, you need a column to order by to achieve this task. 您可能已经知道,您需要一个列来命令才能完成此任务。 OVER Clause be used for this. 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
Datas should be sorted before they can be effectively search. 数据应该在有效搜索之前进行排序。
I would recommend to add an extra field in your table id with autoincrement
. 我建议使用autoincrement
在表id中添加一个额外的字段。
Its not a big deal as below : 它不是一个大问题如下:
Query : 查询:
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 : 第一个前2个数据以降序(DESC)方式选择,您可以根据id值得到结果:
8,7 (8 values are available in example shown) 8,7(示例中显示了8个值)
Next select the top 1 value in ASC (ascending manner) 接下来选择ASC中的前1个值(升序方式)
Output : 输出:
使用SQL Server 2012或更高版本,您可以在一个行代码中执行此操作:
LAG([MyValue],1) OVER (PARTITION BY [Category] ORDER BY [AnyColumnForOrdinal] ASC)
i know this is too late but you can try this. 我知道这太晚了但你可以尝试一下。
SELECT TOP 1 * FROM (SELECT * FROM dbo.customer
EXCEPT SELECT TOP (SELECT (COUNT(*)-2) FROM dbo.customer ) * FROM dbo.customer) A
select identity(int,1,1) as Id, * into #temp from customer 选择身份(int,1,1)作为Id,*从客户中选择#temp
select * from #temp where Id = (select max(Id) as count from #temp group by Id) - 1 drop table #temp select * from #temp其中Id =(选择max(Id)作为来自#temp group by Id的计数) - 1 drop table #temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.