0

我有三张桌子。

  1. SCHOOL:学校代码(PK),年份,学校名称。
  2. ENROLMENT:学校代码、年份、班级名称、注册
  3. CLASS:学校代码,年份,classid,房间

现在,我想查找在班级名称中注册的学校列表 - 1 到 4 以及班级 1-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 
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 classid in (1,2,3,4) 
    and e.classname in (1,2,3,4) 
group by 
    m.schoolcode, m.schoolname 

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

4

2 回答 2

1

尝试这个:

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(1,2,3,4) 
and e.classname = c.classid
group by m.schoolcode, m.schoolname 

你拥有它的方式:and e.classname in(1,2,3,4)就像OR在你的 where 子句中有一个运算符。

(c.classid=1 or c.classid=2 or c.classid=3 or c.classid=4) 
and 
(e.classname=1 or e.classname=2 or e.classname=3 or e.classname=4)

所以,c.classid 可以是“1”,e.classname 可以是“2”,这是错误的

更新 我仍然认为问题是你没有c.classid连接e.classname

试试这样:

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(1,2,3,4) 
and c.classid = decode(e.classname,1,7,2,7,3,8,4,8,5,9,6,9,7,10,8,10)
group by m.schoolcode, m.schoolname 
于 2012-08-06T12:57:28.147 回答
0

尝试这个:

 select m.schoolcode, m.schoolname, sum(e.c1+e.c2+e.c3+e.c4), sum(c.rooms) 
from dise2k_enrolment09 e join dise2k_master m
on  m.schoolcode=e.schoolcode 
join dise2k_clsbycondition c
on m.schoolcode=c.schoolcode
where  e.year='2011-12' and m.year='2011-12' and c.year='2011-12' 
and classid in(1,2,3,4) 
and e.classname in(1,2,3,4) 
group by m.schoolcode, m.schoolname
于 2012-08-06T12:44:08.197 回答