MySQL 带有 avg 和 group by 的 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10702546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:28:50  来源:igfitidea点击:

SQL query with avg and group by

mysqlsqlgroup-byaverage

提问by theFen

I have some problems with writing a SQL query for MySQL. I have a table with the following structure:

我在为 MySQL 编写 SQL 查询时遇到了一些问题。我有一个具有以下结构的表:

mysql> select id, pass, val from data_r1 limit 10;
+------------+--------------+----------------+
| id         | pass         | val            |
+------------+--------------+----------------+
| DA02959106 | 5.0000000000 |  44.4007000000 |
| 08A5969201 | 1.0000000000 | 182.4100000000 |
| 08A5969201 | 2.0000000000 | 138.7880000000 |
| DA02882103 | 5.0000000000 |  44.7265000000 |
| DA02959106 | 1.0000000000 | 186.1470000000 |
| DA02959106 | 2.0000000000 | 148.2660000000 |
| DA02959106 | 3.0000000000 | 111.9050000000 |
| DA02959106 | 4.0000000000 |  76.1485000000 |
| DA02959106 | 5.0000000000 |  44.4007000000 |
| DA02959106 | 4.0000000000 |  76.6485000000 |

I want to create a query that extracts the following information from the table:

我想创建一个从表中提取以下信息的查询:

id, AVG of 'val' for 'pass' = 1, AVG of 'val' for 'pass' = 2, etc

The result of the query should look like this:

查询结果应如下所示:

+------------+---------+---------+---------+---------+---------+---------+---------+
| id         | val_1   | val_2   | val_3   | val_4   | val_5   | val_6   | val_7   |
+------------+---------+---------+---------+---------+---------+---------+---------+
| DA02959106 | 186.147 | 148.266 | 111.905 | 76.3985 | 44.4007 | 0       | 0       |
+------------+---------+---------+---------+---------+---------+---------+---------+

with more rows for each unique 'id', of course.

当然,每个唯一的“id”都有更多行。

I already tried some queries like

我已经尝试过一些查询,例如

SELECT id, pass, AVG(val) AS val_1 FROM data_r1 WHERE pass = 1 GROUP BY id;

This returns the correct result, but I have to expand it with results for the other possible values of 'pass' (up to 7)

这将返回正确的结果,但我必须使用其他可能的“pass”值(最多 7)的结果来扩展它

I tried to use a nested SELECT within AVG but this didn't work because I didn't figure out how to correctly limit it to the current 'id'.

我尝试在 AVG 中使用嵌套的 SELECT 但这不起作用,因为我不知道如何正确地将它限制为当前的“id”。

I then created Views to represent the result of each query for 'pass' = 1, 'pass' = 2, etc. But for most ids the highest value for 'pass' is 5. When using JOIN queries to get the final result from the views I received an empty result set, because some of the Views are empty / don't have values for a specific 'id'.

然后我创建了视图来表示每个查询的结果,如 'pass' = 1、'pass' = 2 等。但对于大多数 id,'pass' 的最高值为 5。当使用 JOIN 查询获取最终结果时我收到一个空结果集的视图,因为某些视图是空的/没有特定“id”的值。

Any ideas?

有任何想法吗?

回答by Marco

If I understand what you need, try this:

如果我明白你需要什么,试试这个:

SELECT id, pass, AVG(val) AS val_1 
FROM data_r1 
GROUP BY id, pass;

Or, if you want just one row for every id, this:

或者,如果您只想为每个 id 分配一行,则:

SELECT d1.id,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 1) as val_1,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 2) as val_2,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 3) as val_3,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 4) as val_4,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 5) as val_5,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 6) as val_6,
    (SELECT IFNULL(ROUND(AVG(d2.val), 4) ,0) FROM data_r1 d2 
     WHERE d2.id = d1.id AND pass = 7) as val_7
from data_r1 d1
GROUP BY d1.id

回答by Cristian Iordache

As I understand, you want the average value for each id at each pass. The solution is

据我了解,您希望每次通过时每个 id 的平均值。解决办法是

SELECT id, pass, avg(value) FROM data_r1
GROUP BY id, pass;