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

Group OHLC-Stockmarket Data into multiple timeframes with T-SQL(使用 T-SQL 将 OHLC-Stockmarket 数据分组到多个时间范围内)
本文介绍了使用 T-SQL 将 OHLC-Stockmarket 数据分组到多个时间范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using SQL Server 2008 R2 and need to create new tables grouped in Time intervals.

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

The data is data from a stock market index. I have the data in 1 minute intervals, now i need them in 5,10,15,30,45,60...minute intervals. My primary key is the time stamp.

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

My question is: how to query the 1 minute data table to return data that is grouped by a specific time interval for example 5 minute intervals.

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

The query must return the Highest, Lowest, Last and First values in that particular group and most importantly also the last entry of the time-stamp in the group.

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

I'm very new to the SQL language and have tried numerous code found on the net, but i cant get to exactly return the desired results.

数据:

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 |....|....|...|...

推荐答案

当您将 datetime 转换为 float 时,您会得到天数.如果你将它乘以 24 * 12,你会得到 5 分钟的间隔.所以如果你分组:

When you convert a datetime to a float, you get a number of days. If you multiply that by 24 * 12, you get a number of 5 minute intervals. So if you group on:

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

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

you can do aggregates per five minutes:

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 的一种方法:

Finding the first and the last row is tricky in SQL Server. Here's one way using 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)

这是一个SE Data 的工作示例.

这篇关于使用 T-SQL 将 OHLC-Stockmarket 数据分组到多个时间范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)