gpt4 book ai didi

sql - 不同表的两个不同列(添加)的总和以及 Oracle 中的多表连接

转载 作者:行者123 更新时间:2023-12-04 14:10:23 27 4
gpt4 key购买 nike

我有三个表。

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

现在,我想找到 class-1 到 4 入学的学校列表以及 class 1-4 使用的教室数量(CLASSID 定义为:class-1&2 为 7,class-3&4 为 8,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

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

最佳答案

好的,试试这个看看你的问题是否来自于连接中的重复记录:

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'

关于sql - 不同表的两个不同列(添加)的总和以及 Oracle 中的多表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com