feat: add bin/join.sh

This commit is contained in:
dancingCycle 2023-12-21 17:28:27 +01:00
parent 4340a1d7ea
commit 57f370029b
7 changed files with 80 additions and 0 deletions

26
bin/join.sh Normal file
View File

@ -0,0 +1,26 @@
#!/bin/sh
#
echo "Started..."
#Started...
#
# special variable $# is the number of arguments
if [ $# -lt 3 ] ; then
echo 'Call ./<script> <db name> <db user> <db schema>'
exit 1
fi
#
DB_NAME="$1"
echo "DB_NAME: ${DB_NAME}"
DB_USER="$2"
echo "DB_USER: ${DB_USER}"
DB_SCHEMA="$3"
echo "DB_SCHEMA: ${DB_SCHEMA}"
#
psql -h localhost -p 5432 -U $DB_USER -f ./sql/join-gncy-rts.sql -d $DB_NAME -v schema=$DB_SCHEMA
psql -h localhost -p 5432 -U $DB_USER -f ./sql/join-gncy-rts-trps.sql -d $DB_NAME -v schema=$DB_SCHEMA
psql -h localhost -p 5432 -U $DB_USER -f ./sql/services.sql -d $DB_NAME -v schema=$DB_SCHEMA
psql -h localhost -p 5432 -U $DB_USER -f ./sql/join-gncy-rts-trps-srvcs.sql -d $DB_NAME -v schema=$DB_SCHEMA
psql -h localhost -p 5432 -U $DB_USER -f ./sql/join-gncy-rts-trps-srvcs-pdts.sql -d $DB_NAME -v schema=$DB_SCHEMA
#
echo "Done."
#done.

View File

@ -0,0 +1,14 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
CREATE SCHEMA IF NOT EXISTS :schema_gtfsr;
SET search_path to :schema, :schema_gtfsr, public;
---create view
DROP VIEW IF EXISTS :schema.vw_join_gncy_rts_trps_srvcs_pdts CASCADE;
CREATE OR REPLACE VIEW :schema.vw_join_gncy_rts_trps_srvcs_pdts AS
SELECT gncy_rts_trps_srvcs.agency_name, gncy_rts_trps_srvcs.agency_id, gncy_rts_trps_srvcs.route_id, gncy_rts_trps_srvcs.route_short_name, gncy_rts_trps_srvcs.service_id, gncy_rts_trps_srvcs.trip_id, gncy_rts_trps_srvcs.trip_short_name, gncy_rts_trps_srvcs.trip_headsign, gncy_rts_trps_srvcs.dates, pdts.timestamp_pgsql FROM :schema.vw_join_gncy_rts_trps_srvcs AS gncy_rts_trps_srvcs LEFT JOIN :schema_gtfsr.trip_updates AS pdts ON gncy_rts_trps_srvcs.route_id = pdts.trip_route_id AND gncy_rts_trps_srvcs.trip_id = pdts.trip_trip_id ORDER BY gncy_rts_trps_srvcs.agency_name, gncy_rts_trps_srvcs.route_id, gncy_rts_trps_srvcs.trip_id, pdts.timestamp_pgsql;
DROP VIEW IF EXISTS mblthk.vw_join_gncy_rts_trps_srvcs_pdts CASCADE;
CREATE OR REPLACE VIEW mblthk.vw_join_gncy_rts_trps_srvcs_pdts AS
SELECT gncy_rts_trps_srvcs.agency_name, gncy_rts_trps_srvcs.agency_id, gncy_rts_trps_srvcs.route_id, gncy_rts_trps_srvcs.route_short_name, gncy_rts_trps_srvcs.service_id, gncy_rts_trps_srvcs.trip_id, gncy_rts_trps_srvcs.trip_short_name, gncy_rts_trps_srvcs.trip_headsign, gncy_rts_trps_srvcs.dates, pdts.timestamp_pgsql FROM mblthk.vw_join_gncy_rts_trps_srvcs AS gncy_rts_trps_srvcs LEFT JOIN gtfsr.trip_updates AS pdts ON gncy_rts_trps_srvcs.route_id = pdts.trip_route_id AND gncy_rts_trps_srvcs.trip_id = pdts.trip_trip_id ORDER BY gncy_rts_trps_srvcs.agency_name, gncy_rts_trps_srvcs.route_id, gncy_rts_trps_srvcs.trip_id, pdts.timestamp_pgsql;

View File

@ -0,0 +1,9 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
---create view
DROP VIEW IF EXISTS :schema.vw_join_gncy_rts_trps_srvcs CASCADE;
CREATE OR REPLACE VIEW :schema.vw_join_gncy_rts_trps_srvcs AS
SELECT gncy_rts_trps.agency_id, gncy_rts_trps.agency_name, gncy_rts_trps.route_id, gncy_rts_trps.route_short_name, gncy_rts_trps.service_id, gncy_rts_trps.trip_id, gncy_rts_trps.trip_short_name, gncy_rts_trps.trip_headsign, srvcs.dates FROM :schema.vw_join_gncy_rts_trps AS gncy_rts_trps LEFT JOIN :schema.services AS srvcs ON gncy_rts_trps.service_id = srvcs.service_id ORDER BY gncy_rts_trps.agency_name, gncy_rts_trps.route_id, gncy_rts_trps.trip_id;

View File

@ -0,0 +1,8 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
---create view
DROP VIEW IF EXISTS :schema.vw_join_gncy_rts_trps CASCADE;
CREATE OR REPLACE VIEW :schema.vw_join_gncy_rts_trps AS SELECT gncy_rts.agency_id, gncy_rts.agency_name, gncy_rts.route_id, gncy_rts.route_short_name, trps.service_id, trps.trip_id, trps.trip_headsign, trps.trip_short_name FROM :schema.vw_join_gncy_rts AS gncy_rts LEFT JOIN :schema.trips AS trps ON trps.route_id = gncy_rts.route_id ORDER BY gncy_rts.agency_name, gncy_rts.route_id, trps.trip_id;

8
sql/join-gncy-rts.sql Normal file
View File

@ -0,0 +1,8 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
---create view
DROP VIEW IF EXISTS :schema.vw_join_gncy_rts CASCADE;
CREATE OR REPLACE VIEW :schema.vw_join_gncy_rts AS SELECT gncy.agency_id, gncy.agency_name, rts.route_id, rts.route_short_name FROM :schema.agency AS gncy LEFT JOIN :schema.routes AS rts ON rts.agency_id = gncy.agency_id ORDER BY gncy.agency_name, rts.route_id;

7
sql/rts-count.sql Normal file
View File

@ -0,0 +1,7 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
-- create view
CREATE OR REPLACE VIEW :schema.vw_route_count AS SELECT gncy_rts.agency_id, gncy_rts.agency_name, count(DISTINCT gncy_rts.route_id) AS route_count FROM :schema.vw_join_gncy_rts AS gncy_rts GROUP BY gncy_rts.agency_id, gncy_rts.agency_name ORDER BY route_count ASC;

8
sql/trps-count.sql Normal file
View File

@ -0,0 +1,8 @@
-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
---create view
CREATE OR REPLACE VIEW :schema.vw_trip_count AS
SELECT gncy_rts_trps.agency_name, gncy_rts_trps.agency_id, count(DISTINCT gncy_rts_trps.route_id) AS route_count, count(DISTINCT gncy_rts_trps.trip_id) AS trip_count FROM :schema.vw_join_gncy_rts_trps AS gncy_rts_trps GROUP BY gncy_rts_trps.agency_id, gncy_rts_trps.agency_name ORDER BY route_count ASC, trip_count ASC;