2017-05-24 9 views
1

私は以下のように3つのテーブルがあります。クエリ列のヘッダーとして行の値を持つ3つのテーブルを結合

TABLE1 : tb_subject 
subject_id subject_name 
    1   English 
    2   Maths 
    3   Science 

Table2 : tb_student 
subject_id student_id 
    1   AA   
    1   BB 
    2   CC 
    3   DD 
    3   EE 

    Table3 : tb_student_score 
    student_id score conducted_month_number 
     AA   20   2 
     BB   30   3 
     CC   50   4 
     AA   80   4 
     DD   50   6 
     BB   10   2 
     EE   40   3 

結果は、このために選択クエリを作成する方法

conducted_month_number SUM(subject_id1) SUM(subject_id2) SUM(subject_id3) 
     1     0     0     0 
     2     30     0     0 
     3     30     0     40 
     4     80     50     0 
     5     0     0     0 
     6     0     0     60 
     7     0     0     0 
     8     0     0     0 
     9     0     0     0 
     10     0     0     0 
     11     0     0     0 
     12     0     0     0 

すべきですか?結果の出力のようにテーブルに保存されていない月の番号をすべて追加できますか?

+0

SQL Server 2005の – user2431727

+0

あなたはまだコードをお持ちですか?既知の(固定された)数の科目があるか、あるいはそれらが変わりますか? –

+0

4.月のsubject_id1は50ではありませんあなたはそれを確認できます –

答えて

3

あなたが個別に被験者ごとに合計するcase whenを使用することができるはずです。

SELECT conducted_month_number, 
     SUM(CASE b.subject_id WHEN 1 THEN a.score ELSE 0 END) AS English, 
     SUM(CASE b.subject_id WHEN 2 THEN a.score ELSE 0 END) AS Maths, 
     SUM(CASE b.subject_id WHEN 3 THEN a.score ELSE 0 END) AS Science 
FROM tb_student_score AS a 
    JOIN tb_student AS b ON b.student_id = a.student_id 
GROUP BY conducted_month_number 
ORDER BY conducted_month_number; 

しかし、これだけでは、あなたが存在しないconducted_month_numberの値の結果を持っていることを確認しません - これが問題になる場合毎月0点のダミー生徒を作ることができます。

編集:私は自分の答えを提出した同じ時期にポストされたいくつかのコメントに気付きました。tb_subjectテーブルの行の値に基づいて総和カラムの数を可変にしたい場合は、 SQLはそのタスクに適しています。ただし、簡単に戻ってクエリーを更新して、後で追加する可能性のある新しい科目を含めることができます。

3

union statement以降を使用してdummy valuesを1から12か月に追加した場合は、合計スコアを計算するためにgroup byとなりました。

はこれを試してください: -

Select conducted_month_number , 
sum(case when subject_id=1 then score else 0 end) as sum_subject_id1, 
sum(case when subject_id=2 then score else 0 end) as sum_subject_id2, 
sum(case when subject_id=3 then score else 0 end) as sum_subject_id3 
from 
(
Select a.conducted_month_number ,subject_id,score 
from 
tb_student_score a 
inner join 
tb_student b 
on a.student_id=b.student_id 
union 
select 1,' ',0 from tb_student_score 
union 
select 2,' ',0 from tb_student_score 
union 
select 3,' ',0 from tb_student_score 
union 
select 4,' ',0 from tb_student_score 
union 
select 5,' ',0 from tb_student_score 
union 
select 6,' ',0 from tb_student_score 
union 
select 7,' ',0 from tb_student_score 
union 
select 8,' ',0 from tb_student_score 
union 
select 9,' ',0 from tb_student_score 
union 
select 10,' ',0 from tb_student_score 
union 
select 11,' ',0 from tb_student_score 
union 
select 12,' ',0 from tb_student_score 
)a 
group by conducted_month_number 

My Output 

conducted_month_number sum_subject_id1 sum_subject_id2 sum_subject_id3 
       1    0    0    0 
       2    30    0    0 
       3    30    0    40 
       4    80    50   0 
       5    0    0    0 
       6    0    0    50 
       7    0    0    0 
       8    0    0    0 
       9    0    0    0 
       10    0    0    0 
       11    0    0    0 
       12    0    0    0 
関連する問題