如何取每组最新的数据
现在我有一个数据表,字段大致为StoreNum(库号)、Time(时间)和T(温度)这几个字段,每个库都有多条数据,现在如何取出每个库中最新的一条记录呢?
------解决方案--------------------select * from 表 a
where Time=(select max(Time) from 表 where StoreNum=a.StoreNum)
------解决方案--------------------select * from tb a
where exists (select 1 from (select StoreNum,max(Time)time,T from tb group by StoreNum,T)b where a.StoreNum=b.StoreNum and a.t=b.t)
------解决方案--------------------
if OBJECT_ID('a')is not null
drop table a
create table a(name nvarchar(20),[date] date )
insert a
select '张三','1990-10-15' union all
select '张三','1997-10-15' union all
select '张三','1996-10-15' union all
select '李四','1990-10-15' union all
select '王五','1990-10-15' union all
select '李四','1991-10-15'
go
select * from a
select * from a aa where not Exists(select * from a where name=aa.name and [date]>aa.[date])