使用where语句中的子查询进行查询

时间:2017-01-19 08:47:16

标签: sql subquery

我在where语句中有子查询的查询,但是我需要改进它,但我不确定最好的方法。

基本上我在这个年级课程中获得了最后年的成绩。以下代码适用于类代码' 10DRADRA1'但是还有更多的类,我希望查询能够遍历2017学期的所有类。我需要将类代码作为输出字段之一包含在内。因此,查询将从第一个类开始并为该类提供所有结果,然后对子查询中的下一个类执行相同操作。我不确定如何将classcode包含在上一年所有结果旁边的select语句中。

请注意,学生今年可能会参加一些课程,因此可能会重复上一年的成绩。他们今年每节课都要包括学生姓名和上一年的成绩。我硬编码了#10; 10DRADRA1'为了让事情变得有效,如果我删除它,它只为学校里的所有学生提供一次,而我希望他们每次上课一次,并且每个班级都有相同的结果。

同样不是vStudentReportsSemesterResults是一个包含所有结果的视图,StudentClasses是一个包含学生正在进行的类代码的表,SubjectClasses是一个包含学校所有课程代码的表

是否有人能够就最佳方式提出建议?这是我目前的代码。

SELECT      vStudentReportsSemesterResults.StudentID, 
            vStudentReportsSemesterResults.AssessResultsResult, 
            vStudentReportsSemesterResults.AssessAreaHdgAbbrev2, 
            vStudentReportsSemesterResults.FileSemester, 
            vStudentReportsSemesterResults.FileYear,
            vStudentReportsSemesterResults.ClassLearningAreaCode, 
            vStudentReportsSemesterResults.AssessmentCode, 
            vStudentReportsSemesterResults.StudentNameInternal

FROM        vStudentReportsSemesterResults 

WHERE      vStudentReportsSemesterResults.StudentID in 
            (
            select StudentClasses.ID from StudentClasses
            where StudentClasses.filesemester = 1 and 
            StudentClasses.fileyear = 2017 and 
            StudentClasses.classcode in 
                (
                select SubjectClasses.ClassCode from SubjectClasses
                where SubjectClasses.FileYear = 2017 and 
                SubjectClasses.FileSemester = 1 and 
                SubjectClasses.FileType = 'A' and 
                SubjectClasses.ClassCampus = 'S' and 
                SubjectClasses.ClassCode like '10DRADRA1'
                )
            )
            and (vStudentReportsSemesterResults.ClassLearningAreaCode = 'ENG'     OR
             vStudentReportsSemesterResults.ClassLearningAreaCode = 'MAT' OR
             vStudentReportsSemesterResults.ClassLearningAreaCode = 'SCI') AND 
             (vStudentReportsSemesterResults.AssessAreaHdgAbbrev2 = 'Grade' OR 
             vStudentReportsSemesterResults.AssessAreaHdgAbbrev2 = 'Level') AND 
             (vStudentReportsSemesterResults.AssessResultsResult <> '') AND 
             (vStudentReportsSemesterResults.FileYear = 2016) AND 
             (vStudentReportsSemesterResults.FileSemester = 4)

2 个答案:

答案 0 :(得分:1)

将StudentClasses加入vStudentReportsSemesterResults:

SELECT      
  sc.ClassCode,
  srsr.StudentID, 
  srsr.AssessResultsResult, 
  srsr.AssessAreaHdgAbbrev2, 
  srsr.FileSemester, 
  srsr.FileYear,
  srsr.ClassLearningAreaCode, 
  srsr.AssessmentCode, 
  srsr.StudentNameInternal
FROM vStudentReportsSemesterResults srsr
JOIN StudentClasses sc 
  ON  sc.ID = srsr.StudentID
  AND sc.FileSemester = 1
  AND sc.FileYear = srsr.FileYear + 1
  AND EXISTS
  (
    select *
    from SubjectClasses subc
    where subc.FileYear = sc.FileYear 
      and subc.ClassCode = sc.ClassCode
      and subc.FileSemester = sc.FileSemester  
      and subc.FileType = 'A'  
      and subc.ClassCampus = 'S' 
  )
WHERE srsr.ClassLearningAreaCode in ('ENG', 'MAT', 'SCI') 
  AND srsr.AssessAreaHdgAbbrev2 in ('Grade', 'Level')
  AND srsr.AssessResultsResult <> ''
  AND srsr.FileYear = 2016
  AND srsr.FileSemester = 4;

答案 1 :(得分:0)

这更具可读性:

SELECT      
    ClassCode,
    StudentID, 
    AssessResultsResult, 
    AssessAreaHdgAbbrev2, 
    ClassLearningAreaCode, 
    AssessmentCode, 
    StudentNameInternal,
    lr.FileYear,
    lr.FileSemester, 
    tr.FileYear, 
    tr.FileSemester, 
    tr.FileType, 
    tr.ClassCampus 
FROM        
    vStudentReportsSemesterResults lr
inner join (
        select ID, sc.ClassCode, sc.FileYear, sc.filesemester, sbjc.FileType, sbjc.ClassCampus
        from StudentClasses sc
        INNER JOIN SubjectClasses sbjc ON SC.classcode = sbjc.ClassCode and sc.FileSemester=sbjc.FileSemester and sc.FileYear=sbjc.FileYear
    ) tr on lr.StudentID = tr.ID 
    and lr.FileYear = tr.FileYear - 1
WHERE      
    ClassLearningAreaCode IN ('ENG', 'MAT', 'SCI') 
    AND AssessAreaHdgAbbrev2 IN ('Grade', 'Level') 
    AND (AssessResultsResult <> '') 
    AND lr.FileYear = 2016
    AND lr.FileSemester = 4
    and tr.FileSemester = 1 
    and tr.FileType = 'A' 
    and tr.ClassCampus = 'S' 
ORDER BY 2,1,5

SELECT部分,我添加了一些字段以便更好地理解结果 在WHERE子句中,您可以控制如何过滤比较。

在连接和过滤列上放置一些索引,查询将快速运行