使用`group by`获得所需结果的正确方法是什么?

时间:2013-02-16 19:35:29

标签: mysql group-by

这是我要查询的表的结构:

------------------------------------------------------------------
| UserId  | ExperimentId | TaskId | TaskName | Errors | FileKey  |
-----------------------------------------------------------------
| 12      | 10           | 8      | Jumping   | 9      | 1200298 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Jumping   | 7      | 1200297 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Jumping   | 6      | 1200296 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Jumping   | 8      | 1200295 |
-----------------------------------------------------------------
| 12      | 10           | 6      | Writing   | 9      | 1200294 |
-----------------------------------------------------------------
| 12      | 10           | 6      | Writing   | 2      | 1200293 |
-----------------------------------------------------------------
| 12      | 10           | 6      | Writing   | 0      | 1200292 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Pointing  | 9      | 1200291 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Pointing  | 8      | 1200290 |
-----------------------------------------------------------------
| 12      | 10           | 8      | Pointing  | 7      | 1200289 |
------------------------------------------------------------------

我正在使用的查询:

select
    UserId,
    TaskName,
    sum(Errors) as Errors
from
    UserTaskStats
where
    TaskName = TaskName
and
    UserId = UserId
group by
    Errors, TaskName
order by
    UserId asc;

我得到的结果示例(忽略Errors列中值的准确性):

--------------------------------
| UserId  | TaskName  | Errors |
--------------------------------
| 12      | Jumping   | 3      |
--------------------------------
| 12      | Jumping   | 3      |
--------------------------------
| 12      | Writing   | 8      |
--------------------------------
| 12      | Jumping   | 8      |
--------------------------------
| 12      | Writing   | 6      |
--------------------------------
| 12      | Writing   | 6      |
--------------------------------
| 12      | Pointing  | 6      |
--------------------------------
| 12      | Pointing  | 8      |
--------------------------------
| 12      | Jumping   | 8      |
--------------------------------
| 12      | Writing   | 8      |
--------------------------------

我想要的结果:

---------------------------------
| UserId  | TaskName  | Errors  |
--------------------------------
| 12      | Jumping   | 30      |
--------------------------------
| 12      | Writing   | 11      |
--------------------------------
| 12      | Pointing  | 24      |
---------------------------------

我没有正确使用group by吗?还有另一种方法可以获得理想的结果吗?

2 个答案:

答案 0 :(得分:2)

你非常接近。

您希望每个UserId, TaskName组都有一行,所以只需要group by UserId, TaskName

SELECT UserId,
       TaskName,
       sum(Errors) AS Errors
FROM   UserTaskStats
GROUP  BY UserId,
          TaskName
ORDER  BY UserId ASC; 

SQL Fiddle

答案 1 :(得分:0)

这是一种简单的方法

select
  user_id,
  task_name,
  sum(errors)
from test
group by user_id , task_name

Demo