MySql平均值和按用法分组

时间:2017-12-08 07:25:59

标签: mysql sql mariadb

报告表

+----+-------------------+--------+---------------------+
| id | mac_address       |quantity| zaman               |
+----+-------------------+--------+---------------------+
| 12 | 26-C0-B7-1E-7A-8C |   001  | 2017-12-07 10:22:09 |
| 14 | 26-C0-B7-1E-7A-8C |   001  | 2017-12-07 10:28:44 |
| 15 | 26-C0-B7-1E-7A-8D |   001  | 2017-12-07 10:44:54 |
| 19 | 26-C0-B7-1E-7A-AC |   001  | 2017-12-08 10:11:00 |
| 20 | 26-C0-B7-1E-7A-AD |   002  | 2017-12-08 10:32:12 |
| 24 | 26-C0-B7-1E-7A-8D |   001  | 2017-12-09 10:29:54 |
| 25 | 26-C0-B7-1E-7A-8E |   002  | 2017-12-09 10:39:11 |

我想根据这个等式找出天数之间的平均数量:

Sum of quantity(9)/Number of day(3) = 3

到目前为止,我有这个问题:

SELECT ROUND(AVG(quantity),2) AS quantity, DATE(zaman) as DateOnly 
FROM report WHERE DATE(zaman) BETWEEN ? AND ? 
GROUP BY DateOnly

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

尝试使用数量之和除以天数

select sum(quantity) /( datediff(max(zaman), min(zaman))) as quantity 

FROM report WHERE DATE(zaman) BETWEEN ? AND ? 

答案 1 :(得分:1)

试试这个:

SELECT ROUND((SUM(Quantity)*1.0)/datediff(MAX(Zaman), MIN(Zaman)),0)
FROM report 
WHERE zaman BETWEEN ? AND ?