我有以下sql将显示测试分数值:
SELECT s.dcid, s.lastfirst, s.student_number, s.grade_level, s.schoolid,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t on ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 451
AND ts.id = 857
AND st.termid LIKE '24%'
AND ROWNUM = 1) as FALL,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t on ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 501
AND ts.id = 1001
AND st.termid LIKE '24%'
AND ROWNUM = 1) as WINTER,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t on ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 551
AND ts.id = 1051
AND st.termid LIKE '24%'
AND ROWNUM = 1) as SPRING
FROM students s
WHERE s.grade_level = 1
ORDER BY s.lastfirst
如上所述,这将返回所有学生以及他们在秋季,冬季和春季测试期间的分数。我现在需要做的是将学生名单限制在秋季和冬季期间他们的分数低于特定基准的学生名单。我知道我可以通过添加WHERE子句来实现这一点:
WHERE s.grade_level = 1
AND (SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t on ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 451
AND ts.id = 857
AND st.termid LIKE '24%'
AND ROWNUM = 1) < 28
AND (SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t on ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 501
AND ts.id = 1001
AND st.termid LIKE '24%'
AND ROWNUM = 1) < 37
我的问题是,这是创建选择标准的最有效方法吗?有没有办法可以回顾所选分数的别名,FALL和WINTER?当我用
测试它时,它不起作用WHERE s.grade_level = 1
AND FALL < 28
AND WINTER < 37
答案 0 :(得分:1)
您只需将Select嵌套在派生表(也称为内联视图)中,然后就可以使用WHERE中的别名列:
SELECT *
FROM
(
SELECT s.dcid, s.lastfirst, s.student_number, s.grade_level, s.schoolid,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t ON ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 451
AND ts.id = 857
AND st.termid LIKE '24%'
AND ROWNUM = 1) AS FALL,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t ON ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 501
AND ts.id = 1001
AND st.termid LIKE '24%'
AND ROWNUM = 1) AS WINTER,
(SELECT stc.numscore
FROM studenttestscore stc
JOIN testscore ts ON stc.testscoreid = ts.id
JOIN test t ON ts.testid = t.id
JOIN studenttest st ON stc.studenttestid = st.id
WHERE stc.studentid = s.id
AND t.id = 551
AND ts.id = 1051
AND st.termid LIKE '24%'
AND ROWNUM = 1) AS SPRING
FROM students s
WHERE s.grade_level = 1
) dt
WHERE FALL < 28
AND WINTER < 37
答案 1 :(得分:1)
使用公用表表达式,您可以引用主查询的where子句中CTE select语句的字段。他们还清理了一点结构,重用限制了你需要复制+粘贴公共谓词的次数(例如 - 和st.termid LIKE&#39; 24%&#39;)
WITH TermTestData AS (
SELECT ts.testid
, ts.id
, stc.numscore
, stc.studentid
FROM studenttestscore AS stc
JOIN testscore AS ts
ON ts.id = stc.testscoreid
JOIN studenttest AS st
ON st.id = stc.testscoreid
WHERE st.termid LIKE '24%'
), SemesterScores AS (
SELECT s.dcid, s.lastfirst, s.student_number, s.grade_level, s.schoolid
, (SELECT td.numscore
FROM TermTestData AS td
WHERE td.studentid = s.id
AND td.id = 451
AND td.id = 857
AND ROWNUM = 1) as FALL
, (SELECT td.numscore
FROM TermTestData AS td
WHERE td.studentid = s.id
AND td.id = 501
AND td.id = 1001
AND ROWNUM = 1) as WINTER
, (SELECT td.numscore
FROM TermTestData AS td
WHERE td.studentid = s.id
AND td.id = 551
AND td.id = 1051
AND ROWNUM = 1) as SPRING
FROM students AS s
)
SELECT *
FROM SemesterScores
WHERE FALL < 28
AND WINTER < 37
备注:如果您使用的是Oracle 11g,则可以转动数据以避免使用单值字段的select语句
答案 2 :(得分:-1)
如果这是MYSQL,你可以试试它:
WHERE s.grade_level = 1
HAVING FALL < 28 AND WINTER < 37
我相信“HAVING”会再次通过,并会选择别名。