1. 程式人生 > >mysql傳入一個時間範圍 查詢每一天的彙總資料 以及es-sql的寫法

mysql傳入一個時間範圍 查詢每一天的彙總資料 以及es-sql的寫法

案例:

select
  sum(quantity) as quantity,
  sum(charge_weight) as charge_weight,
  sum(balance_amount) as balance_amount,
  DATE_FORMAT(create_time,'%Y-%m-%d'),
  create_time as t
from wd_income_details
where create_time >'2018-07-01 00:00:00' and create_time <'2018-08-15 00:00:00'
group by  DATE_FORMAT(create_time,'%Y-%m-%d');

此方法會返回每天的計算結果;所以簡單的統計就不需要做一個定時任務

DATE_FORMAT()需要傳入一個時間datetime 然後轉化為格式化的樣子

Es-sql的寫法:

SELECT
  sum(quantity) as quantity,
  sum(charge_weight) as charge_weight,
  sum(balance_amount) as balance_amount,
  create_time as t
FROM wd_income_details
where create_time >'2018-07-01 00:00:00' and create_time <'2018-08-15 00:00:00'
group by
date_histogram(field='create_time',
'format'='yyyy-MM-dd','interval'='day','alias'='time')