orical SQL 取最新时间

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:54:31
就是要把表中所有“名称”的最后一次发生的最后一条数据取出来,最后发生的时间由日期来判断

名称 金额 日期
a 1.2 07-6-12
a 1.1 07-6-11
b 3.5 07-6-10
b 4.0 07-8-21
c 0.5 07-1-11
c 1.5 08-3-11
如何取到最新的价格和所在的日期 ,如
a 1.2 07-6-12
b 4.0 07-8-21
c 1.5 08-3.11

select a.* from 表 a,
(select 名称,max(日期) as 日期 from 表 group by 名称) b
where a.名称=b.名称
and a.日期=b.日期

select convert(nvarchar(10), getdate(),120)

select max(金额),max(日期) from 表 group by 名称

先建临时表

select [名称],max([日期]) [日期] into #A from [表名] group by [名称]

select a.* from [表名] a,#A b
where a.[名称]=b.[名称] and a.[日期]=b.[日期]