原数据为每10秒写入一条新数据
查询历史记录时要求每间隔10分钟取一个样点,查过去10小时的数据
SQL语句:
declare @endDate datetime
declare @startDate datetime
set @endDate=(select max([monitorTime]) from table_x where columnA='条件')
set @startDate=DATEADD(minute,-600,@endDate)
select
[uuid]
,[columnA]
, convert(varchar(16),monitorTime,120) as monitorTime
from table_x a where columnA='条件' and monitorTime>=@startDate and datediff(MINUTE,monitorTime,@endDate)%10=0
and not exists
(
select [uuid] from table_x b where columnA='条件' and monitorTime>=@startDate and datediff(MINUTE,monitorTime,@endDate)%10=0
and convert(varchar(16),a.monitorTime,120) = convert(varchar(16),b.monitorTime,120)
and a.monitorTime < b.monitorTime
)
order by monitorTime desc