2011-04-25 59 views
1

結果太多,我想雙方都知道下面的查詢,從查詢

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字段上有幾個左外部連接,但跨越不同的表,所以我很困惑在哪裏修改查詢。

+4

好主人,至少嘗試並格式化代碼。沒有人有任何理解目前狀況正在發生的事情。 – YXD 2011-04-25 17:43:20

+1

這是一個非常混亂的查詢 – Lamak 2011-04-25 17:44:32

+3

我會解決的第一件事是在相同的查詢中使用隱​​式和顯式連接。這可能會導致錯誤,不應該完成。事實上,沒有任何藉口使用隱含的語法。 – HLGEM 2011-04-25 17:55:29

回答

2

那麼當然你有多個記錄,如果子表加入同一個人有多個記錄。這是預期的和正確的行爲。

如果您只希望每個人有一條記錄,那麼您必須修改查詢以告訴它您希望它選擇多個子記錄中的哪一條。但爲什麼你不想看到這個人的所有預定課程,而不是隻有一個?

如果你必須你coudl使用group by,然後把聚合(如最小或最大)在導致你多個記錄的字段。但是,您仍然需要知道您是否只需要第一個期間記錄或最後一個期間記錄,或者您如何決定要查看哪個人的六個記錄?

+0

謝謝......事實證明,這是正確的查詢,但它被放在錯誤的地方:(將它移到稍後的點得到了我需要的結果。 – earachefl 2011-07-20 14:59:14

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 

希望它有幫助。