SQL以不同的间隔选择行

时间:2011-10-10 13:32:05

标签: sql sql-server

我有一个情况,我有一个巨大的表,包含大量的行,看起来像(例如):

id          Timestamp               Value
14574499    2011-09-28 08:33:32.020 99713.3000
14574521    2011-09-28 08:33:42.203 99713.3000
14574540    2011-09-28 08:33:47.017 99713.3000
14574559    2011-09-28 08:38:53.177 99720.3100
14574578    2011-09-28 08:38:58.713 99720.3100
14574597    2011-09-28 08:39:03.590 99720.3100
14574616    2011-09-28 08:39:08.950 99720.3100
14574635    2011-09-28 08:39:13.793 99720.3100
14574654    2011-09-28 08:39:19.063 99720.3100
14574673    2011-09-28 08:39:23.780 99720.3100
14574692    2011-09-28 08:39:29.167 99758.6400
14574711    2011-09-28 08:39:33.967 99758.6400
14574730    2011-09-28 08:39:40.803 99758.6400
14574749    2011-09-28 08:39:49.297 99758.6400

好的,所以规则是: 时间戳可以是任意n秒,5秒,30秒,60秒等,它取决于记录的年龄(存档发生)。

我希望能够查询此表,根据时间戳选择每一行。

例如:

从mytable中选择*,其中intervalBetweenTheRows = 30s

(出于这个问题的目的,根据假设,所请求的间隔总是比数据库中的可用精度更高)

所以,每隔第n行基于每行之间的时间

有什么想法吗?!

卡尔

对于那些感兴趣的人来说,递归CTE实际上非常慢,我想到了一种稍微不同的方法:

SELECT TOP 500
    MIN(pvh.[TimeStamp]) as [TimeStamp],
    AVG(pvh.[Value]) as [Value]
FROM
    PortfolioValueHistory pvh
WHERE
    pvh.PortfolioID = @PortfolioID
    AND pvh.[TimeStamp] >= @StartDate
    AND pvh.[TimeStamp] <= @EndDate
GROUP BY
    FLOOR(DateDiff(Second, '01/01/2011 00:00:00', pvh.[TimeStamp]) / @ResolutionInSeconds)
ORDER BY 
    [TimeStamp] ASC

我将时间戳减去任意日期以给出一个基本的int来处理,然后将其除以我想要的分辨率,然后按此分组,取最小时间戳(邮票的第一个“区域”) )和'期间'的平均值。

这用于绘制历史数据的图表,因此平均值对我很好。

这是基于我可以提出的表格大小的最快执行次数

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

假设要求是否返回行的决定因素取决于从前一个返回行所经过的时间,这需要一个程序方法。递归CTE可能比光标更有效。

WITH RecursiveCTE
     AS (SELECT TOP 1 *
         FROM @T
         ORDER BY [Timestamp]
         UNION ALL
         SELECT id,
                [Timestamp],
                Value
         FROM   (
                --Can't use TOP directly
                SELECT T.*,
                       rn = ROW_NUMBER() OVER (ORDER BY T.[Timestamp])
                 FROM   @T T
                        JOIN RecursiveCTE R
                          ON T.[Timestamp] >=
                                 DATEADD(SECOND, 30, R.[Timestamp])) R
         WHERE  R.rn = 1)
SELECT *
FROM RecursiveCTE

答案 1 :(得分:1)

这不像Martin S的CTE那么优雅,而是在预定义的采样点上使用插值来获取每对采样时间之间的第一个样本。 如果一段时间内没有样本,则不返回任何记录。

DECLARE @SampleTime DATETIME
DECLARE @NumberSamples INT
DECLARE @SampleInterval INT

SET @SampleTime = '2011-09-28 08:33:32.020' -- Start time
SET @NumberSamples = 20 -- Or however many sample intervals you need to evaluate
SET @SampleInterval = 30 -- Seconds

CREATE TABLE #tmpTimesToSample
(
    SampleID INT,
    SampleTime DATETIME NULL
)

-- Works out the time intervals, 0 to 19
INSERT INTO #tmpTimesToSample(SampleID, SampleTime)
SELECT TOP (@NumberSamples)
    sv.number,
    DATEADD(ss, sv.number * @SampleInterval, @SampleTime)
FROM
    master..spt_values sv
WHERE 
    type = 'p'
ORDER BY
    sv.number ASC

-- Now interpolate these sample intervals back into the data table
SELECT ID, [TimeStamp], Value
FROM
(
    SELECT mt.Id, mt.[TimeStamp], mt.Value, row_number() over (partition by tmp.SampleID order by tmp.SampleID) as RowNum
    FROM #tmpTimesToSample tmp RIGHT OUTER JOIN MyTable mt
    on mt.[TimeStamp] BETWEEN tmp.SampleTime and DATEADD(ss, @SampleInterval, tmp.SampleTime)
) x
WHERE x.RowNum = 1 -- Only want the first sample in each bin

DROP TABLE #tmpTimesToSample

测试数据:

CREATE TABLE MyTable
(
    ID BIGINT,
    [TimeStamp] DATETIME,
    [Value] DECIMAL(18,4)
)
GO

insert into MyTable values(14574499, '2011-09-28 08:33:32.020', 99713.3000)
insert into MyTable values(14574521    ,'2011-09-28 08:33:42.203',  99713.3000)
insert into MyTable values(14574540    ,'2011-09-28 08:33:47.017', 99713.3000)
insert into MyTable values(14574559    ,'2011-09-28 08:38:53.177', 99720.3100)
insert into MyTable values(14574578    ,'2011-09-28 08:38:58.713', 99720.3100)
insert into MyTable values(14574597    ,'2011-09-28 08:39:03.590', 99720.3100)
insert into MyTable values(14574616    ,'2011-09-28 08:39:08.950', 99720.3100)
insert into MyTable values(14574635    ,'2011-09-28 08:39:13.793', 99720.3100)
insert into MyTable values(14574654    ,'2011-09-28 08:39:19.063', 99720.3100)
insert into MyTable values(14574673    ,'2011-09-28 08:39:23.780', 99720.3100)
insert into MyTable values(14574692    ,'2011-09-28 08:39:29.167', 99758.6400)
insert into MyTable values(14574711    ,'2011-09-28 08:39:33.967', 99758.6400)
insert into MyTable values(14574730    ,'2011-09-28 08:39:40.803', 99758.6400)
insert into MyTable values(14574749    ,'2011-09-28 08:39:49.297', 99758.6400)
go

答案 2 :(得分:0)

这将为您提供到下一行的间隔为30毫秒的所有行。两行都是并排的。

Select T1.*, T2.*
From MyTable T1
    Inner Join MyTable T2
        On DateDiff (millisecond, T1.Value, T2.Value) = 30