64 | | Les données sont dans db_feeding_meuse.sql sur le serveur... reprendre à partir de TODO |
| 64 | {{{ |
| 65 | #!sql |
| 66 | -- insertion données Laura |
| 67 | create schema belge; |
| 68 | alter table stations set schema belge; |
| 69 | alter table mesures_ang set schema belge; |
| 70 | alter table obstacles set schema belge; |
| 71 | alter table anguilles set schema belge; |
| 72 | |
| 73 | |
| 74 | set search_path to belge,public,dbeel; |
| 75 | select * from belge.stations; |
| 76 | select st_srid(the_geom) from belge.stations;--103300 |
| 77 | --103300 est le code ESRI mais ce n'est pas un ESPG qui est 31370 |
| 78 | update belge.stations set the_geom=st_setsrid(the_geom,31370); |
| 79 | update belge.stations set the_geom=st_transform(the_geom,3035);--2484 |
| 80 | alter table belge.stations ADD CONSTRAINT station_pkey PRIMARY KEY (st_id ), |
| 81 | CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2), |
| 82 | CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL), |
| 83 | CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 27572), |
| 84 | CONSTRAINT geometry_valid_check CHECK (isvalid(the_geom)); |
| 85 | |
| 86 | |
| 87 | --Meuse |
| 88 | INSERT INTO dbeel.establishment (et_establishment_name) VALUES ('SPW'); |
| 89 | INSERT INTO dbeel.data_provider(dp_name, dp_et_id) VALUES ('Laura',7); |
| 90 | |
| 91 | alter table stations rename to operations; |
| 92 | create table stations as select distinct on (site) site, organisme, eau,commune, lambex,lambey, code_precxy, the_geom from operations |
| 93 | where code_typpec= 'PECHEELECTRIQUE'; |
| 94 | alter table stations add constraint c_pk_stations PRIMARY KEY (site); |
| 95 | |
| 96 | DROP TABLE if exists belge.stationdbeel CASCADE; |
| 97 | CREATE TABLE belge.stationdbeel ( |
| 98 | LIKE belge.stations, |
| 99 | CONSTRAINT pk_so_op_id PRIMARY KEY (op_id), |
| 100 | CONSTRAINT fk_so_observation_place_type_id FOREIGN KEY (op_no_observationplacetype) |
| 101 | REFERENCES dbeel_nomenclature.observation_place_type (no_id) |
| 102 | MATCH SIMPLE ON UPDATE CASCADE ON DELETE RESTRICT |
| 103 | ) INHERITS (dbeel.observation_places); |
| 104 | |
| 105 | INSERT INTO belge.stationdbeel |
| 106 | SELECT uuid_generate_v4() AS op_id, |
| 107 | 'SPW' AS op_gis_systemname , |
| 108 | 'STATIONS' AS op_gis_layername, |
| 109 | st.site AS op_gislocation, |
| 110 | site AS op_placename, |
| 111 | 10 AS op_no_observationplacetype, -- Sampling station |
| 112 | NULL AS op_op_id, |
| 113 | the_geom, |
| 114 | site, organisme, eau,commune, lambex,lambey, code_precxy FROM belge.stations st ; -- 563 lines |
| 115 | |
| 116 | select * from stationdbeel order by site |
| 117 | ----- |
| 118 | -- Pour intégrer les données de pêche il faut détailler le nombre d'anguilles par passage |
| 119 | -- Intégration dans la table opérations |
| 120 | |
| 121 | select numpas from belge.mesures_ang group by numpas -- de 1 à 4 et 12? mail SEP |
| 122 | select nb_pas from belge.operations group by nb_pas -- de 1 à 4 passages |
| 123 | select*from belge.mesures_ang order by id -- données en double idem pour anguilles |
| 124 | select*from belge.operations order by id -- données en double egalement |
| 125 | select*from belge.stations order by site,organisme,eau -- pas en double |
| 126 | |
| 127 | select m.nbrind |
| 128 | FROM belge.operations op JOIN belge.mesures_ang m ON (m.codsit=op.site AND m.datvis=op.dates) where numpas=1 |
| 129 | (select m.nbrind |
| 130 | FROM belge.operations op JOIN belge.mesures_ang m ON (m.codsit=op.site AND m.datvis=op.dates) where numpas=2 |
| 131 | (select m.nbrind as nbp3 |
| 132 | FROM belge.operations op JOIN belge.mesures_ang m ON (m.codsit=op.site AND m.datvis=op.dates) where numpas=3 |
| 133 | (select m.nbrind as nbp4 |
| 134 | FROM belge.operations op JOIN belge.mesures_ang m ON (m.codsit=op.site AND m.datvis=op.dates) where numpas=4 |
| 135 | |
| 136 | -- Rerentrer le tableau mesures_ang ou supprimer les lignes doubles |
| 137 | -- Créer 4 colonnes dans opérations |
| 138 | ---------------------------------------------------- |
| 139 | -- belge.electrofishing |
| 140 | --------------------------------------------------- |
| 141 | |
| 142 | alter table operations add constraint c_pk_id PRIMARY KEY (ope_id); |
| 143 | drop table if exists operationspeche; |
| 144 | create table operationspeche as select ope_id, |
| 145 | organisme, |
| 146 | code_typpec, |
| 147 | eau, |
| 148 | site, |
| 149 | dates, |
| 150 | nb_pas, |
| 151 | statut_validation, |
| 152 | resultatsfish, |
| 153 | code_repres_spec, |
| 154 | surface_peche from operations where code_typpec = 'PECHEELECTRIQUE';--874 |
| 155 | alter table operationspeche add constraint c_pk_id_operationspeche PRIMARY KEY (ope_id); |
| 156 | -- creation de la cle etrangere vers station |
| 157 | alter table operationspeche add constraint c_fk_site FOREIGN KEY (site) REFERENCES stations(site) ; |
| 158 | select count(*) from operationspeche; --874 |
| 159 | |
| 160 | |
| 161 | DROP TABLE if exists belge.electrofishing CASCADE; |
| 162 | CREATE TABLE belge.electrofishing ( |
| 163 | LIKE belge.operationspeche INCLUDING DEFAULTS INCLUDING CONSTRAINTS,-- seulement pour les contraintes check |
| 164 | CONSTRAINT pk_oo_ob_id PRIMARY KEY (ob_id), |
| 165 | CONSTRAINT fk_oo_dp_id FOREIGN KEY (ob_dp_id) |
| 166 | REFERENCES dbeel.data_provider (dp_id) MATCH SIMPLE |
| 167 | ON UPDATE CASCADE ON DELETE RESTRICT, |
| 168 | CONSTRAINT fk_oo_electrofishing_mean FOREIGN KEY (ef_no_electrofishing_mean) |
| 169 | REFERENCES dbeel_nomenclature.electrofishing_mean (no_id) MATCH SIMPLE |
| 170 | ON UPDATE CASCADE ON DELETE RESTRICT, |
| 171 | CONSTRAINT fk_oo_electrofishing_method FOREIGN KEY (ef_no_fishingmethod) |
| 172 | REFERENCES dbeel_nomenclature.scientific_observation_method (no_id) MATCH SIMPLE |
| 173 | ON UPDATE CASCADE ON DELETE RESTRICT, |
| 174 | CONSTRAINT fk_oo_ob_no_origin FOREIGN KEY (ob_no_origin) |
| 175 | REFERENCES dbeel_nomenclature.observation_origin (no_id) MATCH SIMPLE |
| 176 | ON UPDATE CASCADE ON DELETE RESTRICT, |
| 177 | CONSTRAINT fk_oo_ob_no_period FOREIGN KEY (ob_no_period) |
| 178 | REFERENCES dbeel_nomenclature.period_type (no_id) MATCH SIMPLE |
| 179 | ON UPDATE CASCADE ON DELETE RESTRICT, |
| 180 | CONSTRAINT fk_oo_ob_no_type FOREIGN KEY (ob_no_type) |
| 181 | REFERENCES dbeel_nomenclature.observation_type (no_id) MATCH SIMPLE |
| 182 | ON UPDATE CASCADE ON DELETE RESTRICT |
| 183 | ) INHERITS (dbeel.electrofishing); |
| 184 | |
| 185 | |
| 186 | INSERT INTO belge.electrofishing (ob_id,ob_no_origin,ob_no_type,ob_no_period,ob_starting_date,ob_ending_date,ob_dp_id, |
| 187 | ef_no_fishingmethod,ef_no_electrofishing_mean,ef_wetted_area,ef_fished_length,ef_fished_width,ef_duration,ef_nbpas,ob_op_id, |
| 188 | ope_id, |
| 189 | organisme, |
| 190 | code_typpec, |
| 191 | eau, |
| 192 | site, |
| 193 | dates, |
| 194 | nb_pas, |
| 195 | statut_validation, |
| 196 | resultatsfish, |
| 197 | code_repres_spec, |
| 198 | surface_peche |
| 199 | ) |
| 200 | SELECT |
| 201 | uuid_generate_v4() AS ob_id , |
| 202 | observation_origin.no_id AS ob_no_origin, |
| 203 | observation_type.no_id AS ob_no_type, |
| 204 | period_type.no_id AS ob_no_period, |
| 205 | dates AS ob_starting_date, |
| 206 | NULL AS ob_ending_date, |
| 207 | data_provider.dp_id AS ob_dp_id, -- belge |
| 208 | case when nb_pas is null then 61 -- Unknown |
| 209 | when nb_pas>=2 then 62 --whole (peche complete) |
| 210 | when nb_pas=1 then 61 else |
| 211 | 61 end AS ef_no_fishingmethod, --scientific_observation_method.no_id, |
| 212 | electrofishing_mean.no_id AS ef_no_electrofishing_mean, |
| 213 | surface_peche AS ef_wetted_area, |
| 214 | NULL AS ef_fished_length, |
| 215 | NULL AS ef_fished_width, |
| 216 | NULL AS ef_duration, |
| 217 | nb_pas AS ef_nbpas, |
| 218 | op.* |
| 219 | FROM dbeel_nomenclature.observation_origin, |
| 220 | dbeel_nomenclature.scientific_observation_method, |
| 221 | dbeel_nomenclature.observation_type, |
| 222 | dbeel_nomenclature.period_type, |
| 223 | dbeel.data_provider, |
| 224 | dbeel_nomenclature.electrofishing_mean, |
| 225 | (select st.op_id as op_ob_id, op.* from belge.operationspeche op |
| 226 | join belge.stationdbeel st on op.site=st.site |
| 227 | order by site |
| 228 | ) as op |
| 229 | |
| 230 | WHERE observation_origin.no_name='Raw data' |
| 231 | AND scientific_observation_method.sc_observation_category='Unknown' |
| 232 | --AND scientific_observation_method.sc_observation_category='Electro-fishing' (je le met pas plusieurs cas) |
| 233 | AND observation_type.no_name='Electro-fishing' |
| 234 | AND electrofishing_mean.no_name='Unknown' -- a preciser plus tard |
| 235 | AND period_type.no_name='Daily' |
| 236 | AND data_provider.dp_name='Laura'; --874 lignes |
| 237 | |
| 238 | select * from belge.electrofishing |
| 239 | |
| 240 | ------------------------------- |
| 241 | -- Batch intégration |
| 242 | ------------------------------- |
| 243 | -- |
| 244 | select count (*) from mesures_ang;--1501 |
| 245 | select * from mesures_ang; |
| 246 | /* |
| 247 | alter table mesures_ang add constraint c_pk_mea_id PRIMARY KEY (mea_id) ; |
| 248 | alter table mesures_ang add column mea_ope_id integer; |
| 249 | alter table mesures_ang add constraint c_fk_ope_id FOREIGN KEY (mea_ope_id) references operationspeche (ope_id); |
| 250 | */ |
| 251 | update mesures_ang set mea_ope_id=sub.ope_id |
| 252 | from (select ope_id, mea_id from mesures_ang mea join operationspeche o on (mea.codsit,mea.datvis)=(o.site,o.dates)) sub |
| 253 | where sub.mea_id=mesures_ang.mea_id;--1285 |
| 254 | |
| 255 | --select * from mesures_ang where mea_ope_id is not null; |
| 256 | |
| 257 | update mesures_ang set numpas=1 where mea_ope_id=263;--11 |
| 258 | |
| 259 | select * from belge.electrofishing |
| 260 | |
| 261 | drop view joineel; |
| 262 | create or replace view joineel as select ob_op_id,ob_id,mea_id,mea_ope_id,site as ope_st_id,numpas,lonmin,lonmax,nbrind from belge.electrofishing |
| 263 | join mesures_ang on mea_ope_id=ope_id; |
| 264 | select * from joineel; |
| 265 | |
| 266 | |
| 267 | |
| 268 | -- La première table fait référence aux opérations de pêches (electrofishing) |
| 269 | |
| 270 | DROP TABLE if exists belge.batch_ope CASCADE; |
| 271 | CREATE TABLE belge.batch_ope ( |
| 272 | ope_id integer, |
| 273 | ope_st_id character varying(25), |
| 274 | pa_numero integer, |
| 275 | nb integer, |
| 276 | CONSTRAINT pk_batch_ope_id PRIMARY KEY (ba_id), |
| 277 | CONSTRAINT fk_batch_ope_ba_no_species_id FOREIGN KEY (ba_no_species) |
| 278 | REFERENCES dbeel_nomenclature.species (no_id) |
| 279 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 280 | CONSTRAINT fk_batch_ope_ba_no_stage_id FOREIGN KEY (ba_no_stage) |
| 281 | REFERENCES dbeel_nomenclature.stage (no_id) |
| 282 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 283 | CONSTRAINT fk_batch_ope_ba_value_type FOREIGN KEY (ba_no_value_type) |
| 284 | REFERENCES dbeel_nomenclature.value_type (no_id) |
| 285 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 286 | CONSTRAINT fk_batch_ope_ba_no_biological_characteristic_type FOREIGN KEY (ba_no_biological_characteristic_type) |
| 287 | REFERENCES dbeel_nomenclature.biological_characteristic_type (no_id) |
| 288 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 289 | CONSTRAINT fk_batch_ope_ba_no_individual_status FOREIGN KEY (ba_no_individual_status) |
| 290 | REFERENCES dbeel_nomenclature.individual_status(no_id) |
| 291 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 292 | CONSTRAINT c_fk_batch_ope_ope_st_id FOREIGN KEY (ope_st_id) |
| 293 | REFERENCES belge.stations (site) MATCH SIMPLE |
| 294 | ON UPDATE NO ACTION ON DELETE NO ACTION |
| 295 | ) INHERITS (dbeel.batch); |
| 296 | |
| 297 | --select * from belge.batch_ope; |
| 298 | -- on peut mettre les ob_op_id dans le group by car structurellement ça rajoutera pas de lignes |
| 299 | /* |
| 300 | select sum(nbrind),ob_op_id,ob_id from joineel group by ob_op_id,ob_id |
| 301 | union |
| 302 | select 0 as sum, ob_op_id,ob_id from belge.electrofishing where ope_id not in ( |
| 303 | select mea_ope_id from joineel);--874 |
| 304 | */ |
| 305 | |
| 306 | -- nb_total |
| 307 | INSERT INTO belge.batch_ope |
| 308 | SELECT uuid_generate_v4() AS ba_id, |
| 309 | species.no_id AS ba_no_species, |
| 310 | stage.no_id AS ba_no_stage, |
| 311 | value_type.no_id AS ba_no_value_type, |
| 312 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 313 | joineel2.nb AS ba_quantity, |
| 314 | individual_status.no_id AS ba_no_individual_status, |
| 315 | 1 AS ba_batch_level, |
| 316 | joineel2.* --contains ba_ob_id and ba_ba_id |
| 317 | FROM dbeel_nomenclature.species, |
| 318 | dbeel_nomenclature.stage, |
| 319 | dbeel_nomenclature.biological_characteristic_type, |
| 320 | dbeel_nomenclature.value_type, |
| 321 | dbeel_nomenclature.individual_status, |
| 322 | (select ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,ope_st_id,0 as pa_numero,sum(nbrind) as nb from joineel group by ob_op_id,ob_id,mea_ope_id,ope_st_id |
| 323 | union |
| 324 | select ob_id,cast(NULL as uuid) as ba_ba_id,ope_id,site as ope_st_id, 0 as pa_numero, 0 as nb from belge.electrofishing where ope_id not in ( |
| 325 | select mea_ope_id from joineel) |
| 326 | )joineel2 |
| 327 | WHERE species.no_name='Anguilla anguilla' |
| 328 | AND stage.no_name='Yellow eel' |
| 329 | AND biological_characteristic_type.no_name='Number' |
| 330 | AND individual_status.no_name='Alive' |
| 331 | AND value_type.no_name='Raw data or Individual data'; --874 lines |
| 332 | |
| 333 | /* |
| 334 | select * from batch_ope |
| 335 | select * from joineel where numpas=12; |
| 336 | select * from joineel where mea_ope_id=263 |
| 337 | |
| 338 | select * from joineel; |
| 339 | select ob_op_id,ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,1 as pa_numero,sum(nbrind) as nb from joineel |
| 340 | where numpas=1 |
| 341 | group by ob_op_id,ob_id,mea_ope_id |
| 342 | */ |
| 343 | |
| 344 | -- op_nbp1 |
| 345 | INSERT INTO belge.batch_ope |
| 346 | SELECT uuid_generate_v4() AS ba_id, |
| 347 | species.no_id AS ba_no_species, |
| 348 | stage.no_id AS ba_no_stage, |
| 349 | value_type.no_id AS ba_no_value_type, |
| 350 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 351 | joineel2.nb AS ba_quantity, |
| 352 | individual_status.no_id AS ba_no_individual_status, |
| 353 | 2 AS ba_batch_level, |
| 354 | joineel2.* --contains ba_ob_id and ba_ba_id |
| 355 | FROM dbeel_nomenclature.species, |
| 356 | dbeel_nomenclature.stage, |
| 357 | dbeel_nomenclature.biological_characteristic_type, |
| 358 | dbeel_nomenclature.value_type, |
| 359 | dbeel_nomenclature.individual_status, |
| 360 | (select ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,ope_st_id,1 as pa_numero,sum(nbrind) as nb from joineel |
| 361 | where numpas=1 |
| 362 | group by ob_op_id,ob_id,mea_ope_id,ope_st_id) as joineel2 |
| 363 | WHERE species.no_name='Anguilla anguilla' |
| 364 | AND stage.no_name='Yellow eel' |
| 365 | AND biological_characteristic_type.no_name='Number p1' |
| 366 | AND individual_status.no_name='Alive' |
| 367 | AND value_type.no_name='Raw data or Individual data';--200 lines |
| 368 | |
| 369 | -- op_nbp2 |
| 370 | INSERT INTO belge.batch_ope |
| 371 | SELECT uuid_generate_v4() AS ba_id, |
| 372 | species.no_id AS ba_no_species, |
| 373 | stage.no_id AS ba_no_stage, |
| 374 | value_type.no_id AS ba_no_value_type, |
| 375 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 376 | joineel2.nb AS ba_quantity, |
| 377 | individual_status.no_id AS ba_no_individual_status, |
| 378 | 2 AS ba_batch_level, |
| 379 | joineel2.* --contains ba_ob_id and ba_ba_id |
| 380 | FROM dbeel_nomenclature.species, |
| 381 | dbeel_nomenclature.stage, |
| 382 | dbeel_nomenclature.biological_characteristic_type, |
| 383 | dbeel_nomenclature.value_type, |
| 384 | dbeel_nomenclature.individual_status, |
| 385 | (select ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,ope_st_id,2 as pa_numero,sum(nbrind) as nb from joineel |
| 386 | where numpas=2 |
| 387 | group by ob_op_id,ob_id,mea_ope_id,ope_st_id) as joineel2 |
| 388 | WHERE species.no_name='Anguilla anguilla' |
| 389 | AND stage.no_name='Yellow eel' |
| 390 | AND biological_characteristic_type.no_name='Number p2' |
| 391 | AND individual_status.no_name='Alive' |
| 392 | AND value_type.no_name='Raw data or Individual data';--96 lines |
| 393 | |
| 394 | -- op_nbp3 |
| 395 | INSERT INTO belge.batch_ope |
| 396 | SELECT uuid_generate_v4() AS ba_id, |
| 397 | species.no_id AS ba_no_species, |
| 398 | stage.no_id AS ba_no_stage, |
| 399 | value_type.no_id AS ba_no_value_type, |
| 400 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 401 | joineel2.nb AS ba_quantity, |
| 402 | individual_status.no_id AS ba_no_individual_status, |
| 403 | 2 AS ba_batch_level, |
| 404 | joineel2.* --contains ba_ob_id and ba_ba_id |
| 405 | FROM dbeel_nomenclature.species, |
| 406 | dbeel_nomenclature.stage, |
| 407 | dbeel_nomenclature.biological_characteristic_type, |
| 408 | dbeel_nomenclature.value_type, |
| 409 | dbeel_nomenclature.individual_status, |
| 410 | (select ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,ope_st_id,3 as pa_numero,sum(nbrind) as nb from joineel |
| 411 | where numpas=3 |
| 412 | group by ob_op_id,ob_id,mea_ope_id,ope_st_id) as joineel2 |
| 413 | WHERE species.no_name='Anguilla anguilla' |
| 414 | AND stage.no_name='Yellow eel' |
| 415 | AND biological_characteristic_type.no_name='Number p3' |
| 416 | AND individual_status.no_name='Alive' |
| 417 | AND value_type.no_name='Raw data or Individual data';--2 lines |
| 418 | |
| 419 | -- op_nbp4 |
| 420 | INSERT INTO belge.batch_ope |
| 421 | SELECT uuid_generate_v4() AS ba_id, |
| 422 | species.no_id AS ba_no_species, |
| 423 | stage.no_id AS ba_no_stage, |
| 424 | value_type.no_id AS ba_no_value_type, |
| 425 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 426 | joineel2.nb AS ba_quantity, |
| 427 | individual_status.no_id AS ba_no_individual_status, |
| 428 | 2 AS ba_batch_level, |
| 429 | joineel2.* --contains ba_ob_id and ba_ba_id |
| 430 | FROM dbeel_nomenclature.species, |
| 431 | dbeel_nomenclature.stage, |
| 432 | dbeel_nomenclature.biological_characteristic_type, |
| 433 | dbeel_nomenclature.value_type, |
| 434 | dbeel_nomenclature.individual_status, |
| 435 | (select ob_id,cast(NULL as uuid) as ba_ba_id,mea_ope_id as ope_id,ope_st_id,4 as pa_numero,sum(nbrind) as nb from joineel |
| 436 | where numpas=4 |
| 437 | group by ob_op_id,ob_id,mea_ope_id,ope_st_id) as joineel2 |
| 438 | WHERE species.no_name='Anguilla anguilla' |
| 439 | AND stage.no_name='Yellow eel' |
| 440 | AND biological_characteristic_type.no_name='Number p4' |
| 441 | AND individual_status.no_name='Alive' |
| 442 | AND value_type.no_name='Raw data or Individual data';--1 line |
| 443 | |
| 444 | |
| 445 | -- pas de densités |
| 446 | /* |
| 447 | INSERT INTO belge.batch_ope |
| 448 | SELECT uuid_generate_v4() AS ba_id, |
| 449 | species.no_id AS ba_no_species, |
| 450 | stage.no_id AS ba_no_stage, |
| 451 | value_type.no_id AS ba_no_value_type, |
| 452 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 453 | joineel.op_nb AS ba_quantity, |
| 454 | individual_status.no_id AS ba_no_individual_status, |
| 455 | 1 AS ba_batch_level, |
| 456 | joineel.* --contains ba_ob_id and ba_ba_id |
| 457 | FROM dbeel_nomenclature.species, |
| 458 | dbeel_nomenclature.stage, |
| 459 | dbeel_nomenclature.biological_characteristic_type, |
| 460 | dbeel_nomenclature.value_type, |
| 461 | dbeel_nomenclature.individual_status, |
| 462 | (select ob_id as ba_ob_id, cast(NULL as uuid) as ba_ba_id,op_st_id,op_id,op_density as op_nb, cast(Null as integer) as pa_numero from belge.electrofishing) as joineel |
| 463 | WHERE species.no_name='Anguilla anguilla' |
| 464 | AND stage.no_name='Yellow eel' |
| 465 | AND biological_characteristic_type.no_name='Density' |
| 466 | AND individual_status.no_name='Alive' |
| 467 | AND value_type.no_name='Raw data or Individual data'; |
| 468 | */ |
| 469 | |
| 470 | |
| 471 | -- Batch integration, the second level will reference the fish table |
| 472 | select * from joineel |
| 473 | select * from joineel join operationspeche on mea_ope_id=ope_id order by lonmax |
| 474 | -- les anguilles de belgique sont grosses mais pas plus que ça.... |
| 475 | -- A 100 km de la mer... |
| 476 | -- Mais il y a eu des transports... |
| 477 | update mesures_ang set lonmax=NULL where lonmax=9999;-- prétentieux |
| 478 | -- les petites anguilles correspondent elles à des opérations ou les anguilles sont nombreuses (sites de déversements ?) |
| 479 | select * from mesures_ang |
| 480 | where mea_ope_id in |
| 481 | (select ope_id from batch_ope where ba_batch_level=1 and ba_quantity>10) |
| 482 | order by codsit,lonmax |
| 483 | --1275 1276 1277=> civelles a virer |
| 484 | --100BUR |
| 485 | --1288 1290 => civelles |
| 486 | --BERW_40222 lâché de civelles (correspondant à 1288 1290) |
| 487 | --BERW_03260 189 182 |
| 488 | --1338 site=BERW_04130 |
| 489 | --18 |
| 490 | select * from mesures_ang where codsit='100BUR' |
| 491 | select * from mesures_ang |
| 492 | where mea_ope_id not in (1275,1276,1277,1288,1290,1338,189,182) |
| 493 | order by lonmax; |
| 494 | select * from mesures_ang where lonmax<50 and datvis<'1998-01-01' |
| 495 | update mesures_ang set lonmax=10*lonmax where lonmax<50 and datvis<'1998-01-01';--3 |
| 496 | |
| 497 | -- données de taille |
| 498 | DROP TABLE if exists belge.batch_fish CASCADE; |
| 499 | CREATE TABLE belge.batch_fish ( |
| 500 | ba_ope_id integer, |
| 501 | mea_id integer, |
| 502 | --LIKE belge.fish_fi INCLUDING DEFAULTS INCLUDING CONSTRAINTS, |
| 503 | CONSTRAINT pk_fish_fi_id PRIMARY KEY (ba_id), |
| 504 | CONSTRAINT fk_fish_fi_ba_no_species_id FOREIGN KEY (ba_no_species) |
| 505 | REFERENCES dbeel_nomenclature.species (no_id) |
| 506 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 507 | CONSTRAINT fk_fish_fi_ba_no_stage_id FOREIGN KEY (ba_no_stage) |
| 508 | REFERENCES dbeel_nomenclature.stage (no_id) |
| 509 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 510 | CONSTRAINT fk_fish_fi_ba_value_type FOREIGN KEY (ba_no_value_type) |
| 511 | REFERENCES dbeel_nomenclature.value_type (no_id) |
| 512 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 513 | CONSTRAINT fk_fish_fi_ba_no_biological_characteristic_type FOREIGN KEY (ba_no_biological_characteristic_type) |
| 514 | REFERENCES dbeel_nomenclature.biological_characteristic_type (no_id) |
| 515 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 516 | CONSTRAINT fk_fish_fi_ba_no_individual_status FOREIGN KEY (ba_no_individual_status) |
| 517 | REFERENCES dbeel_nomenclature.individual_status(no_id) |
| 518 | ON DELETE RESTRICT ON UPDATE CASCADE, |
| 519 | CONSTRAINT c_fk_fi_op_id FOREIGN KEY (ba_ope_id) |
| 520 | REFERENCES belge.operationspeche (ope_id) MATCH SIMPLE |
| 521 | ON UPDATE NO ACTION ON DELETE NO ACTION |
| 522 | ) INHERITS (dbeel.batch); |
| 523 | |
| 524 | select * from belge.batch_fish |
| 525 | |
| 526 | -- creation des lots avec 1 ind; |
| 527 | INSERT INTO belge.batch_fish |
| 528 | SELECT uuid_generate_v4() AS ba_id, |
| 529 | species.no_id AS ba_no_species, |
| 530 | stage.no_id AS ba_no_stage, |
| 531 | value_type.no_id AS ba_no_value_type, |
| 532 | biological_characteristic_type.no_id AS ba_no_biological_characteristic_type, |
| 533 | 1 AS ba_quantity, |
| 534 | individual_status.no_id AS ba_no_individual_status, |
| 535 | 3 AS ba_batch_level, |
| 536 | joineel.* --contains ba_ob_id and ba_ba_id |
| 537 | FROM dbeel_nomenclature.species, |
| 538 | dbeel_nomenclature.stage, |
| 539 | dbeel_nomenclature.biological_characteristic_type, |
| 540 | dbeel_nomenclature.value_type, |
| 541 | dbeel_nomenclature.individual_status, |
| 542 | (select ob_id as ba_ob_id, |
| 543 | cast(NULL as uuid) as ba_ba_id, |
| 544 | mea_ope_id as ba_ope_id, |
| 545 | mea_id |
| 546 | from joineel |
| 547 | where lonmax is not null |
| 548 | and nbrind=1 |
| 549 | ) as joineel |
| 550 | WHERE species.no_name='Anguilla anguilla' |
| 551 | AND stage.no_name='Yellow eel' |
| 552 | AND biological_characteristic_type.no_name='Number' |
| 553 | AND individual_status.no_name='Alive' |
| 554 | AND value_type.no_name='Raw data or Individual data';--1211 |
| 555 | |
| 556 | DROP TABLE if exists belge.mensurationindiv_biol_charac CASCADE; |
| 557 | CREATE TABLE belge.mensurationindiv_biol_charac ( |
| 558 | CONSTRAINT pk_mensindivbiocho_id PRIMARY KEY (bc_id), |
| 559 | CONSTRAINT fk_mensindivbiocho_bc_characteristic_type FOREIGN KEY (bc_no_characteristic_type) |
| 560 | REFERENCES dbeel_nomenclature.biological_characteristic_type (no_id) ON DELETE RESTRICT ON UPDATE CASCADE, |
| 561 | CONSTRAINT fk_mensindivbiocho_bc_value_type FOREIGN KEY (bc_no_value_type) |
| 562 | REFERENCES dbeel_nomenclature.value_type (no_id) ON DELETE RESTRICT ON UPDATE CASCADE, |
| 563 | CONSTRAINT fk_mensindivbiocho_bc_ba_id FOREIGN KEY (bc_ba_id) |
| 564 | REFERENCES belge.batch_fish (ba_id) ON DELETE RESTRICT ON UPDATE CASCADE |
| 565 | )INHERITS (dbeel.biological_characteristic); |
| 566 | -- select * from belge.mensurationindiv_biol_charac |
| 567 | |
| 568 | |
| 569 | -- tailles |
| 570 | INSERT INTO belge.mensurationindiv_biol_charac |
| 571 | SELECT |
| 572 | uuid_generate_v4() AS bc_id, |
| 573 | ba_id AS bc_ba_id, |
| 574 | biological_characteristic_type.no_id AS bc_no_characteristic_type, |
| 575 | value_type.no_id AS bc_no_value_type, |
| 576 | lonmax AS bc_numvalue |
| 577 | FROM |
| 578 | dbeel_nomenclature.biological_characteristic_type, |
| 579 | dbeel_nomenclature.value_type, |
| 580 | belge.batch_fish, |
| 581 | mesures_ang |
| 582 | WHERE mesures_ang.mea_id=batch_fish.mea_id |
| 583 | AND biological_characteristic_type.no_name = 'Length' |
| 584 | AND value_type.no_name = 'Raw data or Individual data' |
| 585 | ; --1211 lines |
| 586 | |
| 587 | -- select * from mesures_ang |
| 588 | |
| 589 | }}} |