MySql平均值和按用法分组

时间:2021-05-28 00:10:23

Report Table

报告表

+----+-------------------+--------+---------------------+
| 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 |

I want to find average of quantity between days according to this equation:

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

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

So far I have this query:

到目前为止,我有这个查询:

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

How can I achieve this?

我怎样才能做到这一点?

2 个解决方案

#1


2  

try using sum of the quantity divided by number of days

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

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

FROM report WHERE DATE(zaman) BETWEEN ? AND ? 

#2


1  

Try this:

尝试这个:

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

#1


2  

try using sum of the quantity divided by number of days

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

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

FROM report WHERE DATE(zaman) BETWEEN ? AND ? 

#2


1  

Try this:

尝试这个:

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