【问题标题】:Sql Server: Get OHLC in one query with n recodrsSql Server:在具有 n 条记录的一次查询中获取 OHLC
【发布时间】:2013-07-18 10:28:19
【问题描述】:

我需要从一个查询中获取 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]

【问题讨论】:

  • 什么版本的 SQL Server?
  • 是时候寻找升级了。 2000 甚至在今年 4 月退出了扩展支持。从那时起已经发布了 4 个版本,目前正在预览第五个版本。
  • 我无法升级,因为它是客户端机器

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


【解决方案1】:

你可以使用类似的东西

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

【讨论】:

【解决方案2】:

这对我有帮助

select minmax.[hour], f_min.[Open] [open], [min], [max], f_max.Ask [close] 
from 
(select 
   datepart(hh,timestamp) [hour], 
   min(High) [min], 
   max(Low) [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 15: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 order by [hour]

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多