使用T-SQL将OHLC-Stockmarket数据分组到多个时间范围内

时间:2012-02-18 11:04:35

标签: sql-server tsql group-by timestamp finance

我正在使用SQL Server 2008 R2,需要创建按时间间隔分组的新表。

数据来自股票市场指数。我有1分钟间隔的数据,现在我需要它们在5,10,15,30,45,60 ......分钟间隔。我的主要关键是时间戳。

我的问题是:如何查询1分钟数据表以返回按特定时间间隔分组的数据,例如5分钟的间隔。

查询必须返回该特定组中的最高,最低,最后和第一个值,最重要的是还要返回组中时间戳的最后一个条目。

我对SQL语言非常陌生并尝试过在网上找到的大量代码,但我无法准确地返回所需的结果。

数据:

TimeStamp          | Open | High | Low | Close
2012-02-17 15:15:0 | 102  | 110  |100  |105
2012-02-17 15:16:0 |106   |112   |105  |107
2012-02-17 15:17:0 | 106  |110   |98   |105
2012-02-17 15:18:0 |105   |109   |104  |106
2012-02-17 15:19:0 |107   |112   |107  |112
2012-02-17 15:20:0 |115   |125   |115  |124

所需的查询结果(5分钟):

Timestamp       |Open|High|Low|Close
2012-02-15:19:0 |102 |125 |98 |124
2012-02-15:24:0 |115.|....|...|...
2012-02-15:29:0 |....|....|...|...

1 个答案:

答案 0 :(得分:3)

datetime转换为float时,您会获得若干天。如果将其乘以24 * 12,则会得到5分钟的间隔。所以,如果你分组:

cast(cast(timestamp as float) * 24 * 12 as int)

你可以每五分钟做一次聚合:

select  min(timestamp)
,       max(high) as Highest
,       min(low) as Lowest
from    @t
group by
        cast(cast(timestamp as float) * 24 * 12 as int)

在SQL Server中查找第一行和最后一行很棘手。这是使用row_number的一种方式:

select  min(timestamp)
,       max(high) as Highest
,       min(low) as Lowest
,       min(case when rn_asc = 1 then [open] end) as first
,       min(case when rn_desc = 1 then [close] end) as Last
from    (
        select  row_number() over (
                    partition by cast(cast(timestamp as float) * 24 * 12 as int)
                    order by timestamp) as rn_asc
        ,       row_number() over (
                    partition by cast(cast(timestamp as float) * 24 * 12 as int)
                    order by timestamp desc) as rn_desc
        ,       *
        from    @t
        ) as SubQueryAlias
group by
        cast(cast(timestamp as float) * 24 * 12 as int)

这是working example at SE Data.