计算多个列的平均值,并按group_id分组

时间:2013-04-22 08:01:31

标签: mysql

我正在尝试计算一个表的平均值,并将其放入一个新的表中,该表按MySQL中的组名分组。

在第一个表格(称为答案)中,我有许多小组的调查答案。答案是数值(INT,1-7),问题分组为问题组(文化表现等。)。所有答案都有一个响应者姓名,他们都属于一个具有* group_id *的组。我想汇总答案并计算所有问题组的平均值,并按group_id计算。

例如。我们可以假设答案中的数据如下所示:

||respondent | group_id | question_1 | question_2 | question_3| question_4 | question_5 | question_6||
||Joe        |1         |4           |3           |5          |4           |2           |2          ||
||Jane       |1         |3           |6           |6          |2           |1           |6          ||
||Jones      |1         |7           |3           |4          |1           |6           |4          ||
||Harry      |2         |2           |2           |3          |7           |5           |3          ||
||Pete       |2         |3           |5           |1          |4           |4           |5          ||
||Frank      |2         |1           |1           |2          |2           |7           |6          ||
||Sam        |3         |6           |7           |4          |6           |2           |2          ||
||Kim        |3         |3           |3           |6          |5           |1           |1          ||
||Todd       |3         |1           |4           |7          |4           |5           |7          ||

我现在想将问题1-3的平均值变为average_a,将问题4-6的平均值变为average_b,将其与所有组的不同表格(结果)进行比较并按group_id分组

现在表答案如下所示:

 ||group_id|average_a|average_b||<br>
    ||   1    | null  | null   ||<br>
    ||   2    |  null | null  ||<br>
    ||   3    |  null | null  ||<br>

我想将结果表更新为如下所示:

||group_id|average_a|average_b||
||   1    |  4,5556 |  3,1111 ||
||   2    |  2,2222 |  4,7778 ||
||   3    |  4,5556 |  3,6667 ||

修改

在原始答案表中添加一些详细信息:

+------------------------+------------------+------+-----+---------+----------------+<br>
| Field                  | Type             | Null | Key | Default | Extra          |<br>
+------------------------+------------------+------+-----+---------+----------------+<br>
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |<br>
| Respondent             | varchar(20)      | YES  |     | NULL    |                |<br>
| Email                  | varchar(39)      | YES  |     | NULL    |                |<br>
| Website                | varchar(60)      | YES  |     | NULL    |                |<br>
| Bransch                | varchar(60)      | YES  |     | NULL    |                |<br>
| Koncern                | varchar(60)      | YES  |     | NULL    |                |<br>
| Company                | varchar(60)      | YES  |     | NULL    |                |<br>
| typ_av_enhet           | varchar(60)      | YES  |     | NULL    |                |<br>
| avdelning              | varchar(60)      | YES  |     | NULL    |                |<br>
| typ_av_avdelning       | varchar(30)      | YES  |     | NULL    |                |<br>
| gruppid                | varchar(5)       | YES  |     | NULL    |                |<br>
| survey_no              | varchar(5)       | YES  |     | NULL    |                |<br>
| Grupp                  | varchar(28)      | YES  |     | NULL    |                |<br>
| group_type             | varchar(60)      | YES  |     | NULL    |                |<br>
| Kön                    | varchar(1)       | YES  |     | NULL    |                |<br>
| Ålder                  | varchar(5)       | YES  |     | NULL    |                |<br>
| Samarbetsträning       | varchar(1)       | YES  |     | NULL    |                |<br>
| Samarbetserfarenhet    | varchar(1)       | YES  |     | NULL    |                |<br>
| TaskClarity1           | varchar(1)       | YES  |     | NULL    |                |<br>
| TaskClarity2           | varchar(1)       | YES  |     | NULL    |                |<br>
| TaskClarity3           | varchar(1)       | YES  |     | NULL    |                |<br>

这里也是我尝试使用@ stefan的说明的脚本:

    <?php
$username = "root";
$password = "root";
$hostname = ":/Applications/MAMP/tmp/mysql/mysql.sock";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("ca",$dbhandle) 
  or die("Could not select ca");

//delete old table
$delete_sql="Drop Table results";
// Execute query
if (mysql_query($delete_sql))
  {
  echo "Table deleted successfully";
  }
else
  {
  echo "Error deleting table: " . mysql_error();
  }

//create table
$sql="CREATE TABLE results(Group_id INT PRIMARY KEY, TaskClarity FLOAT)";

// Execute query
if (mysql_query($sql))
  {
  echo "Table results created successfully";
  }
else
  {
  echo "Error creating table: " . mysql_error();
  }



//Calculate task clarity
$task_clarity = "
INSERT INTO results (
    Group_id,
    TaskClarity
)
(
   SELECT
     gruppid ,
     AVG((TaskClarity1 + TaskClarity2 + TaskClarity3)/3) as average_task_clarity
   FROM
     answers
   GROUP BY
     gruppid
) ON DUPLICATE KEY UPDATE TaskClarity = VALUES(TaskClarity)"
;
$resource_retrive_task_clarity = mysql_query($task_clarity);    //execute the query
if (! $resource_retrive_task_clarity = mysql_query($task_clarity) ){
    echo "Error reading from table";
    die;
}

if (! mysql_num_rows($resource_retrive_task_clarity ) ){
    echo "No records found in table";
}
else {
echo "funkar";

}

//close the connection
mysql_close($dbhandle);
?>

3 个答案:

答案 0 :(得分:8)

试试这个:

编辑更新结果表(假设group_id列具有UQ或PK索引)

INSERT INTO results (
    group_id,
    average_a,
    average_b
)
(
   SELECT
     group_id ,
     AVG((question_1 + question_2 + question_3)/3) as za_average_a,
     AVG((question_4 + question_5 + question_6)/3) as za_average_b
   FROM
     answers
   GROUP BY
     group_id
) ON DUPLICATE KEY UPDATE 
     average_a = VALUES(average_a), 
     average_b = VALUES(average_b)

更新示例设为here并且有效

答案 1 :(得分:2)

你可以这样做:

 INSERT INTO new_table(`group_id`,`average_a`,`average_b`)
    SELECT group_id,(AVG(question_1 ) + AVG(question_2 )  + AVG(question_3 ))/3,
    (AVG(question_4 ) + AVG(question_5 )  + AVG(question_6 ))/3  FROM old_table
    GROUP BY group_id

答案 2 :(得分:1)

您可以使用:

SELECT GROUP_ID
,(SUM(question_1)+SUM(question_2)+SUM(question_3))/(3*COUNT(GROUP_ID)) AS Avg1
,(SUM(question_4)+SUM(question_5)+SUM(question_6))/(3*COUNT(GROUP_ID)) AS Avg2
FROM answers
GROUP BY Group_Id;

/(3*COUNT(GROUP_ID) 3中的列数是列数(q1,q2,q3),而COUNT(GROUP_ID)是每个GROUP_ID的行数

See this SQLFiddle