现在系统中有这样一个功能。 系统会每隔3分钟获取系统的一些数据并存入数据据,数据库格式很简单: id time(dateTime类型) data 就这么三列.
现在给定一个月,要获取这个月内每天的最后一条数据。各位有没有比较好的SQL解决办法? 初始版本中是查出这个月的所有数据,然后在程序中处理的。 效率不怎么高, 想问下各位能不能在SQL上做点文章? 如果不行的话,只能去优化程序了。~
现在给定一个月,要获取这个月内每天的最后一条数据。各位有没有比较好的SQL解决办法? 初始版本中是查出这个月的所有数据,然后在程序中处理的。 效率不怎么高, 想问下各位能不能在SQL上做点文章? 如果不行的话,只能去优化程序了。~
where datediff(month, '2011-11-1',[time])=0
and
not exists(select 1 from @t where datediff(day,[time],A.[time])=0 and [time]>A.[time])
用了很多函数~ 悲剧的是我用的是MongoDB存储数据,这些函数估计不存在
;with cte_play
as
(
select tempcol= row_number() over (partition by convert(char(10),[time],120) order by [time] desc) , * from @t
where datediff(month,'2011-1-1',[time])=0
)
select id,[time],data from cte_play
where tempcol=1
and time >t.time) ???
MongoDB 的不懂
MongoDB 的不懂
Mongo DB ??? 不懂...
distinct b.*
from
tb a
cross apply
(select top 1 * from tb where convert(varchar(10),[time],120)=convert(varchar(10),t.[time],120) order by time desc)b