日期:2014-05-17 浏览次数:20533 次
select [time]
from tab
where [time] between timeStart and timeEnd
and datediff([间隔时间的单位(年/月/日/时/分/秒)],timeStart,[time])%timeSpan=0
CREATE TABLE #temp([time] DATETIME, field1 INT, field2 FLOAT)
INSERT #temp
select '2013-08-01 09:44:30','1','1.0'union all
select '2013-08-01 09:45:24','1','1.0'union all
select '2013-08-01 09:45:28','1','1.0'union all
select '2013-08-01 09:45:31','1','1.0'union all
select '2013-08-01 09:45:33','1','1.0'union all
select '2013-08-01 09:45:36','1','1.0'
DECLARE @timeStart DATETIME, @timeEnd DATETIME
SELECT @timeStart = '2013-08-01 09:45:00', @timeEnd = '2013-08-01 10:35:00'
SELECT A.timePoint, B.* FROM
(
SELECT
timePoint = DATEADD(minute, 10*number, @timeStart)
FROM master..spt_values b
WHERE type = 'p'
AND DATEADD(minute, 10*number, @timeStart) <= @timeEnd
) A
CROSS APPLY
(
SELECT TOP(1) *
FROM #temp M
WHERE M.[time] < A.timePoint
ORDER BY M.[time] DESC
) B
/*
timePoint time field1 field2
2013-08-01 09:45:00.000 2013-08-01 09:44:30.000 1 1
2013-08-01 09:55:00.000 2013-08-01 09:45:36.000 1 1
2013-08-01 10:05:00.000 2013-08-01 09:45:36.000 1 1
2013-08-01 10:15:00.000 2013-08-01 09:45:36.000 1 1