请教一个关于按日期分组查询的语句
表结构如下
id myvalue datatime
大概每10分钟多一条记录
现在要查询最近二十小时内 每个小时 最大的myvalue值,并且按照时间倒叙显示
[解决办法]
select convert(varchar(13),datatime,120) as datatime,
max(myvalue) as myvalue
from tbname
where datediff(hh,datatime,getdate()) <= 20
group by convert(varchar(13),datatime,120)
--试试这个行不行?
select left(format(日期, 'yyyy-mm-dd hh'),13) as datatime,
max(myvalue) as myvalue
from tbname
where datediff('h',datatime,date()) <= 20
group by left(format(日期, 'yyyy-mm-dd hh'),13)
order by left(format(日期, 'yyyy-mm-dd hh'),13) desc