消除重复SQL?

时间:2013-10-04 03:38:39

标签: sql database oracle11g

如何消除重复? 它必须是10行,显示每个学生ID(1x)。 香港专业教育学院尝试了这两种不同的方(见下文) (常见字段是class_id)

SQL> select distinct(student_id),event.class_id, event.event_id, event.event_name
  2  from student_class
  3  inner join event on event.class_id = student_class.class_id
  4  where student_class.class_id = '10'
  5  order by student_id desc;

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

SQL> select distinct(student_id), student_class.class_id,
     event.event_id, event.event_date_time
     from student_class
     inner join event on event.class_id = student_class.class_id
     order by student_id desc;

STUDENT_ID   CLASS_ID   EVENT_ID EVENT_DAT
---------- ---------- ---------- ---------
        22          9          1 09-NOV-99
        21          2          6 20-NOV-99
        21          9          1 09-NOV-99
        20          9          1 09-NOV-99
        20         10          2 19-JAN-99
        20         10          5 09-JUL-99
        18          9          1 09-NOV-99
        18         10          2 19-JAN-99
        18         10          5 09-JUL-99
        17          9          1 09-NOV-99
        16          2          6 20-NOV-99
        16          9          1 09-NOV-99
        16         10          2 19-JAN-99
        16         10          5 09-JUL-99
        15          9          1 09-NOV-99
        14          9          1 09-NOV-99
        13          9          1 09-NOV-99
        12          2          6 20-NOV-99
        12          9          1 09-NOV-99
        12         10          2 19-JAN-99
        12         10          5 09-JUL-99
        11          2          6 20-NOV-99
        11          9          1 09-NOV-99
         9         10          2 19-JAN-99
         9         10          5 09-JUL-99
         8          2          6 20-NOV-99
         8         10          2 19-JAN-99
         8         10          5 09-JUL-99
         7          2          6 20-NOV-99
         6         10          2 19-JAN-99
         6         10          5 09-JUL-99
         4         10          2 19-JAN-99
         4         10          5 09-JUL-99
         3         10          2 19-JAN-99
         3         10          5 09-JUL-99
         2          2          6 20-NOV-99
         2          9          1 09-NOV-99
         2         10          2 19-JAN-99
         2         10          5 09-JUL-99

1 个答案:

答案 0 :(得分:0)

您可以通过以下方法

来完成此操作
    create table table_name2 as select distinct * from table_name1;
drop table table_name1;
rename table_name2 to table_name1;

或通过此查询

DELETE FROM table_name A WHERE ROWID > (SELECT min(rowid) FROM table_name B WHERE      A.key_values = B.key_values);