我正在尝试理解以下查询,
SELECT s.LAST_NAME||', '||s.FIRST_NAME||' '||COALESCE(s.MIDDLE_NAME,' ') AS FULL_NAME,
s.LAST_NAME,
s.FIRST_NAME,
s.MIDDLE_NAME,
s.STUDENT_ID,
ssm.SCHOOL_ID,
ssm.SCHOOL_ID AS LIST_SCHOOL_ID,
ssm.GRADE_ID ,
sg1.BENCHMARK_ID,
sg1.GRADE_TITLE,
sg1.COMMENT AS COMMENT_TITLE,
ssm.STUDENT_ID,
sg1.MARKING_PERIOD_ID,
sg1.LONGER_COURSE_COMMENTS,
sp.SORT_ORDER,
sched.COURSE_PERIOD_ID
FROM STUDENTS s,
STUDENT_ENROLLMENT ssm ,
SCHEDULE sched
LEFT OUTER JOIN STUDENT_REPORT_CARD_BENCHMARKS sg1 ON (
sg1.STUDENT_ID=sched.STUDENT_ID
AND sched.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID
AND sg1.MARKING_PERIOD_ID IN ('0','442','445','450')
AND sg1.SYEAR=sched.SYEAR)
LEFT OUTER JOIN COURSE_PERIODS rc_cp ON (
rc_cp.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID
AND rc_cp.DOES_GRADES='Y')
LEFT OUTER JOIN SCHOOL_PERIODS sp ON (sp.PERIOD_ID=rc_cp.PERIOD_ID)
WHERE ssm.STUDENT_ID=s.STUDENT_ID
AND ssm.SCHOOL_ID='1'
AND ssm.SYEAR='2010'
AND ('22-APR-11' BETWEEN ssm.START_DATE AND ssm.END_DATE OR (ssm.END_DATE IS NULL))
AND (LOWER(s.LAST_NAME) LIKE 'la''porsha%' OR LOWER(s.FIRST_NAME) LIKE 'la''porsha%' )
AND sched.STUDENT_ID=ssm.STUDENT_ID AND sched.MARKING_PERIOD_ID IN ('0','444','446','447','445','448','450','443','449')
AND ('22-APR-11' BETWEEN sched.START_DATE AND sched.END_DATE OR (sched.END_DATE IS NULL AND '22-APR-11'>=sched.START_DATE))
ORDER BY s.LAST_NAME,s.FIRST_NAME
并修改它以返回正确的结果 - 仅返回一个不同的人。搜索任何特定人员时,会返回多个结果,因为schedule.course_period_id
返回了唯一值。由于course_period_id
字段上有多个左外连接,但是在不同的表中,我对于在何处修改查询感到困惑。
答案 0 :(得分:3)
我试图通过格式化您的查询并摆脱混合语法来帮助人们回答。不是一个答案,但评论太长了:
SELECT s.LAST_NAME || ', ' || s.FIRST_NAME || ' ' || COALESCE(s.MIDDLE_NAME,' ')
AS FULL_NAME,
s.LAST_NAME, s.FIRST_NAME, s.MIDDLE_NAME, s.STUDENT_ID,
ssm.SCHOOL_ID, ssm.SCHOOL_ID AS LIST_SCHOOL_ID, ssm.GRADE_ID ,
sg1.BENCHMARK_ID, sg1.GRADE_TITLE, sg1.COMMENT AS COMMENT_TITLE,
ssm.STUDENT_ID, sg1.MARKING_PERIOD_ID, sg1.LONGER_COURSE_COMMENTS,
sp.SORT_ORDER, sched.COURSE_PERIOD_ID
FROM STUDENTS s
INNER JOIN STUDENT_ENROLLMENT ssm
ON ssm.STUDENT_ID=s.STUDENT_ID -- moved from WHERE to here
INNER JOIN SCHEDULE sched
ON sched.STUDENT_ID=ssm.STUDENT_ID -- moved from WHERE to here
LEFT OUTER JOIN STUDENT_REPORT_CARD_BENCHMARKS sg1
ON ( sg1.STUDENT_ID=sched.STUDENT_ID
AND sched.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID
AND sg1.MARKING_PERIOD_ID IN ('0','442','445','450')
AND sg1.SYEAR=sched.SYEAR)
LEFT OUTER JOIN COURSE_PERIODS rc_cp
ON ( rc_cp.COURSE_PERIOD_ID=sg1.COURSE_PERIOD_ID
AND rc_cp.DOES_GRADES='Y')
LEFT OUTER JOIN SCHOOL_PERIODS sp
ON (sp.PERIOD_ID=rc_cp.PERIOD_ID)
WHERE ssm.SCHOOL_ID='1'
AND ssm.SYEAR='2010'
AND ('22-APR-11' BETWEEN ssm.START_DATE AND ssm.END_DATE
OR (ssm.END_DATE IS NULL))
AND ( LOWER(s.LAST_NAME) LIKE 'la''porsha%'
OR LOWER(s.FIRST_NAME) LIKE 'la''porsha%' )
AND sched.MARKING_PERIOD_ID
IN ('0','444','446','447','445','448','450','443','449')
AND ( '22-APR-11' BETWEEN sched.START_DATE AND sched.END_DATE
OR ( sched.END_DATE IS NULL
AND '22-APR-11' >= sched.START_DATE))
ORDER BY s.LAST_NAME, s.FIRST_NAME
希望它有所帮助。
答案 1 :(得分:2)
当然,如果加入的子表有多个同一个人的记录,那么你有多个记录。这是预期和正确的行为。
如果您只需要每人一条记录,那么您必须修改查询以告诉它您希望它选择多个子记录中的哪一条。但是,为什么你不想看到这个人的所有预定课程,而不是只有一个?
如果您必须使用group by,然后将聚合(如min或max)放在导致多个记录的字段上。但是,您仍然需要知道您是否只需要第一个期间记录或最后一个期间记录,或者您将如何决定您想要查看的人的六个记录?
答案 2 :(得分:0)
Look up group by
条款。