Commit bed51175 authored by Christine Plumejeaud's avatar Christine Plumejeaud
Browse files

Code SQL qui sert à préparer les requêtes de l'API pour vizsources. Facilite le debug aussi.

Cette version prépare le tag v1.1 qui va suivre et qui permettra de récupérer les points où aucun CR ni inputdone n'existe (et source vaut alors null) + les flags.
parent 5ed404b2
-- ca merdouille à cause des tonnages
select p.pointcall_uhgs_id, count(p.pkid) as total , count(tonnage) as nb_tonnage_filled, count(homeport) as nb_homeport_filled,
sum(tonnage::float) as bad_sum_tonnage
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
group by p.pointcall_uhgs_id
select distinct tonnage_unit, count(*) from navigoviz.pointcall p group by tonnage_unit
-- tx ou quintaux
select (tonnage::float)/24 from navigoviz.pointcall p where tonnage_unit = 'quintaux';
select pkid, tonnage, homeport, (case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where tonnage_unit is not null
-- ca marche super
select pointcall_uhgs_id, source_suite,
count(pkid) as total ,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
sum(tonnage_numeric) as good_sum_tonnage
from
(select source_suite, pointcall_uhgs_id, pkid, tonnage, homeport, commodity_purpose, birthplace, citizenship,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
-- where state_1789_fr = 'France' and pointcall_function = 'O'
group by pointcall_uhgs_id , source_suite
select * from navigoviz.pointcall p
where source_doc_id = '00364008'
order by pointcall_rankfull
-- marseille
select distinct source_suite, source_component from navigoviz.pointcall
select pp.* , toto.*
from ports.port_points pp ,
(
select pointcall_uhgs_id, source_suite,
count(pkid) as total ,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
sum(tonnage_numeric) as good_sum_tonnage
from (
select source_suite, pointcall_uhgs_id, pkid, tonnage, homeport, commodity_purpose, birthplace, citizenship,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
group by pointcall_uhgs_id , source_suite
) as toto
where toto.pointcall_uhgs_id = pp.uhgs_id
select nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
source_suite, pointcall_year, total, nb_tonnage_filled, nb_homeport_filled, nb_product_filled, nb_birthplace_filled, nb_citizenship_filled, good_sum_tonnage
from
(
select
pointcall_uhgs_id, source_suite, pointcall_year,
--nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
count(pkid) as total ,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
sum(tonnage_numeric) as good_sum_tonnage
from (
select pointcall_uhgs_id, source_suite,
(coalesce(extract(year from TO_DATE(pointcall_out_date2,'YYYY=MM=DD')), extract(year from TO_DATE(pointcall_in_date2,'YYYY=MM=DD')))) as pointcall_year,
--nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
pkid, tonnage, homeport, commodity_purpose, birthplace, citizenship,
--nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric,
nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
group by pointcall_uhgs_id, source_suite, pointcall_year
) as toto,
ports.port_points pp
where toto.pointcall_uhgs_id = pp.uhgs_id
-- aide le 23/12/2021
select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
source_suite, pointcall_year,
(case when pointcall_year = '1787' then nb_conges_1787_inputdone else nb_conges_1789_inputdone end) as nb_conges_inputdone,
(case when pointcall_year = '1787' then nb_conges_1787_cr else nb_conges_1789_cr end) as nb_conges_cr,
(case when pointcall_year = '1787' then nb_sante_1787 else nb_sante_1789 end) as nb_conges_sante,
(case when pointcall_year = '1787' then nb_petitcabotage_1787 else nb_petitcabotage_1789 end) as nb_petitcabotage,
nb_tonnage_filled, nb_homeport_filled, nb_product_filled, nb_birthplace_filled, nb_citizenship_filled, good_sum_tonnage
from ports.port_points pp ,
(
select
pointcall_uhgs_id, source_suite, pointcall_year,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
sum(tonnage_numeric) as good_sum_tonnage
from (
select pointcall_uhgs_id, source_suite,
(coalesce(extract(year from TO_DATE(substring(pointcall_out_date2 for 4),'YYYY')), extract(year from TO_DATE(substring(pointcall_in_date2 for 4),'YYYY'))))::int4 as pointcall_year,
nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
tonnage, homeport, commodity_purpose, birthplace, citizenship,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
group by pointcall_uhgs_id, source_suite, pointcall_year
) as toto
where toto.pointcall_uhgs_id = pp.uhgs_id
order by toponym, pointcall_year, source_suite
-- le 4 janvier
select ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
nb_sante_1787, nb_sante_1789, nb_petitcabotage_1789, nb_petitcabotage_1787
from ports.port_points pp
where uhgs_id = 'A0210797'
select ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
nb_sante_1787, nb_sante_1789, nb_petitcabotage_1789, nb_petitcabotage_1787
from ports.port_points pp
where uhgs_id = 'A0210797'
-- Vu avec Silvia le 05 janvier 2021 : il manque Vannes par exemple
select distinct source_suite , source_subset from navigoviz.pointcall where source_suite = '%Marseille%'
select distinct source_subset from navigoviz.pointcall where source_suite = '%Marseille%'
-- Registre du petit cabotage (1786-1787)
-- Expéditions "coloniales" Marseille (1789)
-- Santé Marseille
update navigoviz.pointcall set source_suite = source_subset where source_suite = 'Marseille';
-- version 1
select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
source_suite, pointcall_year,
(case when pointcall_year = '1787' then nb_conges_1787_inputdone else nb_conges_1789_inputdone end) as nb_conges_inputdone,
(case when pointcall_year = '1787' then nb_conges_1787_cr else nb_conges_1789_cr end) as nb_conges_cr,
(case when pointcall_year = '1787' then nb_sante_1787 else nb_sante_1789 end) as nb_conges_sante,
(case when pointcall_year = '1787' then nb_petitcabotage_1787 else nb_petitcabotage_1789 end) as nb_petitcabotage,
nb_tonnage_filled, nb_homeport_filled, nb_product_filled, nb_birthplace_filled, nb_citizenship_filled, good_sum_tonnage
from ports.port_points pp ,
(
select
pointcall_uhgs_id, source_suite, pointcall_year,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
sum(tonnage_numeric) as good_sum_tonnage
from (
select pointcall_uhgs_id, source_suite,
(coalesce(extract(year from TO_DATE(substring(pointcall_out_date2 for 4),'YYYY')), extract(year from TO_DATE(substring(pointcall_in_date2 for 4),'YYYY'))))::int4 as pointcall_year,
nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
tonnage, homeport, commodity_purpose, birthplace, citizenship,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
group by pointcall_uhgs_id, source_suite, pointcall_year
) as toto
-- on toto.pointcall_uhgs_id = pp.uhgs_id
where toto.pointcall_uhgs_id = pp.uhgs_id and pp.state_1789_fr = 'France' -- and province = 'Bretagne' -- and pointcall_year = 1787
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
'G5' as source_suite, 1787 as pointcall_year,
nb_conges_1787_inputdone as nb_conges_inputdone,
nb_conges_1787_cr as nb_conges_cr,
nb_sante_1787 as nb_conges_sante,
nb_petitcabotage_1787 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1787_cr is not null and nb_conges_1787_inputdone is null)
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
'G5' as source_suite, 1789 as pointcall_year,
nb_conges_1789_inputdone as nb_conges_inputdone,
nb_conges_1789_cr as nb_conges_cr,
nb_sante_1789 as nb_conges_sante,
nb_petitcabotage_1789 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1789_cr is not null and nb_conges_1789_inputdone is null)
order by toponym, pointcall_year, source_suite
-- version 2
-- version avec flag et avec source_suite à null si le port n'a pas de données.
-- celle retenue pour l'API le 10 janvier 2022 (implication : si nbxxx = null then nbxxx = 0 chez bernard)
select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
source_suite, pointcall_year,
(case when pointcall_year = '1787' then nb_conges_1787_inputdone else nb_conges_1789_inputdone end) as nb_conges_inputdone,
(case when pointcall_year = '1787' then nb_conges_1787_cr else nb_conges_1789_cr end) as nb_conges_cr,
(case when pointcall_year = '1787' then nb_sante_1787 else nb_sante_1789 end) as nb_conges_sante,
(case when pointcall_year = '1787' then nb_petitcabotage_1787 else nb_petitcabotage_1789 end) as nb_petitcabotage,
nb_tonnage_filled, nb_homeport_filled, nb_product_filled, nb_birthplace_filled, nb_citizenship_filled, nb_flag_filled, good_sum_tonnage
from ports.port_points pp, -- left outer join
(
select
pointcall_uhgs_id, source_suite, pointcall_year,
count(tonnage) as nb_tonnage_filled,
count(homeport) as nb_homeport_filled,
count(commodity_purpose) as nb_product_filled,
count(birthplace) as nb_birthplace_filled,
count(citizenship) as nb_citizenship_filled,
count(flag) as nb_flag_filled,
sum(tonnage_numeric) as good_sum_tonnage
from (
select pointcall_uhgs_id, source_suite,
(coalesce(extract(year from TO_DATE(substring(pointcall_out_date2 for 4),'YYYY')), extract(year from TO_DATE(substring(pointcall_in_date2 for 4),'YYYY'))))::int4 as pointcall_year,
nb_conges_1787_inputdone, nb_conges_1787_cr, nb_conges_1789_inputdone, nb_conges_1789_cr,
tonnage, homeport, commodity_purpose, birthplace, citizenship, flag,
(case when tonnage_unit = 'quintaux' then (tonnage::float)/24 else tonnage::float end) as tonnage_numeric
from navigoviz.pointcall p
where state_1789_fr = 'France' and pointcall_function = 'O'
) as k
group by pointcall_uhgs_id, source_suite, pointcall_year
) as toto
-- on toto.pointcall_uhgs_id = pp.uhgs_id
where toto.pointcall_uhgs_id = pp.uhgs_id and pp.state_1789_fr = 'France' -- and province = 'Bretagne' -- and pointcall_year = 1787
-- where pp.state_1789_fr = 'France'
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
'G5' as source_suite, 1787 as pointcall_year,
nb_conges_1787_inputdone as nb_conges_inputdone,
nb_conges_1787_cr as nb_conges_cr,
nb_sante_1787 as nb_conges_sante,
nb_petitcabotage_1787 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as nb_flag_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1787_cr is not null and nb_conges_1787_inputdone is null)
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
null as source_suite, 1787 as pointcall_year,
nb_conges_1787_inputdone as nb_conges_inputdone,
nb_conges_1787_cr as nb_conges_cr,
nb_sante_1787 as nb_conges_sante,
nb_petitcabotage_1787 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as nb_flag_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1787_cr is null and nb_conges_1787_inputdone is null and geom is not null)
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
'G5' as source_suite, 1789 as pointcall_year,
nb_conges_1789_inputdone as nb_conges_inputdone,
nb_conges_1789_cr as nb_conges_cr,
nb_sante_1789 as nb_conges_sante,
nb_petitcabotage_1789 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as nb_flag_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1789_cr is not null and nb_conges_1789_inputdone is null )
union
(select
ogc_fid, uhgs_id, toponyme_standard_fr as toponym, substate_1789_fr as substate, status, geonameid, amiraute as admiralty, province, shiparea , ST_AsGeoJSON(ST_Transform(geom,900913)) as point,
null as source_suite, 1789 as pointcall_year,
nb_conges_1789_inputdone as nb_conges_inputdone,
nb_conges_1789_cr as nb_conges_cr,
nb_sante_1789 as nb_conges_sante,
nb_petitcabotage_1789 as nb_petitcabotage,
null as nb_tonnage_filled, null as nb_homeport_filled, null as nb_product_filled, null as nb_birthplace_filled, null as nb_citizenship_filled, null as nb_flag_filled, null as good_sum_tonnage
from ports.port_points pp
where state_1789_fr = 'France' and nb_conges_1789_cr is null and nb_conges_1789_inputdone is null and geom is not null)
order by toponym, pointcall_year, source_suite
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment