sql服务器: Get OHLC in one query with n recodrs

标签 sql sql-server performance sql-server-2000

我需要从一个查询中获取 OHLC 值

SELECT [Open] -- first row value
  ,[High] -- max(High)
  ,[Low]  -- min(low)
  ,[Close]-- last row value
FROM [Forex] where Symbol ='EURUSD' and 
[timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 02:00:00.000'

我希望该查询有 12 小时的详细信息。我的意思是我需要获取 12 条记录以及每小时 OHLC 值

谁能帮忙解答一下吗? How to fetch the first and last record of a grouped record in a MySQL query with aggregate functions?在MYSQL中有实现,我需要在SQL Server中

编辑:表的结构

CREATE TABLE [dbo].[Forex](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[Symbol] [varchar](100) NOT NULL,
[TimeStamp] [datetime] NOT NULL,
[Bid] [decimal](18, 5) NOT NULL,
[Ask] [decimal](18, 5) NOT NULL,
[Open] [decimal](18, 5) NOT NULL,
[High] [decimal](18, 5) NOT NULL,
[Low] [decimal](18, 5) NOT NULL,
[NetChange] [decimal](18, 5) NOT NULL,
[PerChange] [decimal](18, 5) NOT NULL,
CONSTRAINT [PK_Forex] PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] 

和索引

CREATE NONCLUSTERED INDEX [index_Forex_29_85575343__K2_K1] ON [dbo].[Forex] 
(
[Symbol] ASC,
[Id] DESC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

最佳答案

你可以使用类似的东西

select [open],[min], [max], [close] 
from 
(
   select min(value) [min], max(value) [max] 
   from forex 
   where Symbol ='EURUSD' and 
   [timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 02:00:00.000'  
) minmax,

(
   select top 1 value as [open] 
   from forex 
   where Symbol ='EURUSD' and 
   [timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 02:00:00.000' 
   order by [timestamp] 
) fst,

(
  select top 1 value [close] 
  from forex 
  where Symbol ='EURUSD' and 
  [timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 02:00:00.000' 
  order by [timestamp] desc
) lst

参见http://sqlfiddle.com/#!3/0547c/2

编辑

根据修改后的问题... 我们可以获取按时间段(小时)分组的最小和最大 ID 以及该小时的最小和最大值:

select 
   datepart(hh,timestamp) [hour], 
   min(value) [min], 
   max(value) [max],
   min(id) min_id, 
   max(id) max_id 
 from forex 
 where Symbol ='EURUSD' and 
[timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 13:00:00.000'
 group by datepart(hh,timestamp)

并将其与外汇表连接(两次)以获得开盘价和收盘价。

给予类似的东西

   select minmax.[hour], f_min.value [open], [min], [max], f_max.value [close] 
    from 
    (select 
       datepart(hh,timestamp) [hour], 
       min(value) [min], 
       max(value) [max],
       min(id) min_id, 
       max(id) max_id 
     from forex 
     where Symbol ='EURUSD' and 
    [timestamp] between '2013-07-18 01:00:00.000' and '2013-07-18 13:00:00.000'
     group by datepart(hh,timestamp)
    ) minmax 
    join forex f_min on min_id = f_min.id
    join forex f_max on max_id = f_max.id 

sqlfiddle

关于sql服务器: Get OHLC in one query with n recodrs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740959/

相关文章:

performance - Elasticsearch 中的多索引搜索与单索引搜索

java - 从映射表中获取一个查询中的数据

sql - create-table 中的 Case 语句

sql - 在 sqlite 中使用 Case 语句,无法获得我正在寻找的结果

mysql - 多次查询同一个表但在不同的列中mysql

sql - 在编写 sql server 查询时如何编写类似于 Linq .Any() 的内容

java - 将数百万个 JSON 文档导入 MongoDB 的最快方法

sql-server - 如何使用SSMS将二进制数据插入sql server

sql - 如何在 SQL Server 中将 15000000.00 转换为 15,000,000?

python - 迭代计算列表中的元素并将计数存储在字典中