Sql 用于联接三个行值为列标题的表的查询
我有三张表,如下所示Sql 用于联接三个行值为列标题的表的查询,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我有三张表,如下所示 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
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
如何为此编写select查询?是否可以将未存储在表中的所有月数添加到结果输出中 您应该能够在对每个主题分别求和时使用
大小写:
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\u month\u number
值的结果-如果这是一个问题,您可以简单地创建一个每月分数为0的虚拟学生
编辑:在我提交答案的同时,我注意到一些评论——如果您希望总和列的数量根据tb_主题表中的行值而变化,您将不会发现SQL
的关系模型非常适合该任务。但是,您可以轻松返回并更新查询,以包括以后可能添加的任何新主题。您应该能够在
对每个主题分别求和时使用大小写:
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\u month\u number
值的结果-如果这是一个问题,您可以简单地创建一个每月分数为0的虚拟学生
编辑:在我提交答案的同时,我注意到一些评论——如果您希望总和列的数量根据tb_主题表中的行值而变化,您将不会发现SQL
的关系模型非常适合该任务。但是,您可以很容易地返回并更新查询,以包括以后可能添加的任何新主题。使用联合声明添加了1到12个月的伪值
,然后对其进行了分组
,以计算总分
试试这个:-
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
使用联合声明
添加1至12个月的虚拟值
,然后对其进行分组
,以计算总分
试试这个:-
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
sql server 2005您有代码吗?是否有已知(固定)数量的受试者,或受试者数量是可变的?受试者数量为4。月份不到50你能检查一下吗?将来可能会有机会在“tb_subject”表中添加新的subject_id。6月份的subject_id 3是50你能检查一下吗?sql server 2005你有代码吗?是否有已知(固定)数量的受试者,或受试者数量是可变的?受试者数量为4。月份不是50,你能检查一下吗?将来可能会有机会在“tb_subject”表中添加新的主题ID。6月的主题ID是50,你能检查一下吗?