我有几年的外汇历史数据,但它是按分钟列出的。我想知道如何将其转换为M2,M5和M15。我尝试了很多东西和方法,但都没有用。
欢迎使用Excel函数或SQL脚本。
下面列出了一些数据格式样本。
Date Time Open High Low Close
2016.01.03 17:00 1.08701 1.08713 1.08701 1.08713
2016.01.03 17:01 1.08712 1.08712 1.08712 1.08712
2016.01.03 17:02 1.08708 1.08722 1.08708 1.08722
2016.01.03 17:03 1.08717 1.08723 1.08717 1.08723
2016.01.03 17:04 1.08718 1.08718 1.08711 1.08711
2016.01.03 17:05 1.08703 1.08716 1.08701 1.08712
2016.01.03 17:06 1.08721 1.08721 1.0871 1.0871
2016.01.03 17:07 1.08712 1.08715 1.08712 1.08712
2016.01.03 17:08 1.08711 1.0872 1.08711 1.08713
2016.01.03 17:09 1.08716 1.08723 1.08708 1.08708
答案 0 :(得分:1)
以下是M2的示例。应该很容易看到如何改变M5和M15
declare @forexDate table
(
recDate datetime,
openVal float,
highVal float,
lowVal float,
closeVal float
)
INSERT INTO @forexDate VALUES('2016.01.03 17:00', 1.08701, 1.08713, 1.08701, 1.08713)
INSERT INTO @forexDate VALUES('2016.01.03 17:01', 1.08712, 1.08712, 1.08712, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:02', 1.08708, 1.08722, 1.08708, 1.08722)
INSERT INTO @forexDate VALUES('2016.01.03 17:03', 1.08717, 1.08723, 1.08717, 1.08723)
INSERT INTO @forexDate VALUES('2016.01.03 17:04', 1.08718, 1.08718, 1.08711, 1.08711)
INSERT INTO @forexDate VALUES('2016.01.03 17:05', 1.08703, 1.08716, 1.08701, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:06', 1.08721, 1.08721, 1.0871, 1.0871)
INSERT INTO @forexDate VALUES('2016.01.03 17:07', 1.08712, 1.08715, 1.08712, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:08', 1.08711, 1.0872, 1.08711, 1.08713)
INSERT INTO @forexDate VALUES('2016.01.03 17:09', 1.08716, 1.08723, 1.08708, 1.08708)
SELECT o.recDate, o.openVal, c.closeVal, mx.highVal, mn.lowVal FROM
(SELECT recDate, openVal, highVal, lowVal, closeVal,
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) o
inner join
(SELECT recDate, openVal, highVal, lowVal, closeVal,
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) c
on o.Batch = c.Batch
inner join
(SELECT Batch, MAX(highVal) as highVal FROM
(SELECT recDate, openVal, highVal, lowVal, closeVal,
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) mxpre GROUP BY Batch) mx
on mx.Batch = o.Batch
inner join
(SELECT Batch, MIN(lowVal) as lowVal FROM
(SELECT recDate, openVal, highVal, lowVal, closeVal,
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) mnpre GROUP BY Batch) mn
on mn.Batch = o.Batch
WHERE o.BatchPos = 0 AND c.BatchPos = 1
结果应如下所示:
recDate openVal closeVal highVal lowVal
2016-01-03 17:00:00.000 1.08701 1.08712 1.08713 1.08701
2016-01-03 17:02:00.000 1.08708 1.08723 1.08723 1.08708
2016-01-03 17:04:00.000 1.08718 1.08712 1.08718 1.08701
2016-01-03 17:06:00.000 1.08721 1.08712 1.08721 1.0871
2016-01-03 17:08:00.000 1.08711 1.08708 1.08723 1.08708
我希望这就是你所追求的!
实际上,您希望将Batch和BatchPos预结果插入到临时表中,然后执行一系列自连接,而不是像上面那样动态实现所有内容!
答案 1 :(得分:1)