任何人都可以帮助一个SQL查询,每隔1分钟将逐个滴答的数据转换为OHLC(Open \ High \ Low \ Close)吗?我能够获得高低数据,但在打开和关闭时遇到困难。
我的示例数据如下所示:
idc ServerDateTime iDateTime sSymbol cAsk cBid Spread
---------------------------------------------------------------------------
2539581 2017-11-13 00:14:56.357 1510539296 EURUSD 1.16473 1.16460 0.00013
2539582 2017-11-13 00:14:56.373 1510539296 EURUSD 1.16475 1.16461 0.00014
2539583 2017-11-13 00:14:56.423 1510539296 EURUSD 1.16476 1.16462 0.00014
2539584 2017-11-13 00:14:56.520 1510539296 EURUSD 1.16477 1.16463 0.00014
2539585 2017-11-13 00:14:56.643 1510539296 EURUSD 1.16478 1.16463 0.00015
2539586 2017-11-13 00:14:58.207 1510539298 EURUSD 1.16478 1.16464 0.00014
2539587 2017-11-13 00:14:59.477 1510539299 EURUSD 1.16477 1.16464 0.00013
2539588 2017-11-13 00:15:00.337 1510539300 EURUSD 1.16477 1.16463 0.00014
2539589 2017-11-13 00:15:00.747 1510539300 EURUSD 1.16478 1.16463 0.00015
2539590 2017-11-13 00:15:00.873 1510539300 EURUSD 1.16477 1.16463 0.00014
2539591 2017-11-13 00:15:01.510 1510539301 EURUSD 1.16477 1.16464 0.00013
答案有一些类似的问题,但这些问题适用于python和MySQL。
答案 0 :(得分:2)
我终于找到了一个在此链接中找到的解决方案。 http://data.stackexchange.com/stackoverflow/query/61772/new
select min(ServerDateTime) as DateTime
, max(cBid) as Highest
, min(cBid) as Lowest
, min(case when rn_asc = 1 then [cBid] end) as first
, min(case when rn_desc = 1 then [cBid] end) as Last
from (
select row_number() over (
partition by cast(cast(ServerDateTime as float) * 60 * 24 as int)
order by ServerDateTime) as rn_asc
, row_number() over (
partition by cast(cast(ServerDateTime as float) * 60 * 24 as int)
order by ServerDateTime desc) as rn_desc
, *
from dbo.MT4TICK
) as SubQueryAlias
group by
cast(cast(ServerDateTime as float) * 60 * 24 as int)
order by DateTime