我需要从一个查询中获取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小时的详细信息。我的意思是我需要获得每小时OHLC值的12条记录
任何人都可以在查询中提供帮助。 How to fetch the first and last record of a grouped record in a MySQL query with aggregate functions?在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]
答案 0 :(得分: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)
并将其与forex表(两次)连接以获得打开和关闭值。
给予像
这样的东西 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
答案 1 :(得分: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]