剧透警告:我对 Oracle 还很陌生。
我有四个表:注册、课程/部分、标准和成绩。
我们正在运行荣誉榜。我对前三个表进行了查询,这些表添加了满足荣誉榜要求所需的各种约束。然后我们看成绩表。 如果他们有有效的注册,在有效的课程中,达到有效的标准,然后计算他们的分数。如果他们的得分数量达到阈值,那么他们将获得荣誉。
这段代码没有优化,我敢肯定,它可能会以更好/更紧凑的方式完成——但是,它每年只运行几次,所以我愿意权衡优化以增加人类可读性,使我可以继续学习基础知识。到目前为止,我有:
WITH validCC (SELECT CC.ID AS CCID,
CC.STUDENTID AS STUDENTID,
CC.SECTIONID AS SECTIONID,
CC.TERMID AS TERMID,
STUDENTS.DCID AS STUDENTSDCID
FROM CC
INNER JOIN STUDENTS ON CC.STUDENTID = STUDENTS.ID
WHERE TERMID in (2700,2701)
AND CC.SCHOOLID = 406;
), --end validCC
validCrsSect (SELECT SECTIONS.ID AS SECTIONID,
SECTIONS.DCID AS SECTIONSDCID,
SECTIONS.EXCLUDEFROMHONORROLL AS SECTHR,
COURSES.COURSE_NUMBER AS COURSE_NUMBER,
COURSES.COURSE_NAME AS COURSE_NAME,
COURSES.EXCLUDEFROMHONORROLL AS CRSHR
FROM SECTIONS
INNER JOIN COURSES ON SECTIONS.COURSE_NUMBER = COURSES.COURSE_NUMBER AND SECTIONS.SCHOOLID = COURSES.SCHOOLID
WHERE SECTIONS.TERMID IN (2700,2701)
AND SECTIONS.SCHOOLID = 406
AND SECTIONS.EXCLUDEFROMHONORROLL = 0
AND COURSES.EXCLUDEFROMHONORROLL = 0
), --end validCrsSect
validStandard (SELECT STANDARDID,
IDENTIFIER,
TRANSIENTCOURSELIST
FROM STANDARD
WHERE isActive = 1
AND YEARID = 27
AND ( instr (STANDARD.identifier, 'MHS.TS', 1 ,1) > 0 --Is a valid standard for this criteria: MHS TS
or STANDARD.identifier = 'MHTC.TS.2' --or MHTC TS
or STANDARD.identifier = 'MHTC.TS.4' )
), --end validStandard
--sgsWithChecks (
SELECT sgs.STANDARDGRADESECTIONID AS SGSID,
sgs.STUDENTSDCID as STUDENTSDCID,
sgs.STANDARDID AS STANDARDID,
sgs.STORECODE AS STORECODE,
sgs.SECTIONSDCID AS SECTIONSDCID,
sgs.YEARID AS YEARID,
sgs.STANDARDGRADE AS STANDARDGRADE,
(select count(CCID) from validCC INNER JOIN STANDARDGRADESECTION sgs ON sgs.STUDENTSDCID = validCC.STUDENTSDCID and sgs.SECTIONSDCID = validCC.SECTIONID) as CC_OK,
(select count(SECTIONID) from validCrsSection INNER JOIN STANDARDGRADESECTION sgs ON sgs.SECTIONSDCID = validCrsSect.SECTIONSDCID) AS CRS_OK,
(select count(STANDARDID) from validStandard INNER JOIN STANDARDGRADESECTION sgs ON sgs.STANDARDID = validStandard.STANDARDID) AS STD_OK
FROM STANDARDGRADESECTION sgs
将“OK”列放在 vGrades 表中的目的是因为最终的 SELECT(不包括在内)通过并计算通过检查过滤的某些分数的实例。
令人沮丧的是,students 表和sections 表中都有两个ID(而且不是同一个数据)。所以当我去链接所有东西时,有些表使用ID作为FK,其他表使用DCID作为FK;我必须拉一个额外的表来进行转换。我猜这样让连接更有趣。
每个单独的查询都独立工作,但我无法让最终的 select count() 工作以提取他们的数据。我尝试将初始查询嵌入为子查询,但我无法将 studentid 传递给它们,它会为每个学生运行该查询,而不是在开始时运行一次。
我目前的错误是:
Error starting at line : 13 in command -
SECTIONS.DCID AS SECTIONSDCID,
Error report -
Unknown Command
但是在它说未知表并引用连接语句的最后一行之前。所有表名都有效。想法?