2

次のようなテーブルがあるとします。

Class   |   Subject | Student   | Marks
----------------------------------------
1       |   Maths   |   A       |   70   
1       |   Eng     |   B       |   80
1       |   IT      |   A       |   90 
1       |   IT      |   C       |   80 
2       |   Maths   |   D       |   60   
2       |   Eng     |   E       |   75
2       |   Maths   |   E       |   90 
2       |   IT      |   F       |   80 
3       |   Maths   |   A       |   160   
3       |   Eng     |   B       |   165
3       |   IT      |   G       |   90 

次のように出力したい

Class   |   Student     | Marks
----------------------------------------
1       |       A       |   160   
2       |       E       |   165
3       |       B       |   165 

つまり、結果には、マークの合計が最大のクラスごとの学生名が含まれます。これに対する SQL クエリの書き方は? たとえば、クラス 1 の場合、生徒 A は 70 + 90 = 160 で、B と C の両方で 80 で最大になります。

4

7 に答える 7

5

1 つの解決策は、生徒がクラスごとに持つ最大ポイントを計算し、それをフィルター結合として使用することです。

select  ClassStudentSum.*
from    (
        select  class
        ,       student
        ,       sum(Marks) as SumMarks
        from    YourTable
        group by
                class
        ,       student
        ) as ClassStudentSum
join    (
        select  class
        ,       max(SumMarks) as MaxSumMarks
        from    (
                select  class
                ,       student
                ,       sum(Marks) as SumMarks
                from    YourTable
                group by
                        class
                ,       student
                ) ClassStudentSum2
        group by
                class
        ) MaxPerClass
on      MaxPerClass.class = ClassStudentSum.class
        and MaxPerClass.MaxSumMarks = ClassStudentSum.SumMarks

SQL Fiddle での実例。

于 2013-05-07T11:17:22.210 に答える
2

より伝統的な(そしてとても遅い)アプローチ...

SELECT x.*
  FROM
     ( SELECT class
            , student
            , SUM(marks) ttl_marks
         FROM yourtable 
        GROUP
           BY class
            , student
     ) x
  LEFT 
  JOIN
     ( SELECT class
            , student
            , SUM(marks) ttl_marks
         FROM yourtable 
        GROUP
           BY class
            , student
     ) y
    ON y.class = x.class
   AND y.ttl_marks > x.ttl_marks
 WHERE y.class IS NULL;
于 2013-05-07T12:22:11.687 に答える
1

正しいクエリは次のとおりです。

select class, student, sums.mark
from (select class, student, sum(marks) as mark
      from student
      group by class, student
      order by mark desc) sums
group by class
于 2013-05-07T12:38:07.293 に答える
1

このクエリを試してください

クエリ 1 :

select a.*, if(@prv=class, 1, 0) as flag, @prv:=class from 
(select class,student, sum(marks) as total from table1 
group by class, student
order by class, total desc)a join (select @prv:=0)tmp
where if(@prv=class, 1, 0) = 0

SQLフィドル

| CLASS | STUDENT | TOTAL | FLAG | @PRV:=CLASS |
------------------------------------------------
|     1 |       A |   160 |    0 |           1 |
|     2 |       E |   165 |    0 |           2 |
|     3 |       B |   165 |    0 |           3 |

お役に立てれば

于 2013-05-07T11:36:33.897 に答える
0

これはもっと単純なクエリだと思います。それは正常に動作します。結合する必要はありません。

SELECT class,
(
SELECT student FROM yourtable
WHERE class = YT.class GROUP BY student
ORDER BY SUM(marks) DESC
LIMIT 1
) AS student,
(
SELECT SUM(marks) FROM yourtable
WHERE class = YT.class GROUP BY student
ORDER BY SUM(marks) DESC
LIMIT 1
) AS marks
FROM yourtable AS YT
GROUP BY class

于 2013-05-08T07:18:48.420 に答える
-1
    SQL> with cte as
      2  (select class, student, sum(marks) marks
      3  from engineer
      4  group by class, student
      5  order by class)
      6  select class, student, marks
      7  from (select class, student, marks, dense_rank() over(partition by class order by marks desc) rank
      8  from cte)
      9  where rank=1;

         CLASS S      MARKS
    ---------- - ----------
             1 A        160
             2 E        165
             3 B        165

    SQL>

Here the most important inner query is for cte table. Resulr set created for this one will be as below.

SQL> select class, student, sum(marks)
  2  from engineer
  3  group by class, student
  4  order by class;

     CLASS S SUM(MARKS)
---------- - ----------
         1 A        160
         1 B         80
         1 C         80
         2 D         60
         2 E        165
         2 F         80
         3 A        160
         3 B        165
         3 G         90

9 rows selected.
于 2015-09-23T19:07:12.233 に答える