2013-10-04 98 views
0

有人能指出我在正確的方向,以消除學生證字段中的重複?如何消除sql中的重複項?

SQL> select DISTINCT(student_class.student_id) as student_Num,student_class.class_id, 
    2 event.event_id, event.event_name 
    3 from student_class, event 
    4 where student_class.class_id = '10' 
    5 and event.class_id = '10'; 

STUDENT_NUM CLASS_ID EVENT_ID EVENT_NAME 
----------- ---------- ---------- -------------------------------------------------- 
     12   10   2 Flag FOOtball Game 
     12   10   5 PICKUP SOCCER GAME 
      9   10   5 PICKUP SOCCER GAME 
     16   10   5 PICKUP SOCCER GAME 
      6   10   2 Flag FOOtball Game 
     18   10   5 PICKUP SOCCER GAME 
      4   10   5 PICKUP SOCCER GAME 
      4   10   2 Flag FOOtball Game 
     16   10   2 Flag FOOtball Game 
     20   10   2 Flag FOOtball Game 
      3   10   5 PICKUP SOCCER GAME 
      2   10   5 PICKUP SOCCER GAME 
      3   10   2 Flag FOOtball Game 
      8   10   2 Flag FOOtball Game 
      9   10   2 Flag FOOtball Game 
      2   10   2 Flag FOOtball Game 
      6   10   5 PICKUP SOCCER GAME 
     20   10   5 PICKUP SOCCER GAME 
     18   10   2 Flag FOOtball Game 
      8   10   5 PICKUP SOCCER GAME 
+2

有重複,因爲有多個相匹配的student_id值。預期的結果是什麼? –

+1

當你說'消除重複'時,你真的是指'抑制輸出中的重複值'嗎?因爲如果我們正確地使用術語,這裏沒有重複。這些行在事件列中具有不同的值。 – developerwjk

+0

截然不同適用於整行。 'distinct(col1)'不是列上的函數。 –

回答

0

TRY SQL>

SELECT DISTINCT event.event_id,event.event_name,COUNT(student_class.student_id) AS 'Total Students' 
    FROM student_class, event 
    WHERE student_class.class_id = '10' 
    AND event.class_id = '10' 
    GROUP BY event.event_id,event.event_name,student_class.student_id 

這應該給你事件ID和名稱,與去它的學生的數量