Comment détecter les lignes d'une table faisant doublon sur une liste de colonne (et non pas sur l'ensemble de la ligne).
Exemple :
SQL> CREATE TABLE toto (
2 pays VARCHAR2(3),
3 ville VARCHAR2(10),
4 note NUMBER
5 );
Table created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',1);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',2);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',3);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',3);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','LYON',4);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','LYON',5);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','MARSEILLE',5);
1 row created.
SQL> INSERT INTO toto VALUES ('USA','NEW YORK',5);
1 row created.
SQL> COMMIT;
Commit complete.
2 pays VARCHAR2(3),
3 ville VARCHAR2(10),
4 note NUMBER
5 );
Table created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',1);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',2);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',3);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','PARIS',3);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','LYON',4);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','LYON',5);
1 row created.
SQL> INSERT INTO toto VALUES ('FRA','MARSEILLE',5);
1 row created.
SQL> INSERT INTO toto VALUES ('USA','NEW YORK',5);
1 row created.
SQL> COMMIT;
Commit complete.
Pour le cas classique :
SQL> SELECT t1.pays, t1.ville, t1.note, count(*)
2 FROM toto t1
3 GROUP BY t1.pays, t1.ville, t1.note
4 HAVING COUNT(*) > 1
5 ;
PAY VILLE NOTE COUNT(*)
--- ---------- ---------- ----------
FRA PARIS 3 2
Et la liste :
2 FROM toto t1
3 GROUP BY t1.pays, t1.ville, t1.note
4 HAVING COUNT(*) > 1
5 ;
PAY VILLE NOTE COUNT(*)
--- ---------- ---------- ----------
FRA PARIS 3 2
Et la liste :
SQL> SELECT t1.* FROM toto t1 WHERE t1.rowid >= (
2 SELECT MIN(t2.rowid) FROM toto t2
3 WHERE t1.pays=t2.pays
4 AND t1.ville=t2.ville
5 AND t1.note=t2.note
6 HAVING COUNT(*) > 1
7 )
8 ;
PAY VILLE NOTE
--- ---------- ----------
FRA PARIS 3
FRA PARIS 3
Et par extension, pour le cas des doublons partiels :
SQL> SELECT t1.pays, t1.ville, COUNT(*)
2 FROM toto t1
3 GROUP BY t1.pays, t1.ville
4 HAVING COUNT(*) > 1
5 ;
PAY VILLE COUNT(*)
--- ---------- ----------
FRA LYON 2
FRA PARIS 4
Et la liste :
SQL> SELECT t1.* FROM toto t1 WHERE t1.rowid >= (
2 SELECT MIN(t2.rowid) FROM toto t2
3 WHERE t1.pays=t2.pays
4 AND t1.ville=t2.ville
5 HAVING COUNT(*) > 1
6 )
7 ;
PAY VILLE NOTE
--- ---------- ----------
FRA PARIS 1
FRA PARIS 2
FRA PARIS 3
FRA PARIS 3
FRA LYON 4
FRA LYON 5
2 FROM toto t1
3 GROUP BY t1.pays, t1.ville
4 HAVING COUNT(*) > 1
5 ;
PAY VILLE COUNT(*)
--- ---------- ----------
FRA LYON 2
FRA PARIS 4
Et la liste :
SQL> SELECT t1.* FROM toto t1 WHERE t1.rowid >= (
2 SELECT MIN(t2.rowid) FROM toto t2
3 WHERE t1.pays=t2.pays
4 AND t1.ville=t2.ville
5 HAVING COUNT(*) > 1
6 )
7 ;
PAY VILLE NOTE
--- ---------- ----------
FRA PARIS 1
FRA PARIS 2
FRA PARIS 3
FRA PARIS 3
FRA LYON 4
FRA LYON 5
pas de commentaire !
RépondreSupprimerLe COUNT(*) > 1 est-il utile partout ?
RépondreSupprimerMettre aussi les exemples de delete...
Non, en effet le COUNT n'est pas obligatoire partout. Normalement il aurait fallu utiliser le HAVING seulement lorsqu'il y a un GROUP BY.
Supprimer