SQL 获取最新更新的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13226450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:56:03  来源:igfitidea点击:

Get latest updated records

sqlsql-server-2008

提问by alextc

I have an "Observation" table in SQL Server 2008. This table has a locationId column for a bunch of geographic locations, a few columns for observation details and a column for latest updated date.

我在 SQL Server 2008 中有一个“观察”表。该表有一个 locationId 列用于一组地理位置,几列用于观察详细信息,还有一个用于最新更新日期的列。

Every week, a new observation record for each location is appended. So a location has many occurrences in the table.

每周,都会附加每个位置的新观察记录。所以一个位置在表中出现了很多次。

What I want to achieve is to be able to get the most recent observation record for each location.

我想要实现的是能够获得每个位置的最新观察记录。

Can anyone help with any idea?

任何人都可以帮助任何想法吗?

回答by SRIRAM

 select * from observation where date=(select max(date) from observation)

or

或者

select top 1 * from observation order by date desc

回答by AnandPhadke

select a.* from observations a inner join 
(select locationid ,max(updateddate) dates  from observations
group by locationid) b
on a.locationid=b.locationid
and a.updateddate=b.dates  

回答by Bajrang

Run query

运行查询

select * from Observation 
group by location 
order by viewdate desc

Please also give the full details about table and what you want to get.

还请提供有关表格的完整详细信息以及您想要获得的内容。

EDIT :Backtick removed.

编辑:删除反引号。

回答by Lier

Add a column to your table with a datatype of [timestamp] execute the following code:

将数据类型为 [timestamp] 的列添加到表中,执行以下代码:

select top(10) * from yourtablename order by columanname desc

Note:columanname should be the column you add with a timestamp type

注意:columanname 应该是您添加的带有时间戳类型的列

回答by swapnil

Use the Getdate function as I used as below.

使用我使用的 Getdate 函数,如下所示。

select * from TBL_MP_QC_CustomerWiseCOA_Master  order by getdate() desc