我有的
1.我的域“User_Order”
CREATE TABLE `user_order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created_date` datetime DEFAULT NULL,
`fees` decimal(19,2) NOT NULL,
`instrument` varchar(255) NOT NULL,
`is_limit_order` bit(1) NOT NULL,
`market_id` bigint(20) NOT NULL,
`order_id` varchar(255) NOT NULL,
`order_status` varchar(255) NOT NULL,
`order_type` varchar(255) NOT NULL,
`price` decimal(20,8) NOT NULL,
`quantity` decimal(20,8) NOT NULL,
`remaining_quantity` decimal(20,8) NOT NULL,
`updated_date` datetime DEFAULT NULL,
`user_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_t5ah1x4wm9314qclf90dy0lyu` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. 我的表中有以下记录。
| id | created_date | fees | instrument | is_limit_order | market_id | order_id | order_status | order_type | price | quantity | remaining_quantity | updated_date | user_id |
+----+---------------------+--------+------------+----------------+-----------+------------------+--------------+------------+-------------+------------+--------------------+---------------------+---------+
| 1 | 2017-09-15 05:08:57 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000001 | EXECUTED | BUY | 10.00000000 | 2.00000000 | 0.00000000 | 2017-09-15 05:08:57 | 3 |
| 2 | 2017-09-15 05:09:34 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000001 | EXECUTED | SELL | 10.00000000 | 2.00000000 | 0.00000000 | 2017-09-15 05:09:34 | 4 |
| 3 | 2017-09-15 05:11:18 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000002 | CANCELLED | BUY | 2.00000000 | 1.00000000 | 1.00000000 | 2017-09-15 05:11:18 | 3 |
| 4 | 2017-09-15 05:12:43 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000003 | EXECUTED | BUY | 4.00000000 | 2.00000000 | 0.00000000 | 2017-09-15 05:12:43 | 3 |
| 5 | 2017-09-15 05:23:10 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000002 | EXECUTED | SELL | 4.00000000 | 2.00000000 | 0.00000000 | 2017-09-15 05:23:10 | 4 |
| 6 | 2017-09-15 08:34:17 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000003 | OPEN | BUY | 2.00000000 | 1.00000000 | 1.00000000 | 2017-09-15 08:34:17 | 4 |
| 7 | 2017-09-15 08:36:32 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000004 | CANCELLED | SELL | 1.00000000 | 2.00000000 | 2.00000000 | 2017-09-15 08:36:32 | 3 |
| 8 | 2017-09-15 09:24:22 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000004 | OPEN | BUY | 2.00000000 | 1.00000000 | 1.00000000 | 2017-09-15 09:24:22 | 4 |
| 9 | 2017-09-15 09:30:19 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000005 | OPEN | BUY | 1.00000000 | 2.00000000 | 2.00000000 | 2017-09-15 09:30:19 | 4 |
| 10 | 2017-09-15 09:32:10 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000005 | CANCELLED | SELL | 1.00000000 | 2.00000000 | 2.00000000 | 2017-09-15 09:32:10 | 3 |
| 11 | 2017-09-15 10:02:57 | 0.0100 | INBTCINR | | 1 | 12:05:52-0000006 | OPEN | SELL | 2.00000000 | 4.00000000 | 4.00000000 | 2017-09-15 10:02:57 | 3 |
| 12 | 2017-09-15 10:16:19 | 0.0100 | INBTCINR | | 1 | 12:25:43-0000006 | OPEN | BUY | 4.00000000 | 2.00000000 | 2.00000000 | 2017-09-15 10:16:19 | 4 |
3. 我正在为每 5(300 秒)分钟间隔记录写下面的查询。
开始日期 = '2017-09-11 00:00:00' 结束日期 = '2017-09-15 23:59:59'
select FROM_UNIXTIME(floor(min(UNIX_TIMESTAMP(created_date))/300)*300)
as timestampDate,
sum(quantity) as volume,sum(price*quantity)/sum(quantity) as wavg_price,
substring_index(min(concat('created_date','_',price)),
'_',-1) as open,
max(price) as high,
min(price) as low,
substring_index(max(concat ('created_date','_',price)),
'_',-1) as 'close'
from user_order
where created_date between '2017-09-11 00:00:00' AND '2017-09-15 23:59:59'
group by floor(unix_timestamp(created_date)/300)
order by created_date;
上述查询的结果:-
timestampDate | volume | wavg_price | open | high | low | close |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+
| 2017-09-15 05:05:00 | 4.00000000 | 10.00000000000000000000 | 10.00000000 | 10.00000000 | 10.00000000 | 10.00000000 |
| 2017-09-15 05:10:00 | 3.00000000 | 3.33333333333333333333 | 2.00000000 | 4.00000000 | 2.00000000 | 4.00000000 |
| 2017-09-15 05:20:00 | 2.00000000 | 4.00000000000000000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 |
| 2017-09-15 08:30:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
| 2017-09-15 08:35:00 | 2.00000000 | 1.00000000000000000000 | 1.00000000 | 1.00000000 | 1.00000000 | 1.00000000 |
| 2017-09-15 09:20:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
| 2017-09-15 09:30:00 | 4.00000000 | 1.00000000000000000000 | 1.00000000 | 1.00000000 | 1.00000000 | 1.00000000 |
| 2017-09-15 10:00:00 | 4.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
| 2017-09-15 10:15:00 | 2.00000000 | 4.00000000000000000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+
4. 上面的结果只给出表永久记录,即使我需要每个间隔的所有记录具有默认值或以前的记录值(打开、关闭、高、低及其时间戳)
预期输出:-
日期范围 = '2017-09-11 00:00:00' 和 '2017-09-15 23:59:59'
timestampDate | volume | wavg_price | open | high | low | close |
+---------------------+------------+-------------------------+-------------+-------------+-------------+-------------+
| 2017-09-11 00:05:00 | 4.00000000 | 10.00000000000000000000 | 10.00000000 | 10.00000000 | 10.00000000 | 10.00000000 |
| 2017-09-11 00:10:00 | 3.00000000 | 3.33333333333333333333 | 2.00000000 | 4.00000000 | 2.00000000 | 4.00000000 |
| 2017-09-11 00:15:00 | 2.00000000 | 4.00000000000000000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 |
................................................
.................................................
.................................................
| 2017-09-15 23:05:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
| 2017-09-15 23:10:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
..........................................................
..........................................................
..........................................................
| 2017-09-15 23:50:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
| 2017-09-15 23:55:00 | 1.00000000 | 2.00000000000000000000 | 2.00000000 | 2.00000000 | 2.00000000 | 2.00000000 |
我想要的是
- 即使我需要所有具有默认值或先前记录值(打开、关闭、高、低及其时间戳)的记录,对于每个间隔
情况,输出也只给出表持续记录 a)
这里,在 '2017-09-11 00 上没有记录:05:00'。在这种情况下,开、关、高、低值将设置为 0(零)。
案例b)
在这里,“2017-09-15 05:25:00”、“2017-09-15 05:30:00”等没有记录,但它位于“2017-09-”这两个记录之间15 05:20:00' 和 2017-09-15 08:30:00。在这种情况下,'2017-09-15 05:25:00' 的 ohlc 值应该是前一个日期时间('2017-09-15 05:15:00')设置的 ohlc 值。
谢谢