2

我需要从一个查询中获取 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 值的记录

任何人都可以帮助查询。 如何使用聚合函数在 MySQL 查询中获取分组记录的第一条和最后一条记录?在 MYSQL 中有实现,我需要在 SQL 服务器中

编辑:表的结构

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]
4

2 回答 2

2

你可以使用类似的东西

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

于 2013-07-19T08:34:43.483 回答
0

这对我有帮助

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]
于 2013-07-19T09:24:39.640 回答