Can not ORDER BY an AVG value with certain GROUP BY criteria in MySQL

浪子不回头ぞ 提交于 2019-12-05 13:18:49

That is a MySQL bug, see Unexpected order for grouped query, that involves avg() in combination with grouping by a text-column. It is still open in MySQL 5.7.15.

As a workaround, you can change your datatype to e.g. varchar. If you don't need indexes to speed it up, casting should work too:

SELECT cast(user_grouping as char(200)), AVG(value) AS avg_value, SUM(value) AS sum_value
FROM data_summaries
GROUP BY cast(user_grouping as char(200))
ORDER BY avg_value

Update:

The bug is fixed in MySQL 5.7.17:

Queries that were grouped on a column of a BLOB-based type, and that were ordered on the result of the AVG(), VAR_POP(), or STDDEV_POP() aggregate function, returned results in the wrong order if InnoDB temporary tables were used. (Bug #22275357, Bug #79366)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!