我想要最新25行的SQL查询 [英] I want a sql query for latest 25 rows
本文介绍了我想要最新25行的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道从表中选择最近25行的查询. ... plzz帮助我做到这一点.
I want to know the query for selecting the latest 25 rows from a table. ...plzz help me to do it.
推荐答案
我认为您是根据日期确定最新的25个..
I assume that you are deciding latest 25 based on a date..
SELECT TOP 25* FROM TABLENAME order by DATECOLUMNNAME DESC
解决方案1也会为您提供正确的结果,但是在有很多记录的情况下会有点慢
这是一个例子
solution 1 will also give you the correct result but it will be a little slow in case of many records
here is an example
SELECT ORDERID, CUSTOMERID, OrderDate
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY OrderDate DESC) AS OrderedDate,*
FROM Orders
) as ordlist
WHERE ordlist.OrderedDate <= 25
首先,您应该在日期时间数据类型的表中有一列名为(Last_Updated或Latest_Visit).
该列应填充当前时间的日期时间,例如-Getdate()
不要转换getdate()的日期时间,因为您无法获取最新的日期时间.
查询如下:
First you should have a column in Tables named as (Last_Updated or Recent_Visit) in datetime datatype.
That column should fill with datetime of the current timing Eg - Getdate()
Dont convert the datetime of getdate(), because you cant get latest one.
Query Below:
Select top 25 * from MyDB..MyTable with (Nolock) order by Recent_Visit desc
这篇关于我想要最新25行的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文