读书人

SQL分组查询该怎么解决

发布时间: 2012-03-24 14:00:46 作者: rapoo

SQL分组查询,急!
select rank () over (order by testtime desc) xh,Stationinfo.StationState,Stationinfo.StationName,Report.Dpress,History.* from History left join Stationinfo on History.StationName=Stationinfo.Agreement left join Report on Report.StationName=History.StationName
where Testtime< convert(varchar(10),getdate(),120) and History.ID in(select max(ID) from History) and Report.ID in(select max(ID) FROM Report)
order by Testtime desc

紫仲0.00459NULLNULLNULLNULLNULL2011-12-10 18:52:13.000NULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULL0.00699.700.000.300.010.000.000.000.000.000.000.000.00NULLNULL0.00700.010.000.000.000.000.00NULLNULLNULLNULLNULLNULLNULL13911111111NULLNULLNULLNULLNULL0.00NULL0.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.00000000.000.000.000
这是查询出来的结果,只能查出紫仲这一个店的数据,我想查出History这个表中所有店的最新数据。也就是只查询出每个店最后一次上传的数据。一个店只显示一条。我写的SQL如何修改?

[解决办法]
select max(ID) from History

==>

select max(ID) from History group by 店

读书人网 >SQL Server

热点推荐