我可以給你在T-SQL的答案,但我不知道你使用的是什麼話。 TBH,這裏的循環是彈簧想到的第一件事情(其他人可能有這樣做的更好的辦法!):
DECLARE @temp TABLE (log_date DATETIME, value FLOAT)
INSERT INTO @temp (log_date, value) SELECT log_date, value FROM <MyTableName>
DECLARE @diff TABLE (time_diff INT, old_value FLOAT, new_value FLOAT)
-- the loop
DECLARE @prev_value FLOAT,
@cur_value FLOAT,
@prev_log_date DATETIME,
@cur_log_date DATETIME
WHILE EXISTS (SELECT NULL FROM @temp)
BEGIN
SELECT TOP 1 @cur_log_date = log_date, @cur_value = value
FROM @temp
ORDER BY log_date
IF (@prev_value IS NOT NULL AND @prev_log_date IS NOT NULL)
BEGIN
INSERT INTO @diff (time_diff, old_value, new_value)
SELECT DATEDIFF('ms', @prev_log_date, @cur_log_date),
@prev_value, @cur_value
END
SELECT @prev_log_date = @cur_log_date, @prev_value = @cur_value
DELETE FROM @temp WHERE log_date = @cur_log_date
END
SELECT MAX(time_diff), MIN(time_diff) FROM @diffs
這樣,你最終帶來的不同的表,然後就可以查詢。
HTH
你可以發佈樣本數據和預期的輸出樣本?我不確定我完全理解這個問題。 – 2010-11-11 09:24:45