- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个表data_summaries
。它有 item_id INT(11)
、user_grouping TEXT
和 value DECIMAL(10,2)
等列。
如果我尝试进行查询,按 user_grouping
对结果进行分组,并按 value
的 AVG
对结果进行排序,那么失败:
SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value
FROM data_summaries
GROUP BY user_grouping
ORDER BY avg_value
+---------------+-----------+-----------+
| user_grouping | avg_value | sum_value |
+---------------+-----------+-----------+
| London | 50.609733 | 18978.65 |
| Paris | 50.791733 | 19046.90 |
| New York | 51.500400 | 2575.02 |
| NULL | 49.775627 | 18665.86 |
+---------------+-----------+-----------+
ORDER BY
子句似乎确实在做某事,因为它确实改变了顺序:
SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value
FROM data_summaries
GROUP BY user_grouping
+---------------+-----------+-----------+
| user_grouping | avg_value | sum_value |
+---------------+-----------+-----------+
| NULL | 49.775627 | 18665.86 |
| New York | 51.500400 | 2575.02 |
| London | 50.609733 | 18978.65 |
| Paris | 50.791733 | 19046.90 |
+---------------+-----------+-----------+
另一方面,按 value
的 SUM
排序按预期工作:
SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value
FROM data_summaries
GROUP BY user_grouping
ORDER BY sum_value
+---------------+-----------+-----------+
| user_grouping | avg_value | sum_value |
+---------------+-----------+-----------+
| New York | 51.500400 | 2575.02 |
| NULL | 49.775627 | 18665.86 |
| London | 50.609733 | 18978.65 |
| Paris | 50.791733 | 19046.90 |
+---------------+-----------+-----------+
如果我改为按 item_id
分组,则可以按 AVG
排序:
SELECT item_id, AVG(value) AS avg_value, SUM(value) AS sum_value
FROM data_summaries
GROUP BY item_id
+---------+-----------+-----------+
| item_id | avg_value | sum_value |
+---------+-----------+-----------+
| 4 | 49.318225 | 11392.51 |
| 1 | 49.737835 | 11489.44 |
| 2 | 50.420606 | 11647.16 |
| 6 | 51.024242 | 11786.60 |
| 5 | 51.456537 | 11886.46 |
| 3 | 53.213000 | 1064.26 |
+---------+-----------+-----------+
我需要如何更改第一个查询以使其按平均值排序?
最佳答案
这是一个 MySQL 错误,请参阅 Unexpected order for grouped query ,这涉及 avg()
与按 text
列分组的组合。它在 MySQL 5.7.15 中仍然打开。
作为解决方法,您可以将数据类型更改为例如可变字符
。如果您不需要索引来加快速度,转换也应该有效:
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
更新:
错误已在 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)
关于mysql - 无法在 MySQL 中使用具有特定 GROUP BY 条件的 AVG 值进行 ORDER BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719785/
我想做一个平均值:问题是我正在计算每个元素的 AVG 的 1 个项目(工作)但是一旦我想要类别平均值的 GLOBAL 平均值(something and foo) 它不起作用(mysql 向我抛出一个
我使用 MySQL。我有以下格式的数据 - DATE , PAGE , PAGE_LOAD_DURATION , VISIT_TYPE 01-01-15 , A ,
我有下表: +--------+-------+ |Username|Points | +--------+-------+ |User1 | 75.00 | |User1 | 87.50 |
表格: a | b 1 | 15 2 | 10 3 | 20 4 | 30 查询: SELECT AVG(table.b) FROM table ORDER BY table.a ASC LIMIT
这是我的数据库 CREATE TABLE korisnici( name VARCHAR(30) NOT NULL, amount DECIMAL(65,2) ); INSER
这是我的数据库 CREATE TABLE korisnici( name VARCHAR(30) NOT NULL, amount DECIMAL(65,2) ); INSER
我正在尝试获取我的指标的平均交互次数,但此查询获取的是那些进行了交互的人的平均交互次数(那些在 metricsActions 中的人只有在他们已经交互时才会存在,他们的 metricsID 仍然存在于
我与三列有关系:ProductName、CategoryID 和 Price。我需要选择仅那些价格高于给定类别中平均产品价格的产品。(例如,当apple(ProductName)是fruit(Cate
我正在尝试使用 C++ 创建一个简单的程序。它计算用户输入的 5 个数字的平均值,但当我运行它时,它一直给我一个垃圾值。我花了 30 多分钟来解决这个问题,但我似乎无法弄清楚。 #include
总结一个练习题: 我需要从 datetime 列查询平均年份。我最初的解决方案是YEAR(AVG())所有日期。但由于我无法 AVG() 为 datetime,因此我将日期转换为 unix,然后再转换
我想从一个表中的一列 (value_to_count) 获得三个不同的平均值,其中所有这些平均值都有不同的 WHERE 子句(根据时间)。 示例数据: ###services#### Table se
我想创建一个 sql 查询,为 2 个不同的查询一起返回结果。例如,我想要以下形式的结果:产品名称, avg(price), min(price), max(price), avg(order), m
我有以下查询: SELECT ROUND(AVG( p.price ),2) as Avg_value FROM quotes inner join `system_users` ON quotes.
我想在 sql 中使用 AVG 函数来返回某些值的工作平均值(即基于上周而不是整体平均值)。我有两个正在计算的值,体重和 restingHR(心率)。我对每个都有以下 sql 语句: SELECT A
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
我有一个包含多个数字列的评论表。我想计算一个查询中所有列的平均值。 如果表格看起来像这样: { foo : 2, bar : 5, foobar : 10 }, { foo :
我正在为我的应用程序使用 SQL Azure SQL Server。我的应用程序直到最近都运行良好,MAX dtu 使用率为 100%,但 AVG DTU 使用率约为 50%。 我应该监控哪个值来扩展
我正在为我的应用程序使用 SQL Azure SQL Server。我的应用程序直到最近都运行良好,MAX dtu 使用率为 100%,但 AVG DTU 使用率约为 50%。 我应该监控哪个值来扩展
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有下表: Date | Product | Price 06-12-17 | 1.1 | 10 06-12-17 | 1.2 | 2
我是一名优秀的程序员,十分优秀!