【问题标题】:sum of two different columns (with additions) of different tables and multiple table joining in OracleOracle中不同表的两个不同列(加法)的总和以及多个表连接
【发布时间】:2012-08-07 10:11:06
【问题描述】:

我有三张桌子。

学校:学校代码(PK)、年份、学校名称。
入学:学校代码、年份、种姓、c1、c2、c3、c4、c5、c6、c7、c8
CLASS:学校代码、年份、classid、房间

现在,我想查找 1 到 4 班入学的学校列表和 1-4 班使用的教室数量(CLASSID 定义为:7 表示 1 和 2 班,8 表示 3 和 4 班,9 5&6级,7&8级10;种姓被定义为1代表普通,2代表sc,3代表st,4代表其他)。

我使用了以下查询:

select m.schoolcode, m.schoolname, sum(e.c1+e.c2+e.c3+e.c4), sum(c.rooms) 
from dise2k_enrolment09 e, 
     dise2k_master m ,
     dise2k_clsbycondition c 
where m.schoolcode=e.schoolcode and
      m.schoolcode=c.schoolcode and 
      e.year='2011-12' and 
      m.year='2011-12' and 
      c.year='2011-12' and 
      c.classid in(7,8) and 
      e.caste in(1,2,3,4) 
group by m.schoolcode, m.schoolname 

但是显示的结果不正确。入学率比实际高得多,在教室的情况下也是如此。

【问题讨论】:

标签: sql oracle11g sum


【解决方案1】:

好的,试试这个看看你的问题是不是因为在join中重复记录引起的:

select m.schoolcode, m.schoolname, e_sum, c_sum 
  from dise2k_master m
 inner join
 (
    select schoolcode,
           sum(c1 + c2 + c3 + c4) e_sum
      from dise2k_enrolment09
     where year='2011-12'
       and caste in(1,2,3,4) 
     group by schoolcode
 ) e
    on m.schoolcode=e.schoolcode
 inner join
 (
    select schoolcode,
           sum(rooms) c_sum
      from dise2k_clsbycondition
     where year='2011-12'
       and classid in(7,8)
     group by schoolcode
 ) c
    on m.schoolcode=c.schoolcode
 where m.year='2011-12'

【讨论】:

    猜你喜欢
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    相关资源
    最近更新 更多