Mysql按时间选择数据但不忽略0值的时间

时间:2017-09-04 11:27:05

标签: mysql sql

+---------+----------------------------+------------+------------+------------+--------------+-----------+
| Id      | Date                       | Quantity   | Price      | Total      | FillType     | OrderType |
+---------+----------------------------+------------+------------+------------+--------------+-----------+
| 9362090 | 2017-09-04 10:59:28.237000 | 0.01643553 |     0.0196 | 0.00032213 | PARTIAL_FILL | SELL      |
| 9362089 | 2017-09-04 10:59:28.237000 |      0.345 |     0.0197 |  0.0067965 | FILL         | SELL      |
| 9362087 | 2017-09-04 10:58:49.017000 |      0.655 |     0.0197 |  0.0129035 | PARTIAL_FILL | SELL      |
| 9362086 | 2017-09-04 10:58:49.017000 |      0.345 | 0.01970001 |  0.0067965 | FILL         | SELL      |
| 9362078 | 2017-09-04 10:57:24.453000 |          1 |     0.0199 |     0.0199 | FILL         | SELL      |
| 9362077 | 2017-09-04 10:57:15.093000 |  5.3580217 | 0.01999296 | 0.10712271 | FILL         | BUY       |
| 9362076 | 2017-09-04 10:57:15.093000 | 0.76888586 | 0.01999237 | 0.01537185 | PARTIAL_FILL | BUY       |
| 9362075 | 2017-09-04 10:57:15.093000 | 0.28599999 | 0.01999235 | 0.00571781 | PARTIAL_FILL | BUY       |
| 9362061 | 2017-09-04 10:52:32.660000 |      0.055 | 0.01999249 | 0.00109958 | FILL         | BUY       |
| 9362030 | 2017-09-04 10:48:03.530000 |  0.0279849 | 0.01999262 | 0.00055949 | FILL         | BUY       |
| 9362029 | 2017-09-04 10:48:03.530000 | 0.40474542 | 0.01999262 | 0.00809192 | PARTIAL_FILL | BUY       |
| 9361976 | 2017-09-04 10:20:32.600000 | 0.38150547 |  0.0196015 | 0.00747807 | FILL         | BUY       |
| 9361974 | 2017-09-04 10:20:08.850000 | 1.58443174 | 0.01950609 | 0.03090606 | PARTIAL_FILL | SELL      |

如何获得每分钟数量行的总和,但如果该分钟内没有任何数据则返回0.

我使用的是在没有任何音量的情况下它没有返回0。

select sum(Quantity) from database.table  GROUP BY (UNIX_TIMESTAMP(Date)) DIV 30

理想情况下,输出将是这样的

2017-09-04 10:59 - 0.5321
2017-09-04 10:58 - 1.000
2017-09-04 10:57 - 6.604
2017-09-04 10:56 - 0
2017-09-04 10:55 - 0
..........

谢谢,如果有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用left join和您想要的时间戳来执行此操作:

select dt.dt, sum(Quantity)
from (select '2017-09-04 10:59' as dt union all
      select '2017-09-04 10:58' as dt union all
      select '2017-09-04 10:57' as dt union all
      select '2017-09-04 10:56' as dt union all
      select '2017-09-04 10:55' as dt
     ) dt left join
     database.table  t
     on dt.dt = date_format(date, '%Y-%m-%s %H:%i)
group by dt.dt
order by dt.dt;