我有三张桌子。
学校:学校代码(PK),年份,学校名称。
注册:学校代码,年份,种姓,c1,c2,c3,c4,c5,c6,c7,c8
班级:学校代码,年份,类别,房间
现在,我想要找到1-4班注册的学校列表和1-4班使用的教室数量(CLASSID的定义为:1-2班7所,3-4班8所,5-6班9所,7-8班10所;种姓定义为一般1所,sc 2所,st 3所,其他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 但是结果显示是不正确的。注册人数远远高于实际,教室的情况也是如此。
发布于 2012-08-07 18:41:24
好的,试试这个,看看你的问题是不是因为在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'https://stackoverflow.com/questions/11843677
复制相似问题