1

これは私がクエリしているテーブルの構造です:

------------------------------------------------------------------
| 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か?望ましい結果を得る別の方法はありますか?

4

2 に答える 2

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フィドル

于 2013-02-16T19:37:30.803 に答える
0

ここにそれを行う簡単な方法があります

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

デモ

于 2013-02-16T19:44:16.117 に答える