从3个表中获取数据时进行Db2查询

时间:2013-06-28 05:53:56

标签: sql database db2

我有以下表格布局

表a

Teacher Students
Mohan pankaj
Mohan sudeepa
Mohan sujitra
Mohan Sumit

表b(第1学期)

Pankaj Maths 50
Pankaj English 40
Pankaj Physics 60
Sudeepa Chemisty 40
Sujitra Physic 10
Sujitra English 40
Sujitra Hindi 70

表c(第2学期)

Pankaj Chemistry 50
Pankaj English 40
Pankaj Physics 60
Sudeepa Science 40
Sujitra Physic 10
Sujitra Maths 40
Sujitra Hindi 70

我希望输出为: -

hindi 140.00
maths 40.00
physics 10.00
science 40.00
chemistry 40.00
english 40.00
physics 10.00

我写了查询

select subject,sum(marks) from sample.b where
student in ( select student from sample.a where teacher='mohan')
group by subject

union all
select subject,sum(marks) from sample.c
where student in ( select student from sample.test where teacher='mohan')
group by subject

我的结果为

hindi 70.00
maths 40.00
physics 10.00
science 40.00
chemistry 40.00
english 40.00
hindi 70.00
physics 10.00

* 问题 * Same subjects marks are not adding up ..

2 个答案:

答案 0 :(得分:0)

更新您的查询可能如

SELECT s.subject, s1.marks sem1_marks, s2.marks sem2_marks
FROM
(
  SELECT b.subject 
    FROM b JOIN a 
      ON b.student = a.students
   WHERE a.teacher = 'Mohan'
   UNION 
  SELECT c.subject 
    FROM c JOIN a 
      ON c.student = a.students
   WHERE a.teacher = 'Mohan'
) s 
LEFT JOIN
(
  SELECT b.subject, SUM(b.marks) marks
    FROM b JOIN a 
      ON b.student = a.students
   WHERE a.teacher = 'Mohan'
   GROUP BY b.subject
) s1 ON s.subject = s1.subject 
LEFT JOIN
(
  SELECT c.subject, SUM(c.marks) marks
    FROM c JOIN a 
      ON c.student = a.students
   WHERE a.teacher = 'Mohan'
   GROUP BY c.subject
) s2 ON s.subject = s2.subject

输出:

|   SUBJECT | SEM1_MARKS | SEM2_MARKS |
---------------------------------------
| Chemistry |         40 |         50 |
|   English |         80 |         40 |
|     Hindi |         70 |         70 |
|     Maths |         50 |         40 |
|   Physics |         70 |         70 |
|   Science |     (null) |         40 |

这是 SQLFiddle 演示(同样是SQL Server,但应该在DB2中运行)。

原始问题的原始答案: 您需要先在内部选择中UNION ALL两个数据集(聚合与否),然后在外部选择中应用聚合SUM()GROUP BY

SELECT subject, SUM(marks) marks
FROM
(
  SELECT student, subject, marks 
    FROM b JOIN a 
      ON b.student = a.students
   WHERE a.teacher = 'Mohan'
   UNION ALL
  SELECT student, subject, marks 
    FROM c JOIN a 
      ON c.student = a.students
   WHERE a.teacher = 'Mohan'
) q
 GROUP BY subject

输出:

|   SUBJECT | MARKS |
---------------------
| Chemistry |    90 |
|   English |   120 |
|     Hindi |   140 |
|     Maths |    90 |
|   Physics |   140 |
|   Science |    40 |

这是 SQLFiddle 演示(SQL Server)。 但我相信它应该在DB2中运行相同。

答案 1 :(得分:0)

通过将成绩分成不同的列,这可以为您提供所要求的内容。

Select subject, sum(s1mark) sem1tot, sum(s2mark) sem2tot
From ( select subject, mark as s1mark, 0 as s2mark
         From b join a  on a.student=b.student and a.teacher='Mohan'
       UNION
       select subject, 0 as s1mark, mark as s2mark
         From b join a  on a.student=b.student and a.teacher='Mohan'
     )
group by subject
order by subject