我已经在这上面挣扎了6个小时,这让我很生气。我来帮忙......
我玩股票价格并且以10分钟的间隔排列交易价格(要价,日期时间,名称等)。我想得到每笔交易的清单,以及交易在下一个小时的交易中有多高。
以下是我到目前为止所玩的内容。我想添加一个' max'返回MAX价格的列。 - 目前以下查询返回JOIN
中的所有交易(重复c.id
' s),而不是使用MAX列进行单笔交易。
如果我使用limit 1
的JOIN,由于a.market = c.market
我得到NULL值,如果我使用MAX(),我会得到分组错误,无论我做什么,我都无法做到工作。如果改变任何答案,ql_mode = only_full_group_by也会打开。
示例数据/查询:http://sqlfiddle.com/#!9/9c61f4/1
SELECT
c.id,
c.ask AS trade_cost_at_time,
c.timestamp,
a.ask AS next_hour_top_price,
a.timestamp AS next_hour_top_price_time
FROM trade_data c
JOIN (
SELECT l1.market, l1.ask, l1.timestamp FROM trade_data l1
) a ON a.market = c.market AND a.timestamp BETWEEN DATE_FORMAT(DATE(c.timestamp), "%Y-%m-%d %H:%i:%s") AND DATE_FORMAT(DATE_ADD(DATE(c.timestamp), INTERVAL 100 HOUR), "%Y-%m-%d %H:%i:%s")
基于上述SQLFiddle的预期结果将是:
id | trade_cost_at_time | timestamp | next_hour_top_price | next_hour_top_price_time
1604 | 0.00001212 | 2017-12-15 16:30:03 | 0.00001220
1804 | 0.00001220 | 2017-12-15 16:40:02 | 0.00001218
2004 | 0.00001218 | 2017-12-15 17:00:02 | 0.00001205
...
5588 | 0.00001171 | 2017-12-15 20:10:02 | 0.00001184
...
10777 | 0.00001204 | 2017-12-16 00:10:03 | 0.00001199
11177 | 0.00001199 | NULL | NULL
提前致谢。
答案 0 :(得分:0)
我不确定10分钟的时间间隔,所以我created a numbers table所以我可以产生10分钟的间隔范围。您可能不需要这个,或者您可能已经有了另一种方法。无论如何,以下查询在2个阶段中使用2个相关子查询。第一个是在接下来的一小时内获得max(ask)
值,然后第二个使用该值来匹配达到该价格的那个小时内的min(timestamp)
。
查询1 :
select
*
, (select min(nxt.timestamp) from trade_data nxt
where nxt.market = d.market
and nxt.timestamp > d.start_time
and nxt.timestamp < date_add(d.start_time, interval 1 hour)
and nxt.ask = d.next_hour_top_price
limit 1
) as next_hour_top_time
from (
select
tr.n
, td.market
, tr.start_time
, coalesce(min(td.ask),0) ask
, count(td.timestamp) num_trades
, (select max(nxt.ask) from trade_data nxt
where nxt.market = td.market
and nxt.timestamp > tr.start_time
and nxt.timestamp < date_add(tr.start_time, interval 1 hour)
limit 1
) as next_hour_top_price
from (
-- generate table of time ranges
select n
, date_add(str_to_date('2017-12-15 16:00','%Y-%m-%d %H:%i'), interval (n*10) minute) as start_time
, date_add(str_to_date('2017-12-15 16:00','%Y-%m-%d %H:%i'), interval ((n+1)*10) minute) as end_time
from numbers
where n between 0 and 60
) tr
left outer join trade_data td on td.timestamp >= tr.start_time and td.timestamp < tr.end_time
group by tr.n, td.market, tr.start_time
) d
;
<强> Results 强>:
| n | market | start_time | ask | next_hour_top_price | next_hour_top_time |
|----|------------|------------------|------------|---------------------|----------------------|
| 0 | (null) | 2017-12-15 16:00 | 0 | (null) | (null) |
| 1 | (null) | 2017-12-15 16:10 | 0 | (null) | (null) |
| 2 | STOCK-NAME | 2017-12-15 16:20 | 0.00001212 | 0.0000122 | 2017-12-15 16:30:03Z |
| 3 | STOCK-NAME | 2017-12-15 16:30 | 0.0000122 | 0.0000122 | 2017-12-15 16:30:03Z |
| 4 | STOCK-NAME | 2017-12-15 16:40 | 0.00001218 | 0.00001218 | 2017-12-15 16:40:02Z |
| 5 | STOCK-NAME | 2017-12-15 16:50 | 0.00001201 | 0.00001205 | 2017-12-15 17:00:02Z |
| 6 | STOCK-NAME | 2017-12-15 17:00 | 0.00001205 | 0.00001205 | 2017-12-15 17:00:02Z |
| 7 | STOCK-NAME | 2017-12-15 17:10 | 0.00001204 | 0.00001204 | 2017-12-15 17:10:02Z |
| 8 | STOCK-NAME | 2017-12-15 17:20 | 0.00001194 | 0.000012 | 2017-12-15 18:00:02Z |
| 9 | STOCK-NAME | 2017-12-15 17:30 | 0.0000118 | 0.000012 | 2017-12-15 18:00:02Z |
| 10 | STOCK-NAME | 2017-12-15 17:40 | 0.00001191 | 0.000012 | 2017-12-15 18:00:02Z |
| 11 | STOCK-NAME | 2017-12-15 17:50 | 0.00001199 | 0.000012 | 2017-12-15 18:00:02Z |
| 12 | STOCK-NAME | 2017-12-15 18:00 | 0.000012 | 0.000012 | 2017-12-15 18:00:02Z |
| 13 | STOCK-NAME | 2017-12-15 18:10 | 0.00001176 | 0.00001188 | 2017-12-15 18:20:02Z |
| 14 | STOCK-NAME | 2017-12-15 18:20 | 0.00001188 | 0.00001188 | 2017-12-15 18:20:02Z |
| 15 | STOCK-NAME | 2017-12-15 18:30 | 0.00001182 | 0.00001182 | 2017-12-15 18:30:02Z |
| 16 | STOCK-NAME | 2017-12-15 18:40 | 0.00001181 | 0.00001181 | 2017-12-15 18:40:03Z |
| 17 | STOCK-NAME | 2017-12-15 18:50 | 0.00001173 | 0.00001174 | 2017-12-15 19:30:02Z |
| 18 | STOCK-NAME | 2017-12-15 19:00 | 0.00001152 | 0.00001174 | 2017-12-15 19:30:02Z |
| 19 | STOCK-NAME | 2017-12-15 19:10 | 0.00001151 | 0.00001183 | 2017-12-15 20:00:02Z |
| 20 | STOCK-NAME | 2017-12-15 19:20 | 0.0000117 | 0.00001184 | 2017-12-15 20:10:02Z |
| 21 | STOCK-NAME | 2017-12-15 19:30 | 0.00001174 | 0.00001184 | 2017-12-15 20:10:02Z |
| 22 | STOCK-NAME | 2017-12-15 19:40 | 0.00001171 | 0.00001184 | 2017-12-15 20:10:02Z |
| 23 | STOCK-NAME | 2017-12-15 19:50 | 0.00001172 | 0.00001184 | 2017-12-15 20:10:02Z |
| 24 | STOCK-NAME | 2017-12-15 20:00 | 0.00001183 | 0.00001184 | 2017-12-15 20:10:02Z |
| 25 | STOCK-NAME | 2017-12-15 20:10 | 0.00001184 | 0.00001184 | 2017-12-15 20:10:02Z |
| 26 | STOCK-NAME | 2017-12-15 20:20 | 0.00001175 | 0.00001181 | 2017-12-15 21:00:02Z |
| 27 | STOCK-NAME | 2017-12-15 20:30 | 0.00001169 | 0.00001183 | 2017-12-15 21:20:02Z |
| 28 | STOCK-NAME | 2017-12-15 20:40 | 0.00001169 | 0.00001183 | 2017-12-15 21:20:02Z |
| 29 | STOCK-NAME | 2017-12-15 20:50 | 0.00001173 | 0.00001183 | 2017-12-15 21:20:02Z |
| 30 | STOCK-NAME | 2017-12-15 21:00 | 0.00001181 | 0.00001183 | 2017-12-15 21:20:02Z |
| 31 | STOCK-NAME | 2017-12-15 21:10 | 0.00001181 | 0.00001183 | 2017-12-15 21:20:02Z |
| 32 | STOCK-NAME | 2017-12-15 21:20 | 0.00001183 | 0.00001183 | 2017-12-15 21:20:02Z |
| 33 | STOCK-NAME | 2017-12-15 21:30 | 0.0000118 | 0.00001198 | 2017-12-15 22:20:02Z |
| 34 | STOCK-NAME | 2017-12-15 21:40 | 0.0000118 | 0.000012 | 2017-12-15 22:30:02Z |
| 35 | STOCK-NAME | 2017-12-15 21:50 | 0.0000118 | 0.0000122 | 2017-12-15 22:40:02Z |
| 36 | STOCK-NAME | 2017-12-15 22:00 | 0.00001179 | 0.0000124 | 2017-12-15 22:50:02Z |
| 37 | STOCK-NAME | 2017-12-15 22:10 | 0.0000118 | 0.0000124 | 2017-12-15 22:50:02Z |
| 38 | STOCK-NAME | 2017-12-15 22:20 | 0.00001198 | 0.0000124 | 2017-12-15 22:50:02Z |
| 39 | STOCK-NAME | 2017-12-15 22:30 | 0.000012 | 0.0000124 | 2017-12-15 22:50:02Z |
| 40 | STOCK-NAME | 2017-12-15 22:40 | 0.0000122 | 0.0000124 | 2017-12-15 22:50:02Z |
| 41 | STOCK-NAME | 2017-12-15 22:50 | 0.0000124 | 0.0000124 | 2017-12-15 22:50:02Z |
| 42 | STOCK-NAME | 2017-12-15 23:00 | 0.00001219 | 0.00001219 | 2017-12-15 23:00:03Z |
| 43 | STOCK-NAME | 2017-12-15 23:10 | 0.00001208 | 0.00001211 | 2017-12-15 23:50:02Z |
| 44 | STOCK-NAME | 2017-12-15 23:20 | 0.00001194 | 0.00001211 | 2017-12-15 23:50:02Z |
| 45 | STOCK-NAME | 2017-12-15 23:30 | 0.00001209 | 0.00001211 | 2017-12-15 23:50:02Z |
| 46 | STOCK-NAME | 2017-12-15 23:40 | 0.00001209 | 0.00001211 | 2017-12-15 23:50:02Z |
| 47 | STOCK-NAME | 2017-12-15 23:50 | 0.00001211 | 0.00001211 | 2017-12-15 23:50:02Z |
| 48 | STOCK-NAME | 2017-12-16T00:00 | 0.00001204 | 0.00001204 | 2017-12-16T00:00:02Z |
| 49 | STOCK-NAME | 2017-12-16T00:10 | 0.00001199 | 0.00001199 | 2017-12-16T00:10:03Z |
| 50 | STOCK-NAME | 2017-12-16T00:20 | 0.00001199 | 0.00001199 | 2017-12-16T00:20:02Z |
设置
CREATE TABLE `trade_data` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`market` varchar(255) DEFAULT NULL,
`ask` decimal(20,8) DEFAULT NULL,
`timestamp` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11374 DEFAULT CHARSET=latin1;
INSERT INTO `trade_data` (`id`, `market`, `ask`, `timestamp`)
VALUES
(1604, 'STOCK-NAME', 0.00001212, '2017-12-15 16:20:02'),
(1804, 'STOCK-NAME', 0.00001220, '2017-12-15 16:30:03'),
(2004, 'STOCK-NAME', 0.00001218, '2017-12-15 16:40:02'),
(2204, 'STOCK-NAME', 0.00001201, '2017-12-15 16:50:02'),
(2404, 'STOCK-NAME', 0.00001205, '2017-12-15 17:00:02'),
(2603, 'STOCK-NAME', 0.00001204, '2017-12-15 17:10:02'),
(2802, 'STOCK-NAME', 0.00001194, '2017-12-15 17:20:02'),
(3001, 'STOCK-NAME', 0.00001180, '2017-12-15 17:30:04'),
(3200, 'STOCK-NAME', 0.00001191, '2017-12-15 17:40:02'),
(3399, 'STOCK-NAME', 0.00001199, '2017-12-15 17:50:02'),
(3598, 'STOCK-NAME', 0.00001200, '2017-12-15 18:00:02'),
(3797, 'STOCK-NAME', 0.00001176, '2017-12-15 18:10:03'),
(3996, 'STOCK-NAME', 0.00001188, '2017-12-15 18:20:02'),
(4195, 'STOCK-NAME', 0.00001182, '2017-12-15 18:30:02'),
(4394, 'STOCK-NAME', 0.00001181, '2017-12-15 18:40:03'),
(4593, 'STOCK-NAME', 0.00001173, '2017-12-15 18:50:02'),
(4792, 'STOCK-NAME', 0.00001152, '2017-12-15 19:00:02'),
(4991, 'STOCK-NAME', 0.00001151, '2017-12-15 19:10:03'),
(5190, 'STOCK-NAME', 0.00001170, '2017-12-15 19:20:02'),
(5389, 'STOCK-NAME', 0.00001174, '2017-12-15 19:30:02'),
(5588, 'STOCK-NAME', 0.00001171, '2017-12-15 19:40:03'),
(5787, 'STOCK-NAME', 0.00001172, '2017-12-15 19:50:02'),
(5986, 'STOCK-NAME', 0.00001183, '2017-12-15 20:00:02'),
(6185, 'STOCK-NAME', 0.00001184, '2017-12-15 20:10:02'),
(6384, 'STOCK-NAME', 0.00001175, '2017-12-15 20:20:03'),
(6583, 'STOCK-NAME', 0.00001169, '2017-12-15 20:30:02'),
(6782, 'STOCK-NAME', 0.00001169, '2017-12-15 20:40:03'),
(6981, 'STOCK-NAME', 0.00001173, '2017-12-15 20:50:02'),
(7180, 'STOCK-NAME', 0.00001181, '2017-12-15 21:00:02'),
(7379, 'STOCK-NAME', 0.00001181, '2017-12-15 21:10:03'),
(7578, 'STOCK-NAME', 0.00001183, '2017-12-15 21:20:02'),
(7777, 'STOCK-NAME', 0.00001180, '2017-12-15 21:30:02'),
(7977, 'STOCK-NAME', 0.00001180, '2017-12-15 21:40:02'),
(8177, 'STOCK-NAME', 0.00001180, '2017-12-15 21:50:03'),
(8377, 'STOCK-NAME', 0.00001179, '2017-12-15 22:00:02'),
(8577, 'STOCK-NAME', 0.00001180, '2017-12-15 22:10:02'),
(8777, 'STOCK-NAME', 0.00001198, '2017-12-15 22:20:02'),
(8977, 'STOCK-NAME', 0.00001200, '2017-12-15 22:30:02'),
(9177, 'STOCK-NAME', 0.00001220, '2017-12-15 22:40:02'),
(9377, 'STOCK-NAME', 0.00001240, '2017-12-15 22:50:02'),
(9577, 'STOCK-NAME', 0.00001219, '2017-12-15 23:00:03'),
(9777, 'STOCK-NAME', 0.00001208, '2017-12-15 23:10:03'),
(9977, 'STOCK-NAME', 0.00001194, '2017-12-15 23:20:02'),
(10177, 'STOCK-NAME', 0.00001209, '2017-12-15 23:30:02'),
(10377, 'STOCK-NAME', 0.00001209, '2017-12-15 23:40:02'),
(10577, 'STOCK-NAME', 0.00001211, '2017-12-15 23:50:02'),
(10777, 'STOCK-NAME', 0.00001204, '2017-12-16 00:00:02'),
(10977, 'STOCK-NAME', 0.00001199, '2017-12-16 00:10:03'),
(11177, 'STOCK-NAME', 0.00001199, '2017-12-16 00:20:02');
-- create numbers table structure (very basic)
create table `numbers` (
`n` int(11) not null,
primary key (`n`)
) engine=InnoDB default charset=latin1;
-- insert generated number sequence (e.g., 1-1,000,000)
insert into `numbers`(n)
select @rownum:=@rownum+1
from (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) a, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) b, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) c, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) d, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) e, (
select 0 union select 1 union select 2 union select 3
union select 4 union select 5 union select 6
union select 7 union select 8 union select 9
) f, (select @rownum:=-1) r
;