获取每月行数的平均值,并按用户

时间:2018-03-01 04:13:41

标签: mysql

我有这些表格:

Used tables

表格的小提琴:http://sqlfiddle.com/#!9/18c65

使用此查询:

SELECT user_id AS user, round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user ORDER BY average ASC LIMIT 10

我获得按用户分组的货运平均值(cost_freight)(粗体值是经过身份验证的用户)

first query

但现在我需要按用户分组的每个月的平均货运费(cost_freight),结果应该是这样的:

enter image description here

这是我尝试了几种方法,但我无法得到理想的结果。

我非常感谢你的帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

将一列(MONTH(日期))用于分组依据。试试这个,

SELECT user_id AS user,MONTH(date), round(AVG(cost_freight), 0) AS average FROM `freights` GROUP BY user,MONTH(date) ORDER BY average ASC LIMIT 10