feat: add bin/route-count.sh and bin/trip-count.sh

This commit is contained in:
dancingCycle 2023-11-15 18:02:57 +01:00
parent 1605a90089
commit 5246a2b0ee
4 changed files with 60 additions and 0 deletions

22
bin/route-count.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
#
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/route-count.sql -d $DB_NAME -v schema=$DB_SCHEMA
#
echo "Done."
#done.

22
bin/trip-count.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
#
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/trip-count.sql -d $DB_NAME -v schema=$DB_SCHEMA
#
echo "Done."
#done.

8
sql/route-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_route_count AS
SELECT count(DISTINCT rts.route_id) AS route_count, rts.agency_id, gncy.agency_name FROM :schema.routes AS rts LEFT JOIN :schema.agency AS gncy ON rts.agency_id = gncy.agency_id GROUP BY rts.agency_id, gncy.agency_name ORDER BY route_count DESC;

8
sql/trip-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 count (DISTINCT trps.trip_id) AS trip_count, count(DISTINCT trps.route_id) AS route_count, rts.agency_id, gncy.agency_name FROM :schema.trips AS trps LEFT JOIN :schema.routes AS rts ON trps.route_id = rts.route_id LEFT JOIN :schema.agency AS gncy ON rts.agency_id = gncy.agency_id GROUP BY rts.agency_id, gncy.agency_name ORDER BY trip_count DESC, route_count DESC;