| 1 | == Table franchissabilite == |
| 2 | == Build a view allowing to know the score of the different dams == |
| 3 | in #46 [[BR]] |
| 4 | |
| 5 | -- 1/ CONTRAINTE, ne marche pas |
| 6 | -- Il ne peut y avoir qu'une seule note pour un barrage et par espèce sinon |
| 7 | {{{ |
| 8 | ALTER TABLE geobs2010.franchissabilite_piscicole add constraint uk_franchissabilite_piscicole UNIQUE(fra_ref_id,fra_esp_id); |
| 9 | }}} |
| 10 | |
| 11 | -- 2/ recherche des valeurs dupliquées |
| 12 | {{{ |
| 13 | SELECT * FROM ( |
| 14 | |
| 15 | SELECT count(*) as nombre, fra_esp_id,fra_ref_id |
| 16 | from geobs2010.franchissabilite_piscicole |
| 17 | where fra_note is not null |
| 18 | group by fra_ref_id,fra_esp_id) as sub |
| 19 | where nombre>1; -- 63 doublons |
| 20 | }}} |
| 21 | |
| 22 | -- 3/Nombre de valeurs distinctes |
| 23 | {{{ |
| 24 | SELECT count(*) from ( |
| 25 | SELECT DISTINCT ON (fra_esp_id,fra_ref_id)* |
| 26 | from geobs.franchissabilite_piscicole |
| 27 | |
| 28 | where fra_note is not null) as sub; -- 23461 |
| 29 | }}} |
| 30 | |
| 31 | -- 4/ nombre de valeurs totales |
| 32 | {{{ |
| 33 | select count (*) from geobs2010.franchissabilite_piscicole -- 24964 |
| 34 | }}} |
| 35 | |
| 36 | -- 5/ Requête pour selectionner les identifiants des valeurs dupliquées |
| 37 | {{{ |
| 38 | SELECT * from geobs.franchissabilite_piscicole where fra_id not in( |
| 39 | |
| 40 | SELECT fra_id from ( |
| 41 | SELECT DISTINCT ON (fra_esp_id,fra_ref_id)* |
| 42 | from geobs2010.franchissabilite_piscicole) as distinctes) |
| 43 | |
| 44 | ORDER BY fra_ref_id; -- 107 lignes |
| 45 | }}} |
| 46 | |
| 47 | -- 6/ Requète pour sélectionner toutes les valeurs dupliquées |
| 48 | {{{ |
| 49 | select * from geobs.franchissabilite_piscicole where fra_ref_id in( |
| 50 | |
| 51 | SELECT fra_ref_id from geobs2010.franchissabilite_piscicole where fra_id not in( |
| 52 | |
| 53 | SELECT fra_id from ( |
| 54 | SELECT DISTINCT ON (fra_esp_id,fra_ref_id)* |
| 55 | from geobs2010.franchissabilite_piscicole) as distinctes) |
| 56 | |
| 57 | ) |
| 58 | ORDER BY fra_ref_id, fra_esp_id; |
| 59 | -- 7/ je crée une table contenant barrages avec plus que une seule note pour la même espèce |
| 60 | drop table if exists duplicated; |
| 61 | create temporary table duplicated as( |
| 62 | select *from geobs2010.franchissabilite_piscicole where fra_ref_id in( |
| 63 | |
| 64 | SELECT fra_ref_id from geobs2010.franchissabilite_piscicole where fra_id not in( |
| 65 | |
| 66 | SELECT fra_id from ( |
| 67 | SELECT DISTINCT ON (fra_esp_id,fra_ref_id)* |
| 68 | from geobs2010.franchissabilite_piscicole) as distinctes) |
| 69 | |
| 70 | ) |
| 71 | ORDER BY fra_ref_id, fra_esp_id); |
| 72 | -- 8/ Liste de tous les dupliqués avec la même note pour la même espèce |
| 73 | drop table if exists identiques; |
| 74 | create temporary table identiques as( |
| 75 | select * from duplicated where (fra_ref_id, fra_esp_id,fra_note) IN |
| 76 | |
| 77 | (SELECT fra_ref_id, fra_esp_id,fra_note FROM duplicated |
| 78 | group by fra_ref_id, fra_esp_id,fra_note |
| 79 | HAVING COUNT(*) > 1) )--115 |
| 80 | }}} |
| 81 | |
| 82 | -- 9/ Je veux n'en garder qu'un donc avoir la liste des autres à virer pour sûr, je choisis un |
| 83 | -- id au hasard avec distinct on et je vais demander de virer les autres |
| 84 | {{{ |
| 85 | select * from identiques where fra_id not in( |
| 86 | select distinct on (fra_ref_id,fra_esp_id,fra_note) fra_id |
| 87 | from identiques) -- 59 lignes à dégager, 56 gardées |
| 88 | }}} |