CREATE SCHEMA IF NOT EXISTS :schema;
CREATE SCHEMA IF NOT EXISTS :schema_vbn;
SET search_path to :schema, :schema_vbn, public;

DROP VIEW IF EXISTS :schema.vw_stops_zhv_join_vbn CASCADE;
DROP VIEW IF EXISTS :schema.vw_stops_zhv_join_vbn_ue CASCADE;
DROP TABLE IF EXISTS :schema.tbl_stops_zhv_join_vbn;

VACUUM ANALYZE;

CREATE OR REPLACE VIEW :schema.vw_stops_zhv_join_vbn AS SELECT stop_seq_no, stop_type, stop_dhid, stop_parent, zhv.stop_name AS stop_name_zhv, stop_lat, stop_lon, id, location, vbn.stop_name AS stop_name_vbn, lat, lon FROM :schema.stops AS zhv JOIN :schema_vbn.tdb_stops AS vbn ON zhv.stop_dhid = vbn.stop_name_extern ORDER BY zhv.stop_seq_no;

CREATE TABLE IF NOT EXISTS :schema.tbl_stops_zhv_join_vbn AS SELECT * FROM :schema.vw_stops_zhv_join_vbn;

CREATE OR REPLACE VIEW :schema.vw_stops_zhv_join_vbn_ue AS SELECT * FROM :schema.tbl_stops_zhv_join_vbn WHERE stop_lat != lat OR stop_lon != lon ORDER BY stop_seq_no;
