-1

学校のデータベースがあり、すべての学生のレポートを表示する必要があり、すべての学生が複数の科目と学位を持っています

SELECT grades.student_id,   
     grades.class_id,   
     grades.course_id,   
     grades.general_total
     grades.activity
FROM grades 
WHERE  grades.class_id = 17 

私の出力はそれが好き

student_id      class_id    course_id   general_total     activity  ....
=================================================================================
330             17              1               15         55
330             17              3               22         44
331             17              1               11         13
331             17              3               12         14
=================================================================================

次のような出力が必要です

--------------------------------------------------------------------
student_id  class_id    general_total for course 1       general_total for course 3            activity for course1          activity for course3     ....
=================================================================================================================================================
330             17              15                               22                                   55                           44
331             17              11                               12                                   13                           14
=================================================================================================================================================

つまり、すべての学生に対して、すべてのコースですべての学位を示す必要があります

4

2 に答える 2

1

これを試して:

SELECT student_id, class_id
,SUM(CASE WHEN course_id = 1 
          THEN general_total ELSE 0 END) AS general_total_Course1
,SUM(CASE WHEN course_id = 3 
          THEN general_total ELSE 0 END) AS general_total_Course3
,SUM(CASE WHEN course_id = 1 
          THEN activity ELSE 0 END) AS Activity_Course1
,SUM(CASE WHEN course_id = 3 
          THEN activity ELSE 0 END) AS Activity_Course3
FROM grades
GROUP BY student_id, class_id

このSQLFiddleを参照してください

の値がわからない場合は、次のcourse_id動的クエリを使用します。

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when `course_id` = ''',
      `course_id`,
      ''' then general_total END) AS `general_total_course_',
      `course_id`, '`'
    )
  ) INTO @sql
FROM grades;

SET @sql1 = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when `course_id` = ''',
      `course_id`,
      ''' then activity END) AS `activity_Course_',
      `course_id`, '`'
    )
  ) INTO @sql1
FROM grades;

SET @sql = CONCAT('SELECT student_id, class_id, ', @sql,',', @sql1, '
                  FROM grades 
                   GROUP BY student_id, class_id 
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

このSQLFiddleを参照してください

出力:

╔════════════╦══════════╦════════════════════════╦════════════════════════╦═══════════════════╦═══════════════════╗
║ STUDENT_ID ║ CLASS_ID ║ GENERAL_TOTAL_COURSE_1 ║ GENERAL_TOTAL_COURSE_3 ║ ACTIVITY_COURSE_1 ║ ACTIVITY_COURSE_3 ║
╠════════════╬══════════╬════════════════════════╬════════════════════════╬═══════════════════╬═══════════════════╣
║        330 ║       17 ║                     15 ║                     22 ║                55 ║                44 ║
║        331 ║       17 ║                     11 ║                     12 ║                13 ║                14 ║
╚════════════╩══════════╩════════════════════════╩════════════════════════╩═══════════════════╩═══════════════════╝
于 2013-05-18T09:12:03.163 に答える