+ -
当前位置:首页 → 问答吧 → SQL分组查询,急!

SQL分组查询,急!

时间:2011-12-11

来源:互联网

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.00 459 NULL NULL NULL NULL NULL 2011-12-10 18:52:13.000 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 0.00 699.70 0.00 0.30 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 NULL NULL 0.00 700.01 0.00 0.00 0.00 0.00 0.00 NULL NULL NULL NULL NULL NULL NULL 13911111111 NULL NULL NULL NULL NULL 0.00 NULL 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0 0 0 0 0 0.00 0.00 0.00 0
这是查询出来的结果,只能查出紫仲这一个店的数据,我想查出History这个表中所有店的最新数据。也就是只查询出每个店最后一次上传的数据。一个店只显示一条。我写的SQL如何修改?

作者: xinshixiaoguan   发布时间: 2011-12-11

select max(ID) from History

==>

select max(ID) from History group by 店

作者: happyflystone   发布时间: 2011-12-11

好,谢谢1楼。己解决

作者: xinshixiaoguan   发布时间: 2011-12-11