SQL Server 2005 中的数据聚合
我需要查询 SQl Server 2005(SQL Server Management Studio Express)。 我将数据存储为 1 分钟时间范围(每行 1 分钟),每个表的列分别是 ID、交易品种、日期时间、开盘价、最高价、最低价、收盘价、交易量。 我需要转换(压缩)到每个可能的多个时间范围,例如 10 分钟、13 分钟、15 分钟等。 如果有人可以提供帮助,请提供完整的详细信息。 谢谢 阿尔贝托
I need a query for SQl server 2005 (SQL server management studio express).
I have data stored as 1 minute time frame (1 minute each row), for each table columns are ID, Symbol, DateTime, Open, High, Low, Close, Volume.
I need to convert (compress) to every possibile multiple time frame, so let's say 10 minutes, 13, 15, and so on.
Provide full details if somebody could help.
Thanks
Alberto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Alberto,看来您需要在 SQL 语句中使用“Group By”子句(如 Leppie 所说)。所以,你最好还是寻找一下。
首先,您应该使用开始和结束日期/时间过滤要聚合的行,然后按提到的子句对它们进行分组。
这是我通过 Google 搜索“sql group by”关键字时的第一个链接。
Alberto, it looks like you need a "Group By" clause in SQL statements (as Leppie stated). So, you should better look for it.
First you should filter the rows that is subject for aggregation by using begin and end date/time and then group them by the mentioned clause.
Here is the first link when i search "sql group by" keywords via Google.
或者另一种可能值得测试的方法,看看它是否更快。
Or another approach that may be worth testing to see if it is any faster.
不是简单的“分组依据” - 需要为组中的第一行和相应的最后一行获取打开和关闭值。或者至少对于外汇数据来说是这样:)
Not simple "Group By" - Open and Close values need taken for first and correspondingly last row in group. Or at least so is it for Forex data :)
使用存储过程首先提取 MIN(datetime) 会更漂亮,但这里有一个草图:
WITH 子句在数据集中获取 15 分钟间隔的列表,向下舍入(让我们假设 2000 年之前没有任何内容)。
然后使用这些间隔按 14:59 间隔进行分组。
对于数量,您必须决定是要平均值还是总数。
语法可能有点偏离,但你应该明白了。
编辑:调整 MIN(开盘)、MIN(收盘)以获取第一个和最后一个。实际上,这不会有太大变化,因为开盘和收盘的概念取决于了解报价来源的交易所与收集数据的计算机时钟之间的时间差。
此外,除非OP拥有从所有交易所实时获取的特权,否则所有报价无论如何都会延迟20分钟。
编辑(2):非常正确,FIRST 和 LAST 是我的 IBM 日子的遗留物 >;-)
解决方案现在使用 TOP 和 ASC/DESC 选择时间间隔内的第一个和最后一个报价。
Would be prettier with a stored proc to extract MIN(datetime) first, but here's a sketch:
The WITH clause gets a list of 15 minute intervals, rounded down, in your dataset (let's assume nothing before 2000).
Then use those intervals to group by 14:59 interval.
For the volume, you'll have to decide if you want average or the total.
The syntax might be a tad off, but you should get the idea.
EDIT: Adjusted MIN(open), MIN(close) to pick up FIRST and LAST. In reality this won't change much, as the concept of Open and Close depend on knowing the time difference between the exchange where the quote originated and the clock of the computer collecting the data.
In addition, unless the OP has the privilege of a real-time feed from all the exchanges, all the quotes are delayed by 20 minutes anyway.
EDIT(2): Quite right, FIRST and LAST are carry-overs from my IBM days >;-)
Solution now selects first and last quotes during the interval using TOP with ASC/DESC.