diff --git a/BlocLink/ddl/00_create_all_october_11.sql b/BlocLink/ddl/00_create_all_october_11.sql new file mode 100644 index 0000000000000000000000000000000000000000..c5e83e08b3dde203295d11b898afc50b3a6d5958 --- /dev/null +++ b/BlocLink/ddl/00_create_all_october_11.sql @@ -0,0 +1,528 @@ +create database bloclink; + +create sequence pna.ashp_score_ashp_score_id_seq; + +create sequence pna.criteria_criteria_id_seq; + +create sequence pna.possible_answers_answer_id_seq; + +create sequence pna.questions_quetion_id_seq; + +create sequence pna.submitted_answers_id_seq; + +create sequence pna.que_ans_sw_id_seq; + +create sequence pna.building_unit_info_id_seq; + +create sequence pna.building_details_id_seq; + +create sequence pna.criteria_scores_id_seq; + +create sequence pna.report_file_id_seq; + +create sequence external_pna.building_unit_info_id_seq; + +create sequence external_pna.criterias_id_seq; + +create sequence external_pna.pna_score_id_seq; + +create sequence external_pna.pna_score_id_seq1; + +create sequence external_pna.possible_answers_id_seq; + +create sequence external_pna.que_ans_scoreweight_id_seq; + +create sequence external_pna.questions_id_seq; + +create sequence external_pna.report_file_id_seq; + +create sequence external_pna.submitted_answers_id_seq; + +create sequence external_pna.user_building_info_id_seq; + +create sequence external_pna.user_info_id_seq; + +create sequence external_pna.questions_id_seq1; + +create sequence external_pna.possible_answers_id_seq1; + +create sequence external_pna.user_info_id_seq1; + +create sequence external_pna.user_building_info_id_seq1; + +create sequence external_pna.submitted_answers_id_seq1; + +create sequence external_pna.building_info_id_seq; + +create sequence external_pna.mapbox_building_id_seq; + +create table public.django_content_type +( + id serial not null + constraint django_content_type_pkey + primary key, + app_label varchar(100) not null, + model varchar(100) not null, + constraint django_content_type_app_label_76bd3d3b_uniq + unique (app_label, model) +); + +create table public.auth_permission +( + id serial not null + constraint auth_permission_pkey + primary key, + name varchar(255) not null, + content_type_id integer not null + constraint auth_permiss_content_type_id_2f476e4b_fk_django_content_type_id + references django_content_type + deferrable initially deferred, + codename varchar(100) not null, + constraint auth_permission_content_type_id_01ab375a_uniq + unique (content_type_id, codename) +); + +create index auth_permission_417f1b1c + on public.auth_permission (content_type_id); + +create table public.auth_group +( + id serial not null + constraint auth_group_pkey + primary key, + name varchar(80) not null + constraint auth_group_name_key + unique +); + +create index auth_group_name_a6ea08ec_like + on public.auth_group (name); + +create table public.auth_group_permissions +( + id serial not null + constraint auth_group_permissions_pkey + primary key, + group_id integer not null + constraint auth_group_permissions_group_id_b120cbf9_fk_auth_group_id + references auth_group + deferrable initially deferred, + permission_id integer not null + constraint auth_group_permiss_permission_id_84c5c92e_fk_auth_permission_id + references auth_permission + deferrable initially deferred, + constraint auth_group_permissions_group_id_0cd325b0_uniq + unique (group_id, permission_id) +); + +create index auth_group_permissions_0e939a4f + on public.auth_group_permissions (group_id); + +create index auth_group_permissions_8373b171 + on public.auth_group_permissions (permission_id); + +create table public.auth_user +( + id serial not null + constraint auth_user_pkey + primary key, + password varchar(128) not null, + last_login timestamp with time zone, + is_superuser boolean not null, + username varchar(150) not null + constraint auth_user_username_key + unique, + first_name varchar(30) not null, + last_name varchar(30) not null, + email varchar(254) not null, + is_staff boolean not null, + is_active boolean not null, + date_joined timestamp with time zone not null +); + +create index auth_user_username_6821ab7c_like + on public.auth_user (username); + +create table public.auth_user_groups +( + id serial not null + constraint auth_user_groups_pkey + primary key, + user_id integer not null + constraint auth_user_groups_user_id_6a12ed8b_fk_auth_user_id + references auth_user + deferrable initially deferred, + group_id integer not null + constraint auth_user_groups_group_id_97559544_fk_auth_group_id + references auth_group + deferrable initially deferred, + constraint auth_user_groups_user_id_94350c0c_uniq + unique (user_id, group_id) +); + +create index auth_user_groups_e8701ad4 + on public.auth_user_groups (user_id); + +create index auth_user_groups_0e939a4f + on public.auth_user_groups (group_id); + +create table public.auth_user_user_permissions +( + id serial not null + constraint auth_user_user_permissions_pkey + primary key, + user_id integer not null + constraint auth_user_user_permissions_user_id_a95ead1b_fk_auth_user_id + references auth_user + deferrable initially deferred, + permission_id integer not null + constraint auth_user_user_per_permission_id_1fbb5f2c_fk_auth_permission_id + references auth_permission + deferrable initially deferred, + constraint auth_user_user_permissions_user_id_14a6b632_uniq + unique (user_id, permission_id) +); + +create index auth_user_user_permissions_e8701ad4 + on public.auth_user_user_permissions (user_id); + +create index auth_user_user_permissions_8373b171 + on public.auth_user_user_permissions (permission_id); + +create table public.django_admin_log +( + id serial not null + constraint django_admin_log_pkey + primary key, + action_time timestamp with time zone not null, + object_id text, + object_repr varchar(200) not null, + action_flag smallint not null + constraint django_admin_log_action_flag_check + check (action_flag >= 0), + change_message text not null, + content_type_id integer + constraint django_admin_content_type_id_c4bce8eb_fk_django_content_type_id + references django_content_type + deferrable initially deferred, + user_id integer not null + constraint django_admin_log_user_id_c564eba6_fk_auth_user_id + references auth_user + deferrable initially deferred +); + +create index django_admin_log_417f1b1c + on public.django_admin_log (content_type_id); + +create index django_admin_log_e8701ad4 + on public.django_admin_log (user_id); + +create table public.django_session +( + session_key varchar(40) not null + constraint django_session_pkey + primary key, + session_data text not null, + expire_date timestamp with time zone not null +); + +create index django_session_de54fa62 + on public.django_session (expire_date); + +create index django_session_session_key_c0390e0f_like + on public.django_session (session_key); + +create table public.django_migrations +( + id serial not null + constraint django_migrations_pkey + primary key, + app varchar(255) not null, + name varchar(255) not null, + applied timestamp with time zone not null +); + +create table pna.building_info +( + building_id integer not null + constraint building_info_pk + primary key, + address varchar not null, + owner_first_name text not null, + owner_last_name text not null, + owner_email varchar, + owner_phone_number bigint +); + +create unique index "building_info_building-id" + on pna.building_info (building_id); + +create table pna.building_unit_info +( + building_id integer not null + constraint building_unit_info_fk + references building_info, + unit_type varchar not null, + count integer not null, + id serial not null + constraint building_unit_info_pk_2 + primary key, + constraint building_unit_info_pk + unique (building_id, unit_type) +); + +create unique index building_unit_info_id_uindex + on pna.building_unit_info (id); + +create table pna.dropdown_answers +( + id serial not null + constraint possible_answers_pk + primary key, + answer varchar not null +); + +create unique index "possible_answers_answer-id" + on pna.dropdown_answers (id); + +create table pna.criterias +( + id serial not null + constraint criteria_pk + primary key, + criteria_name varchar not null +); + +create unique index "criteria_criteria-id" + on pna.criterias (id); + +create unique index "criteria_criteria-name" + on pna.criterias (criteria_name); + +create table pna.pna_costs +( + id serial not null + constraint ashp_score_pk + primary key, + building_id integer not null + constraint ashp_score_fk + references building_info, + ashp_low integer not null, + ashp_high integer not null, + solar_low integer not null, + solar_high integer not null, + weather_low integer not null, + weather_high integer, + weather_doi integer, + ashp_doi integer, + solar_doi integer, + weather_payback varchar, + ashp_payback varchar, + solar_payback varchar +); + +create unique index "ashp_score_ashp-score-id" + on pna.pna_costs (id); + +create table pna.questions +( + id serial not null + constraint questions_pk + primary key, + question text not null +); + +create unique index "questions_quetion-id" + on pna.questions (id); + +create table pna.submitted_answers +( + building_id integer not null + constraint submitted_answers_fk + references building_info, + question_id integer not null + constraint submitted_answers_fk1 + references questions, + answer_id integer not null + constraint submitted_answers_fk2 + references dropdown_answers, + id serial not null + constraint submitted_answers_pk_2 + primary key, + constraint submitted_answers_pk + unique (building_id, question_id) +); + +create unique index submitted_answers_id_uindex + on pna.submitted_answers (id); + +create table pna.que_ans_sw +( + question_id integer not null + constraint que_ans_sw_fk1 + references questions, + answer_id integer not null + constraint que_ans_sw_fk2 + references dropdown_answers, + score_weight integer not null, + criteria_id integer not null + constraint que_ans_sw_fk3 + references criterias, + id serial not null + constraint que_ans_sw_pk_2 + primary key, + constraint que_ans_sw_pk + unique (question_id, answer_id) +); + +create table pna.criteria_scores +( + criteria_id integer not null + constraint criteria_score_fk1 + references criterias, + building_id integer not null + constraint criteria_score_fk + references building_info, + criteria_score integer not null, + id serial not null + constraint criteria_scores_pk + primary key, + constraint criteria_score_pk + unique (criteria_id, building_id) +); + +create table pna.building_details +( + building_id integer not null, + num_of_apts integer, + building_sqft integer, + num_of_floors integer, + num_of_heating_violations varchar, + num_of_dob_violations varchar, + legal_ownership varchar, + id serial not null + constraint building_details_pk + primary key +); + +create unique index building_details_building_id_uindex + on pna.building_details (building_id); + +create unique index building_details_id_uindex + on pna.building_details (id); + +create table pna.report_file +( + id serial not null + constraint report_file_pk + primary key, + resource varchar, + last_updated timestamp, + building_id integer + constraint report_file_building_info_building_id_fk + references building_info +); + +create table external_pna.questions +( + id serial not null + constraint questions_pk + primary key, + question varchar not null +); + +create unique index questions_id_uindex + on external_pna.questions (id); + +create table external_pna.possible_answers +( + id serial not null + constraint possible_answers_pk + primary key, + answer varchar not null +); + +create unique index possible_answers_id_uindex + on external_pna.possible_answers (id); + +create table external_pna.user_info +( + id serial not null + constraint user_info_pk + primary key, + first_name varchar not null, + last_name varchar not null, + email varchar not null, + phone_number varchar, + user_confirmed boolean, + token varchar, + token_time varchar +); + +create unique index user_info_id_uindex + on external_pna.user_info (id); + +create unique index user_info_token_uindex + on external_pna.user_info (token); + +create table external_pna.building_info +( + id serial not null + constraint building_info_pk + primary key, + building_id integer not null, + address varchar not null +); + +create table external_pna.user_building_info +( + id serial not null + constraint user_building_info_pk + primary key, + user_id integer + constraint user_building_info_user_info_id_fk + references user_info, + building_id integer not null + constraint user_building_info_building_info_building_id_fk + references building_info (building_id), + completed boolean default false +); + +create unique index user_building_info_id_uindex + on external_pna.user_building_info (id); + +create table external_pna.submitted_answers +( + id serial not null + constraint submitted_answers_pk + primary key, + user_building_id integer + constraint submitted_answers_user_building_info_id_fk + references user_building_info, + question_id integer + constraint submitted_answers_questions_id_fk + references questions, + answer_id integer + constraint submitted_answers_possible_answers_id_fk + references possible_answers +); + +create unique index submitted_answers_id_uindex + on external_pna.submitted_answers (id); + +create unique index building_info_address_uindex + on external_pna.building_info (address); + +create unique index building_info_building_id_uindex + on external_pna.building_info (building_id); + +create unique index building_info_id_uindex + on external_pna.building_info (id); + +create table external_pna.mapbox_building +( + id serial not null, + mapbox_address varchar, + building_id integer +); + diff --git a/Building/DAL/baltimore_get_building.sql b/Building/DAL/baltimore_get_building.sql new file mode 100644 index 0000000000000000000000000000000000000000..c11e19214d0f37810153aa8f64e817e3d2969fe6 --- /dev/null +++ b/Building/DAL/baltimore_get_building.sql @@ -0,0 +1,43 @@ +create function baltimore_building_get(in_building_id integer DEFAULT NULL::integer) + returns TABLE( + building_id integer, + lot_id integer, + bbl bigint, + bin bigint, + street_address text, + borough character varying, + zipcode numeric, + state character, + targeting_score double precision) +language plpgsql +as $$ +DECLARE + in_building_id ALIAS FOR $1; + + + BEGIN + if ( in_building_id is not null )then + return QUERY + SELECT b.id as building_id, + b.lot_id, + b.bbl, + b.bin, + CONCAT(a.house_number, ' ', a.street_name) :: text, + 'MANHATTAN' :: varchar(15) as borough, + a.zipcode :: numeric(5,0), + a.state, + 75 :: double precision as targeting_score + FROM public.building b + join public.building_address ba + on b.id = ba.building_id + join public.address a + on ba.address_id = a.id + WHERE b.id = in_building_id + ; + END IF; + + END; +$$; + +alter function baltimore_building_get(integer) + owner to blocpower; diff --git a/Building/DAL/building_search.sql b/Building/DAL/building_search.sql new file mode 100644 index 0000000000000000000000000000000000000000..365ecc70e044a544e52e39bd972d5aba18482c07 --- /dev/null +++ b/Building/DAL/building_search.sql @@ -0,0 +1,115 @@ +create function building_search( + in_bbl bigint DEFAULT NULL::bigint, + in_building_id integer DEFAULT NULL::integer, + in_address character varying DEFAULT NULL::character varying, + in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) + returns TABLE( + building_id integer, + lot_id integer, + bbl bigint, + bin bigint, + street_address text, + borough character varying, + zipcode numeric, + state character, + targeting_score double precision) +language plpgsql +as $$ +DECLARE + in_bbl ALIAS FOR $1; + in_building_id ALIAS for $2; + in_address ALIAS for $3; + in_zip ALIAS for $4; + in_boro ALIAS for $5; + + found_address_id integer default NULL; + found_lot_id integer default null; + + BEGIN + if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then + return QUERY -- Building ID or BBL Look up + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score + from vw_building_address v -- from vw_building_address join on Borough to Borough + join borough b + on b.id = v.borough_id + where v.building_id = in_building_id + or v.bbl = in_bbl + ; + ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN + select id + into found_address_id + from get_address(in_address, in_zip, in_boro) + ; + --raise notice 'outer else'; + Return QUERY -- Address Search Lookup + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score + from vw_building_address v + join borough b + on b.id = v.borough_id + WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) + or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro))-- use get address with in_address, in_zip, and in_boro + ; + + END IF; + + + END; +$$; + +alter function building_search(bigint, integer, varchar, numeric, integer) + owner to blocpower; + +--TEST CASES: +------------------------------------------------------------------------------------------------------------------ + + +/* +--inital search hack +select * from get_building(NULL,NULL,null,null) +order by address; + +select * from get_building(); + +select * from get_building('105 CENTRAL AVENUE',NULL,null,null); --address wihout zip + +select * from get_building('49 11 STREET 11101',NULL,null,NULL); --address wihout zip + +select * from get_building('49 11 STREET',3021560124,null,NULL); --address wihout zip + bbl + +select * from get_building('49 11 STREET',3021560124,591383); --address wihout zip + bbl+ blocpwer_id + +select * from get_building('49 11 STREET',NULL,591383); --address wihout zip + blocpwer_id + +select * from get_building('49 11 STREET',3021560124,591497); --address wihout zip + bbl+ wrong blocpwer_id + +select * from get_building(NULL,3021560124,NULL); -- bbl only + +select * from get_building(NULL,NULL,591383); -- blocpower_id only + + + +select * from get_building('49 11 STREET'); --using ordered defaults parameters + +select * from get_building(NULL,3021560124); ----using + +select * from get_building(in_bbl := 3017860039); --using named parameters + +select * from get_building() limit 100 --selcting all buildings; + +*/ diff --git a/Building/DAL/get_address.sql b/Building/DAL/get_address.sql new file mode 100644 index 0000000000000000000000000000000000000000..b0c93fc392f84189486b99d4626f081ae9ba36ba --- /dev/null +++ b/Building/DAL/get_address.sql @@ -0,0 +1,36 @@ +create function get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) + returns TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) +language plpgsql +as $$ +DECLARE + in_addres ALIAS FOR $1; + in_zip ALIAS for $2; + in_boro ALIAS FOR $3; + address_id integer; + BEGIN + SELECT replace(in_address,' ','&') into in_address; + + + return QUERY + select a.borough_id, + a.house_number, + a.house_number_sequence, + a.id, + a.pad_range_id, + a.street_name, + a.state, + a.zipcode + from address a + where street_address @@ to_tsquery(in_address) -- text search of in_address will be deprecated + and (a.borough_id = in_boro or in_boro is null) + and (a.zipcode = in_zip or in_zip is null) + + ; + + END; + +$$; + +alter function get_address(varchar, numeric, integer) + owner to blocpower; + diff --git a/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql b/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql deleted file mode 100644 index 9aed2f29c39b5cbcae7c81ff29b42265659b478b..0000000000000000000000000000000000000000 --- a/Building/ddl/app_dev_building__public_pre_phase_3_snapshot.sql +++ /dev/null @@ -1,886 +0,0 @@ --- --- PostgreSQL database dump --- - --- Dumped from database version 9.6.1 --- Dumped by pg_dump version 9.6.2 - -SET statement_timeout = 0; -SET lock_timeout = 0; -SET idle_in_transaction_session_timeout = 0; -SET client_encoding = 'UTF8'; -SET standard_conforming_strings = on; -SET check_function_bodies = false; -SET client_min_messages = warning; -SET row_security = off; - -SET search_path = public, pg_catalog; - --- --- Name: building_search(bigint, integer, character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, lot_id integer, bbl bigint, bin bigint, street_address text, borough character varying, zipcode numeric, state character, targeting_score float) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_bbl ALIAS FOR $1; - in_building_id ALIAS for $2; - in_address ALIAS for $3; - in_zip ALIAS for $4; - in_boro ALIAS for $5; - - found_address_id integer default NULL; - found_lot_id integer default null; - - BEGIN - if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then - return QUERY - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score - from vw_building_address v - join borough b - on b.id = v.borough_id - where v.building_id = in_building_id - or v.bbl = in_bbl - ; - ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN - select id - into found_address_id - from get_address(in_address, in_zip, in_boro) - ; - --raise notice 'outer else'; - Return QUERY --Query!!!!!!!!!!!!!!!!!!!!! - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score - from vw_building_address v - join borough b - on b.id = v.borough_id - WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) - or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) - ; - - END IF; - - - END; -$_$; - - --- --- Name: get_address(character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_addres ALIAS FOR $1; - in_zip ALIAS for $2; - in_boro ALIAS FOR $3; - address_id integer; - - - BEGIN - SELECT replace(in_address,' ','&') into in_address; - - - return QUERY - select a.borough_id, - a.house_number, - a.house_number_sequence, - a.id, - a.pad_range_id, - a.street_name, - a.state, - a.zipcode - from address a - where street_address @@ to_tsquery(in_address) - and (a.borough_id = in_boro or in_boro is null) - and (a.zipcode = in_zip or in_zip is null) - - ; - - END; - -$_$; - - --- --- Name: get_building(character varying, bigint, integer, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_building(in_address character varying DEFAULT NULL::character varying, in_bbl bigint DEFAULT NULL::bigint, in_lot_id integer DEFAULT NULL::integer, in_building_id integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, address character varying, bbl bigint, borough character, zipcode numeric, lot_id integer, bin_id bigint) - LANGUAGE plpgsql - AS $_$ - - DECLARE - address_input ALIAS FOR $1; - bbl_input ALIAS FOR $2; - lot_id_input ALIAS FOR $3; - building_id_input ALIAS for $4; - - - BEGIN - SELECT replace(address_input,' ','&') into address_input; - - if ( address_input is null and bbl_input is null and lot_id_input is null and building_id_input is null) then - return QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - join kf_bbl_list k - on p.bbl = k.bbl -; - else - RETURN QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - WHERE (address_input is NULL - or textsearchable_address @@ to_tsquery(address_input) ) - AND (bbl_input is null - or p.bbl = bbl_input) - AND (lot_id_input is NULL - or p.lot_id = lot_id_input) - AND (building_id_input is NULL - or p.id = building_id_input) - ; - -END IF; - - END; -$_$; - - -SET default_tablespace = ''; - -SET default_with_oids = false; - --- --- Name: address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE address ( - borough_id smallint, - house_number character varying(20), - house_number_sequence integer, - id integer NOT NULL, - load_date date DEFAULT now(), - pad_range_id integer, - street_name character varying(50), - state character(2), - zipcode numeric(5,0), - street_address tsvector -); - - --- --- Name: address_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE address_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: address_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE address_id_seq OWNED BY address.id; - - --- --- Name: administrative_division; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE administrative_division ( - census_block_2010 smallint, - census_track_2010 numeric(6,2), - census_tract_2_2010 integer, - city_council_district smallint, - community_district smallint, - health_area integer, - id integer NOT NULL, - load_date timestamp without time zone, - school_district smallint, - special_purpose_district_1 character varying(10), - special_purpose_district_2 character varying(10), - zone_class_1 character varying(17), - zone_class_2 character varying(15), - zone_district_clssification_1 character varying(9), - zone_district_clssification_2 character varying(9), - zone_district_clssification_3 character varying(9), - zone_district_clssification_4 character varying(9) -); - - --- --- Name: borough; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE borough ( - id smallint NOT NULL, - description character varying(15) -); - - --- --- Name: building_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE building_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: building; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building ( - basement_description_code smallint, - bbl bigint, - bin bigint, - building_dimentions_id integer, - class character(2), - condo_number smallint, - count_all_units smallint, - count_residential_units smallint, - far numeric(6,2), - id integer DEFAULT nextval('building_id_seq'::regclass) NOT NULL, - load_date timestamp without time zone, - lot_id integer, - number_of_floors numeric(5,2), - proximity_code smallint, - year_altered smallint, - year_altered_previous smallint, - year_built smallint, - year_built_estimate character varying(5), - targeting_score float -); - - --- --- Name: building_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address ( - building_id integer NOT NULL, - address_id integer NOT NULL, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_address_bkup; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address_bkup ( - building_id integer, - address_id integer, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_dimentions ( - area integer, - id integer NOT NULL, - load_date timestamp without time zone, - primary_structure_depth numeric(6,2), - primary_structure_frontage numeric(6,2) -); - - --- --- Name: city_service; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE city_service ( - fire_company character varying(4), - id integer NOT NULL, - load_date timestamp without time zone, - police_precinct smallint, - sanitation_boro smallint, - sanitation_district smallint, - sanitation_subsection character varying(2) -); - - --- --- Name: document; Type: FOREIGN TABLE; Schema: public; Owner: - --- - -CREATE FOREIGN TABLE document ( - id integer NOT NULL, - created timestamp without time zone, - updated timestamp without time zone, - content_type character varying(255), - key uuid NOT NULL, - name character varying(255) NOT NULL, - tags character varying(255), - building_id integer NOT NULL -) -SERVER fdw_documents -OPTIONS ( - schema_name 'public', - table_name 'document' -); -ALTER FOREIGN TABLE document ALTER COLUMN id OPTIONS ( - column_name 'id' -); -ALTER FOREIGN TABLE document ALTER COLUMN created OPTIONS ( - column_name 'created' -); -ALTER FOREIGN TABLE document ALTER COLUMN updated OPTIONS ( - column_name 'updated' -); -ALTER FOREIGN TABLE document ALTER COLUMN content_type OPTIONS ( - column_name 'content_type' -); -ALTER FOREIGN TABLE document ALTER COLUMN key OPTIONS ( - column_name 'key' -); -ALTER FOREIGN TABLE document ALTER COLUMN name OPTIONS ( - column_name 'name' -); -ALTER FOREIGN TABLE document ALTER COLUMN tags OPTIONS ( - column_name 'tags' -); -ALTER FOREIGN TABLE document ALTER COLUMN building_id OPTIONS ( - column_name 'building_id' -); - - --- --- Name: finance; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE finance ( - assessed_land_value bigint, - assessed_lot_value bigint, - exempt_land_value bigint, - exempt_total_value bigint, - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE lot_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: lot; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot ( - administrative_deivision_id integer, - appointment_bbl bigint, - appointment_date timestamp without time zone, - areasource smallint, - bbl bigint, - block smallint, - boro_code character(2), - boro_id smallint, - city_service_id integer, - commerical_overlay_code_1 character varying(9), - commerical_overlay_code_2 character varying(10), - dof_tax_map_number integer, - e_designation_number character varying(11), - extenstion_code character varying(3), - finance_id integer, - historical_district character varying(40), - id integer DEFAULT nextval('lot_id_seq'::regclass) NOT NULL, - irregular_shape_flag boolean, - landmark_name character varying(35), - land_use_category_code smallint, - limited_height_district_code character varying(5), - load_date timestamp without time zone, - lot_dimentions_id integer, - lot_number smallint, - lot_owner_id integer, - number_of_buildings smallint, - number_of_easements smallint, - pluto_id integer, - split_zone_flag boolean, - type smallint, - zone_map_border_flag character varying(5) -); - - --- --- Name: lot_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_address ( - lot_id integer NOT NULL, - address_id integer NOT NULL -); - - --- --- Name: lot_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_dimentions ( - allocated_factory_area integer, - allocated_garage_area integer, - allocated_office_area integer, - allocated_other_area integer, - allocated_residential_area integer, - allocated_retail_use integer, - allocated_storage_area integer, - area bigint, - area_commercial_use integer, - depth numeric(6,2), - far_commerical numeric(4,2), - far_facility numeric(4,2), - far_residential numeric(4,2), - frotage numeric(6,2), - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_owner; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_owner ( - id integer NOT NULL, - load_date timestamp without time zone, - owner_name character varying(21), - owner_type character(1) -); - - --- --- Name: pluto; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE pluto ( - dcp_map_number character varying(3), - id integer NOT NULL, - load_date timestamp without time zone, - pluto_map_id smallint, - pluto_version character varying(4), - sanborn_map_number character varying(8), - x_coord bigint, - y_coord bigint -); - - --- --- Name: vw_building_address; Type: MATERIALIZED VIEW; Schema: public; Owner: - --- - -CREATE MATERIALIZED VIEW vw_building_address AS - WITH address_mapping AS ( - SELECT DISTINCT b.bbl, - b.bin, - b.id AS building_id, - b.lot_id, - b.targeting_score, - ba.pad_range_id, - lh_1.min_sequence, - lh_1.max_sequence - FROM ((building b - JOIN building_address ba ON ((b.id = ba.building_id))) - JOIN ( SELECT building_address.building_id, - building_address.pad_range_id, - min(building_address.house_number_sequence) AS min_sequence, - max(building_address.house_number_sequence) AS max_sequence - FROM building_address - GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (((b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) - ) - SELECT DISTINCT aa.building_id, - aa.lot_id, - aa.bbl, - aa.bin, - aa.targeting_score, - (( - CASE - WHEN (lh.house_number_sequence = hh.house_number_sequence) THEN (lh.house_number)::text - ELSE (((lh.house_number)::text || ' -- '::text) || (hh.house_number)::text) - END || ' '::text) || (lh.street_name)::text) AS street_address, - lh.borough_id, - lh.zipcode, - lh.state, - aa.pad_range_id, - lh.id AS min_address_id, - hh.id AS max_address_id, - la.address_id AS lot_address_id - FROM (((address_mapping aa - JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) - ORDER BY aa.lot_id, aa.building_id - WITH NO DATA; - - --- --- Name: address id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address ALTER COLUMN id SET DEFAULT nextval('address_id_seq'::regclass); - - --- --- Name: address address_pkey1; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT address_pkey1 PRIMARY KEY (id); - - --- --- Name: administrative_division administrative_division_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY administrative_division - ADD CONSTRAINT administrative_division_pkey PRIMARY KEY (id); - - --- --- Name: borough borough_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY borough - ADD CONSTRAINT borough_pkey PRIMARY KEY (id); - - --- --- Name: building_address building_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building_address_pkey PRIMARY KEY (building_id, address_id); - - --- --- Name: building_dimentions building_dimentions_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_dimentions - ADD CONSTRAINT building_dimentions_pk PRIMARY KEY (id); - - --- --- Name: building building_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_pk PRIMARY KEY (id); - - --- --- Name: city_service city_service_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY city_service - ADD CONSTRAINT city_service_pkey PRIMARY KEY (id); - - --- --- Name: finance finance_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY finance - ADD CONSTRAINT finance_pkey PRIMARY KEY (id); - - --- --- Name: lot_address lot_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot_address_pkey PRIMARY KEY (lot_id, address_id); - - --- --- Name: lot_dimentions lot_dimentions_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_dimentions - ADD CONSTRAINT lot_dimentions_pkey PRIMARY KEY (id); - - --- --- Name: lot_owner lot_owner_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_owner - ADD CONSTRAINT lot_owner_pkey PRIMARY KEY (id); - - --- --- Name: lot lot_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_pkey PRIMARY KEY (id); - - --- --- Name: pluto pluto_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY pluto - ADD CONSTRAINT pluto_pkey PRIMARY KEY (id); - - --- --- Name: address__borough_id_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__borough_id_IDX" ON address USING btree (borough_id); - - --- --- Name: address__bourough_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__bourough_id_idx ON address USING btree (borough_id); - - --- --- Name: address__full_address_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__full_address_IDX" ON address USING btree (btrim((((house_number)::text || ' '::text) || (street_name)::text))); - - --- --- Name: address__range_seq_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__range_seq_IDX" ON address USING btree (pad_range_id, house_number_sequence); - - --- --- Name: address__street_address_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__street_address_idx ON address USING gin (street_address); - - --- --- Name: address__zip_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__zip_idx ON address USING btree (zipcode); - - --- --- Name: address__zipcode_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__zipcode_IDX" ON address USING btree (zipcode); - - --- --- Name: building_bbl_bin_unique_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "building_bbl_bin_unique_IDX" ON building USING btree (bbl, bin); - - --- --- Name: lot__bbl_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "lot__bbl_IDX" ON lot USING btree (bbl); - - --- --- Name: lot__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX lot__bbl_idx ON lot USING btree (bbl); - - --- --- Name: vw_building_address__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__bbl_idx ON vw_building_address USING btree (bbl); - - --- --- Name: vw_building_address__building_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__building_id_idx ON vw_building_address USING btree (building_id); - - --- --- Name: vw_building_address__pad_range_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX vw_building_address__pad_range_id_idx ON vw_building_address USING btree (pad_range_id); - - --- --- Name: building_address address__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT address__building_address_fk FOREIGN KEY (address_id) REFERENCES address(id) ON DELETE SET NULL; - - --- --- Name: lot_address address__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT address__lot_address_fk FOREIGN KEY (address_id) REFERENCES address(id); - - --- --- Name: lot administrative_division_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT administrative_division_lot_fk FOREIGN KEY (administrative_deivision_id) REFERENCES administrative_division(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: address borough__address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT borough__address_fk FOREIGN KEY (borough_id) REFERENCES borough(id); - - --- --- Name: building_address building__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building__building_address_fk FOREIGN KEY (building_id) REFERENCES building(id); - - --- --- Name: building building_dimentions_building_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_dimentions_building_fk FOREIGN KEY (building_dimentions_id) REFERENCES building_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: building building_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_lot_fk FOREIGN KEY (lot_id) REFERENCES lot(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot city_service_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT city_service_lot_fk FOREIGN KEY (city_service_id) REFERENCES city_service(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot finance_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT finance_lot_fk FOREIGN KEY (finance_id) REFERENCES finance(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot_address lot__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot__lot_address_fk FOREIGN KEY (lot_id) REFERENCES lot(id); - - --- --- Name: lot lot_dimentions_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_dimentions_lot_fk FOREIGN KEY (lot_dimentions_id) REFERENCES lot_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot lot_owner_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_owner_lot_fk FOREIGN KEY (lot_owner_id) REFERENCES lot_owner(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot pluto_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT pluto_lot_fk FOREIGN KEY (pluto_id) REFERENCES pluto(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- PostgreSQL database dump complete --- - diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql new file mode 100644 index 0000000000000000000000000000000000000000..cc04947ae0e2259956358c261860a3d2355c8e68 --- /dev/null +++ b/Building/ddl/building_functions.sql @@ -0,0 +1,145 @@ +-- TODO: create_building() +-- create function create_building( +-- in_place_name character varying DEFAULT NULL::character varying +-- returns TABLE( +-- building_id integer, +-- lot_id integer, +-- bbl bigint, +-- bin bigint, +-- street_address text, +-- borough character varying, +-- zipcode numeric, +-- state character, +-- targeting_score double precision, +-- place_name VARCHAR) +-- language plpgsql +-- as $$ +-- DECLARE +-- in_place_name ALIAS for $1; +-- BEGIN +-- if (in_place_name is null)then +-- return QUERY -- Building ID or BBL Look up +-- select v.building_id, +-- v.lot_id, +-- v.bbl, +-- v.bin, +-- v.street_address, +-- b.description as borough, +-- v.zipcode, +-- v.state, +-- v.targeting_score +-- v.place_name +-- from vw_building_address v -- from vw_building_address join on Borough to Borough +-- join borough b +-- on b.id = v.borough_id +-- where v.building_id = in_building_id +-- ; +-- ELSIF (in_building_id is null and (in_place_name is not null))THEN +-- Return QUERY -- Address Search Lookup +-- select v.building_id, +-- v.lot_id, +-- v.bbl, +-- v.bin, +-- v.street_address, +-- b.description as borough, +-- v.zipcode, +-- v.state, +-- v.targeting_score, +-- v.place_name, +-- from vw_building_address v +-- join borough b +-- on b.id = v.borough_id +-- where v.place_name = in_place_name-- use get address with in_address, in_zip, and in_boro +-- ; +-- END IF; +-- END; +-- $$; + +-- alter function read_building(integer, varchar) +-- owner to blocpower; +/* + +TEST CASES: create building + +-- building with place name doesn't exist +select * from create_building(in_place_name := '936 East 17th Street, New York, New York 10003, United States'); +-- building with place name exists +select * from create_building(in_place_name := '936 East 17th Street, New York, New York 10003, United States'); +*/ +-- read_building() +create function read_building( + in_building_id integer DEFAULT NULL::integer, + in_place_name character varying DEFAULT NULL::character varying) + returns TABLE( + building_id integer, + lot_id integer, + bbl bigint, + bin bigint, + street_address text, + borough character varying, + zipcode numeric, + state character, + targeting_score double precision, + place_name VARCHAR) +language plpgsql +as $$ +DECLARE + in_building_id ALIAS FOR $1; + in_place_name ALIAS for $2; + BEGIN + if ( (in_building_id is not null) and in_place_name is null)then + return QUERY -- Building ID or BBL Look up + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score, + v.place_name + from vw_building_address v -- from vw_building_address join on Borough to Borough + join borough b + on b.id = v.borough_id + where v.building_id = in_building_id + ; + ELSIF (in_building_id is null and (in_place_name is not null))THEN + Return QUERY -- Address Search Lookup + select v.building_id, + v.lot_id, + v.bbl, + v.bin, + v.street_address, + b.description as borough, + v.zipcode, + v.state, + v.targeting_score, + v.place_name + from vw_building_address v + join borough b + on b.id = v.borough_id + where v.place_name = in_place_name -- use get address with in_address, in_zip, and in_boro + ; + END IF; + END; +$$; + +alter function read_building(integer, varchar) + owner to blocpower; + +/* + +TEST CASES: read building + +select * from read_building(); + +select * from read_building(in_building_id := 591383); -- blocpower_id only + +select * from read_building(in_place_name := '105 CENTRAL AVENUE'); --using named param TODO: add valid place_name test + +select * from read_building() limit 100 --selcting all buildings; + +*/ + +-- TODO: update_building() diff --git a/Building/ddl/fdw_building.sql b/Building/ddl/fdw_building.sql index d3d45b7bdfaacccb83f90a15e9421fa39c033aa7..dafb44985e675e0d9795922460336576a557457e 100644 --- a/Building/ddl/fdw_building.sql +++ b/Building/ddl/fdw_building.sql @@ -1,4 +1,6 @@ -CREATE SERVER fdw_builging +-- Create Foreign Data Wrapper to City Data Research DB +-- TODO: How are are we using fdw building? +CREATE SERVER fdw_builging FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'citydataresearch.czgvwxaefxfj.us-east-1.rds.amazonaws.com', port '5432', dbname 'building'); @@ -7,4 +9,4 @@ CREATE USER MAPPING FOR blocpower OPTIONS (user 'blocpower', password '8tQzq97N6Qxkqt5Q5DoC'); --replace with proper password IMPORT FOREIGN SCHEMA oltp - FROM SERVER fdw_builging INTO salesforce_cleanlead; \ No newline at end of file + FROM SERVER fdw_builging INTO salesforce_cleanlead; diff --git a/Building/ddl/new_building.sql b/Building/ddl/new_building.sql new file mode 100644 index 0000000000000000000000000000000000000000..86ee69b358640e3d94ed641ff569f1753a2bb041 --- /dev/null +++ b/Building/ddl/new_building.sql @@ -0,0 +1,27 @@ +-- Updates to building to building table to add place name +ALTER TABLE public.building_address +ADD place_name varchar(255); + +-- copy Mapbox_building table from BlocLink and put in bis.mapbox_building +-- dump mapbox_building + +-- Update vw_building_address to read buildings using new column in building_address +-- changes in vw_building_address.sql + +-- Insert place_name from BlocLink +-- Read from Bloclink MapBox Building and update value by BuildingID +UPDATE public.building_address +SET place_name = mb.mapbox_address +FROM + bis.mapbox_building mb +WHERE + building_address.building_id = mb.building_id; + +-- refresh materialized view +REFRESH MATERIALIZED VIEW vw_building_address; + +-- Create new functions +-- new functions in building_functions.sql +-- read_building() +-- TODO: create_building() +-- TODO: update_building() diff --git a/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql b/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql deleted file mode 100644 index 5fdd1c1d5507270797b8df88c832c39583db7a3c..0000000000000000000000000000000000000000 --- a/Building/ddl/stg_building__public_pre_phase_3_snapshot.sql +++ /dev/null @@ -1,872 +0,0 @@ --- --- PostgreSQL database dump --- - --- Dumped from database version 9.5.4 --- Dumped by pg_dump version 9.6.2 - -SET statement_timeout = 0; -SET lock_timeout = 0; -SET idle_in_transaction_session_timeout = 0; -SET client_encoding = 'UTF8'; -SET standard_conforming_strings = on; -SET check_function_bodies = false; -SET client_min_messages = warning; -SET row_security = off; - -SET search_path = public, pg_catalog; - --- --- Name: building_search(bigint, integer, character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION building_search(in_bbl bigint DEFAULT NULL::bigint, in_building_id integer DEFAULT NULL::integer, in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, lot_id integer, bbl bigint, bin bigint, street_address text, borough character varying, zipcode numeric, state character, targeting_score float) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_bbl ALIAS FOR $1; - in_building_id ALIAS for $2; - in_address ALIAS for $3; - in_zip ALIAS for $4; - in_boro ALIAS for $5; - - found_address_id integer default NULL; - found_lot_id integer default null; - - BEGIN - if ( (in_bbl is not null or in_building_id is not null) and in_address is null and in_zip is null and in_boro is null )then - return QUERY - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score - from vw_building_address v - join borough b - on b.id = v.borough_id - where v.building_id = in_building_id - or v.bbl = in_bbl - ; - ELSIF (in_bbl is null and in_building_id is null and (in_address is not null or in_zip is not null or in_boro is not null) )THEN - select id - into found_address_id - from get_address(in_address, in_zip, in_boro) - ; - --raise notice 'outer else'; - Return QUERY --Query!!!!!!!!!!!!!!!!!!!!! - select v.building_id, - v.lot_id, - v.bbl, - v.bin, - v.street_address, - b.description as borough, - v.zipcode, - v.state, - v.targeting_score - from vw_building_address v - join borough b - on b.id = v.borough_id - WHERE v.pad_range_id in (select pad_range_id from get_address(in_address, in_zip, in_boro)) - or v.lot_address_id in (select id from get_address(in_address, in_zip, in_boro)) - ; - - END IF; - - - END; -$_$; - - --- --- Name: get_address(character varying, numeric, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_address(in_address character varying DEFAULT NULL::character varying, in_zip numeric DEFAULT NULL::numeric, in_boro integer DEFAULT NULL::integer) RETURNS TABLE(borough_id smallint, house_number character varying, house_number_sequence integer, id integer, pad_range_id integer, street_name character varying, state character, zipcode numeric) - LANGUAGE plpgsql - AS $_$ - - DECLARE - in_addres ALIAS FOR $1; - in_zip ALIAS for $2; - in_boro ALIAS FOR $3; - address_id integer; - - - BEGIN - SELECT replace(in_address,' ','&') into in_address; - - - return QUERY - select a.borough_id, - a.house_number, - a.house_number_sequence, - a.id, - a.pad_range_id, - a.street_name, - a.state, - a.zipcode - from address a - where street_address @@ to_tsquery(in_address) - and (a.borough_id = in_boro or in_boro is null) - and (a.zipcode = in_zip or in_zip is null) - - ; - - END; - -$_$; - - --- --- Name: get_building(character varying, bigint, integer, integer); Type: FUNCTION; Schema: public; Owner: - --- - -CREATE FUNCTION get_building(in_address character varying DEFAULT NULL::character varying, in_bbl bigint DEFAULT NULL::bigint, in_lot_id integer DEFAULT NULL::integer, in_building_id integer DEFAULT NULL::integer) RETURNS TABLE(building_id integer, address character varying, bbl bigint, borough character, zipcode numeric, lot_id integer, bin_id bigint) - LANGUAGE plpgsql - AS $_$ - - DECLARE - address_input ALIAS FOR $1; - bbl_input ALIAS FOR $2; - lot_id_input ALIAS FOR $3; - building_id_input ALIAS for $4; - - - BEGIN - SELECT replace(address_input,' ','&') into address_input; - - if ( address_input is null and bbl_input is null and lot_id_input is null and building_id_input is null) then - return QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - join kf_bbl_list k - on p.bbl = k.bbl -; - else - RETURN QUERY - SELECT p.id as building_id, - a.street_line_1, - p.bbl, - l.boro_code, - a.zip::numeric(5,0), - p.lot_id, - p.bin - FROM public.building p - join public.lot l - on p.lot_id = l.id - join public.address a - on l.address_id = a.id - WHERE (address_input is NULL - or textsearchable_address @@ to_tsquery(address_input) ) - AND (bbl_input is null - or p.bbl = bbl_input) - AND (lot_id_input is NULL - or p.lot_id = lot_id_input) - AND (building_id_input is NULL - or p.id = building_id_input) - ; - -END IF; - - END; -$_$; - - -SET default_tablespace = ''; - -SET default_with_oids = false; - --- --- Name: address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE address ( - borough_id smallint, - house_number character varying(20), - house_number_sequence integer, - id integer NOT NULL, - load_date date DEFAULT now(), - pad_range_id integer, - street_name character varying(50), - state character(2), - zipcode numeric(5,0), - street_address tsvector -); - - --- --- Name: address_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE address_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: address_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE address_id_seq OWNED BY address.id; - - --- --- Name: administrative_division; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE administrative_division ( - census_block_2010 smallint, - census_track_2010 numeric(6,2), - census_tract_2_2010 integer, - city_council_district smallint, - community_district smallint, - health_area integer, - id integer NOT NULL, - load_date timestamp without time zone, - school_district smallint, - special_purpose_district_1 character varying(10), - special_purpose_district_2 character varying(10), - zone_class_1 character varying(17), - zone_class_2 character varying(15), - zone_district_clssification_1 character varying(9), - zone_district_clssification_2 character varying(9), - zone_district_clssification_3 character varying(9), - zone_district_clssification_4 character varying(9) -); - - --- --- Name: borough; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE borough ( - id smallint NOT NULL, - description character varying(15) -); - - --- --- Name: building_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE building_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: building; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building ( - basement_description_code smallint, - bbl bigint, - bin bigint, - building_dimentions_id integer, - class character(2), - condo_number smallint, - count_all_units smallint, - count_residential_units smallint, - far numeric(6,2), - id integer DEFAULT nextval('building_id_seq'::regclass) NOT NULL, - load_date timestamp without time zone, - lot_id integer, - number_of_floors numeric(5,2), - proximity_code smallint, - year_altered smallint, - year_altered_previous smallint, - year_built smallint, - year_built_estimate character varying(5), - targeting_score float -); - - --- --- Name: building_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address ( - building_id integer NOT NULL, - address_id integer NOT NULL, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_address_bkup; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_address_bkup ( - building_id integer, - address_id integer, - pad_range_id integer, - house_number_sequence integer -); - - --- --- Name: building_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE building_dimentions ( - area integer, - id integer NOT NULL, - load_date timestamp without time zone, - primary_structure_depth numeric(6,2), - primary_structure_frontage numeric(6,2) -); - - --- --- Name: city_service; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE city_service ( - fire_company character varying(4), - id integer NOT NULL, - load_date timestamp without time zone, - police_precinct smallint, - sanitation_boro smallint, - sanitation_district smallint, - sanitation_subsection character varying(2) -); - - --- --- Name: document; Type: FOREIGN TABLE; Schema: public; Owner: - --- - -CREATE FOREIGN TABLE document ( - id integer NOT NULL, - created timestamp without time zone, - updated timestamp without time zone, - content_type character varying(255), - key uuid NOT NULL, - name character varying(255) NOT NULL, - tags character varying(255), - building_id integer -) -SERVER fdw_documents -OPTIONS ( - schema_name 'public', - table_name 'document' -); -ALTER FOREIGN TABLE document ALTER COLUMN id OPTIONS ( - column_name 'id' -); -ALTER FOREIGN TABLE document ALTER COLUMN created OPTIONS ( - column_name 'created' -); -ALTER FOREIGN TABLE document ALTER COLUMN updated OPTIONS ( - column_name 'updated' -); -ALTER FOREIGN TABLE document ALTER COLUMN content_type OPTIONS ( - column_name 'content_type' -); -ALTER FOREIGN TABLE document ALTER COLUMN key OPTIONS ( - column_name 'key' -); -ALTER FOREIGN TABLE document ALTER COLUMN name OPTIONS ( - column_name 'name' -); -ALTER FOREIGN TABLE document ALTER COLUMN tags OPTIONS ( - column_name 'tags' -); -ALTER FOREIGN TABLE document ALTER COLUMN building_id OPTIONS ( - column_name 'building_id' -); - - --- --- Name: finance; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE finance ( - assessed_land_value bigint, - assessed_lot_value bigint, - exempt_land_value bigint, - exempt_total_value bigint, - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE lot_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: lot; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot ( - administrative_deivision_id integer, - appointment_bbl bigint, - appointment_date timestamp without time zone, - areasource smallint, - bbl bigint, - block smallint, - boro_code character(2), - boro_id smallint, - city_service_id integer, - commerical_overlay_code_1 character varying(9), - commerical_overlay_code_2 character varying(10), - dof_tax_map_number integer, - e_designation_number character varying(11), - extenstion_code character varying(3), - finance_id integer, - historical_district character varying(40), - id integer DEFAULT nextval('lot_id_seq'::regclass) NOT NULL, - irregular_shape_flag boolean, - landmark_name character varying(35), - land_use_category_code smallint, - limited_height_district_code character varying(5), - load_date timestamp without time zone, - lot_dimentions_id integer, - lot_number smallint, - lot_owner_id integer, - number_of_buildings smallint, - number_of_easements smallint, - pluto_id integer, - split_zone_flag boolean, - type smallint, - zone_map_border_flag character varying(5) -); - - --- --- Name: lot_address; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_address ( - lot_id integer NOT NULL, - address_id integer NOT NULL -); - - --- --- Name: lot_dimentions; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_dimentions ( - allocated_factory_area integer, - allocated_garage_area integer, - allocated_office_area integer, - allocated_other_area integer, - allocated_residential_area integer, - allocated_retail_use integer, - allocated_storage_area integer, - area bigint, - area_commercial_use integer, - depth numeric(6,2), - far_commerical numeric(4,2), - far_facility numeric(4,2), - far_residential numeric(4,2), - frotage numeric(6,2), - id integer NOT NULL, - load_date timestamp without time zone -); - - --- --- Name: lot_owner; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE lot_owner ( - id integer NOT NULL, - load_date timestamp without time zone, - owner_name character varying(21), - owner_type character(1) -); - - --- --- Name: pluto; Type: TABLE; Schema: public; Owner: - --- - -CREATE TABLE pluto ( - dcp_map_number character varying(3), - id integer NOT NULL, - load_date timestamp without time zone, - pluto_map_id smallint, - pluto_version character varying(4), - sanborn_map_number character varying(8), - x_coord bigint, - y_coord bigint -); - - --- --- Name: vw_building_address; Type: MATERIALIZED VIEW; Schema: public; Owner: - --- - -CREATE MATERIALIZED VIEW vw_building_address AS - WITH address_mapping AS ( - SELECT DISTINCT b.bbl, - b.bin, - b.id AS building_id, - b.lot_id, - b.targeting_score, - ba.pad_range_id, - lh_1.min_sequence, - lh_1.max_sequence - FROM ((building b - JOIN building_address ba ON ((b.id = ba.building_id))) - JOIN ( SELECT building_address.building_id, - building_address.pad_range_id, - min(building_address.house_number_sequence) AS min_sequence, - max(building_address.house_number_sequence) AS max_sequence - FROM building_address - GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (((b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) - ) - SELECT DISTINCT aa.building_id, - aa.lot_id, - aa.bbl, - aa.bin, - aa.targeting_score, - (( - CASE - WHEN (lh.house_number_sequence = hh.house_number_sequence) THEN (lh.house_number)::text - ELSE (((lh.house_number)::text || ' -- '::text) || (hh.house_number)::text) - END || ' '::text) || (lh.street_name)::text) AS street_address, - lh.borough_id, - lh.zipcode, - lh.state, - aa.pad_range_id, - lh.id AS min_address_id, - hh.id AS max_address_id, - la.address_id AS lot_address_id - FROM (((address_mapping aa - JOIN address lh ON (((aa.pad_range_id = lh.pad_range_id) AND (aa.min_sequence = lh.house_number_sequence)))) - JOIN address hh ON (((aa.pad_range_id = hh.pad_range_id) AND (aa.max_sequence = hh.house_number_sequence)))) - LEFT JOIN lot_address la ON ((la.lot_id = aa.lot_id))) - ORDER BY aa.lot_id, aa.building_id - WITH NO DATA; - - --- --- Name: address id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address ALTER COLUMN id SET DEFAULT nextval('address_id_seq'::regclass); - - --- --- Name: address address_pkey1; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT address_pkey1 PRIMARY KEY (id); - - --- --- Name: administrative_division administrative_division_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY administrative_division - ADD CONSTRAINT administrative_division_pkey PRIMARY KEY (id); - - --- --- Name: borough borough_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY borough - ADD CONSTRAINT borough_pkey PRIMARY KEY (id); - - --- --- Name: building_address building_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building_address_pkey PRIMARY KEY (building_id, address_id); - - --- --- Name: building_dimentions building_dimentions_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_dimentions - ADD CONSTRAINT building_dimentions_pk PRIMARY KEY (id); - - --- --- Name: building building_pk; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_pk PRIMARY KEY (id); - - --- --- Name: city_service city_service_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY city_service - ADD CONSTRAINT city_service_pkey PRIMARY KEY (id); - - --- --- Name: finance finance_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY finance - ADD CONSTRAINT finance_pkey PRIMARY KEY (id); - - --- --- Name: lot_address lot_address_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot_address_pkey PRIMARY KEY (lot_id, address_id); - - --- --- Name: lot_dimentions lot_dimentions_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_dimentions - ADD CONSTRAINT lot_dimentions_pkey PRIMARY KEY (id); - - --- --- Name: lot_owner lot_owner_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_owner - ADD CONSTRAINT lot_owner_pkey PRIMARY KEY (id); - - --- --- Name: lot lot_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_pkey PRIMARY KEY (id); - - --- --- Name: pluto pluto_pkey; Type: CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY pluto - ADD CONSTRAINT pluto_pkey PRIMARY KEY (id); - - --- --- Name: address__borough_id_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__borough_id_IDX" ON address USING btree (borough_id); - - --- --- Name: address__full_address_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__full_address_IDX" ON address USING btree (btrim((((house_number)::text || ' '::text) || (street_name)::text))); - - --- --- Name: address__range_seq_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__range_seq_IDX" ON address USING btree (pad_range_id, house_number_sequence); - - --- --- Name: address__street_address_vector_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX address__street_address_vector_idx ON address USING gin (street_address); - - --- --- Name: address__zipcode_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX "address__zipcode_IDX" ON address USING btree (zipcode); - - --- --- Name: building_bbl_bin_unique_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "building_bbl_bin_unique_IDX" ON building USING btree (bbl, bin); - - --- --- Name: lot__bbl_IDX; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX "lot__bbl_IDX" ON lot USING btree (bbl); - - --- --- Name: lot__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX lot__bbl_idx ON lot USING btree (bbl); - - --- --- Name: vw_building_address__bbl_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__bbl_idx ON vw_building_address USING btree (bbl); - - --- --- Name: vw_building_address__building_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE INDEX vw_building_address__building_id_idx ON vw_building_address USING btree (building_id); - - --- --- Name: vw_building_address__pad_range_id_idx; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX vw_building_address__pad_range_id_idx ON vw_building_address USING btree (pad_range_id); - - --- --- Name: building_address address__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT address__building_address_fk FOREIGN KEY (address_id) REFERENCES address(id) ON DELETE SET NULL; - - --- --- Name: lot_address address__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT address__lot_address_fk FOREIGN KEY (address_id) REFERENCES address(id); - - --- --- Name: lot administrative_division_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT administrative_division_lot_fk FOREIGN KEY (administrative_deivision_id) REFERENCES administrative_division(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: address borough__address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY address - ADD CONSTRAINT borough__address_fk FOREIGN KEY (borough_id) REFERENCES borough(id); - - --- --- Name: building_address building__building_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building_address - ADD CONSTRAINT building__building_address_fk FOREIGN KEY (building_id) REFERENCES building(id); - - --- --- Name: building building_dimentions_building_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_dimentions_building_fk FOREIGN KEY (building_dimentions_id) REFERENCES building_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: building building_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY building - ADD CONSTRAINT building_lot_fk FOREIGN KEY (lot_id) REFERENCES lot(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot city_service_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT city_service_lot_fk FOREIGN KEY (city_service_id) REFERENCES city_service(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot finance_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT finance_lot_fk FOREIGN KEY (finance_id) REFERENCES finance(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot_address lot__lot_address_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot_address - ADD CONSTRAINT lot__lot_address_fk FOREIGN KEY (lot_id) REFERENCES lot(id); - - --- --- Name: lot lot_dimentions_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_dimentions_lot_fk FOREIGN KEY (lot_dimentions_id) REFERENCES lot_dimentions(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot lot_owner_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT lot_owner_lot_fk FOREIGN KEY (lot_owner_id) REFERENCES lot_owner(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- Name: lot pluto_lot_fk; Type: FK CONSTRAINT; Schema: public; Owner: - --- - -ALTER TABLE ONLY lot - ADD CONSTRAINT pluto_lot_fk FOREIGN KEY (pluto_id) REFERENCES pluto(id) DEFERRABLE INITIALLY DEFERRED; - - --- --- PostgreSQL database dump complete --- - diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql new file mode 100644 index 0000000000000000000000000000000000000000..5b0a08737e7cba65e5de57814b6a980820cae892 --- /dev/null +++ b/Building/ddl/vw_building_address.sql @@ -0,0 +1,51 @@ +create materialized view vw_building_address as + WITH address_mapping AS ( + SELECT DISTINCT b.bbl, + b.bin, + b.id AS building_id, + b.lot_id, + b.targeting_score, + ba.place_name, + ba.pad_range_id, + lh_1.min_sequence, + lh_1.max_sequence + FROM ((building b + JOIN building_address ba ON ((b.id = ba.building_id))) + JOIN (SELECT building_address.building_id, + building_address.pad_range_id, + min(building_address.house_number_sequence) AS min_sequence, + max(building_address.house_number_sequence) AS max_sequence + FROM building_address + GROUP BY building_address.building_id, building_address.pad_range_id) lh_1 ON (( + (b.id = lh_1.building_id) AND (ba.pad_range_id = lh_1.pad_range_id)))) + ) + SELECT DISTINCT am.building_id, + am.lot_id, + am.bbl, + am.bin, + am.targeting_score, + am.place_name, + (( + CASE + WHEN (lh.house_number_sequence = hh.house_number_sequence) THEN (lh.house_number) :: text + ELSE (((lh.house_number) :: text || ' -- ' :: text) || (hh.house_number) :: text) + END || ' ' :: text) || (lh.street_name) :: text) AS street_address, -- Generate Address range from sequence + lh.borough_id, + lh.zipcode, + lh.state, + am.pad_range_id, + lh.id AS min_address_id, + hh.id AS max_address_id, + la.address_id AS lot_address_id + FROM (((address_mapping am + JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) + JOIN address hh ON (((am.pad_range_id = hh.pad_range_id) AND (am.max_sequence = hh.house_number_sequence)))) + LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) + ORDER BY am.lot_id, am.building_id; + +-- CREATE UNIQUE INDEX sales_summary_seller -- Add index for concurrent refresh view +-- ON vw_building_address (lot_id, building_id, place_name); + +alter materialized view vw_building_address + owner to blocpower; + diff --git a/Building/erd/OLTP Building ERD.architect b/Building/erd/OLTP Building ERD.architect deleted file mode 100644 index e07025d081a8cc8128fc90fdd72bac5318651ea9..0000000000000000000000000000000000000000 --- a/Building/erd/OLTP Building ERD.architect +++ /dev/null @@ -1,1271 +0,0 @@ - - - OLTP BUuilding ERD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/Building/erd/OLTP Building ERD.architect~ b/Building/erd/OLTP Building ERD.architect~ deleted file mode 100644 index ca98f88fd4d9ffe34d78170daa67c5933aa39d5e..0000000000000000000000000000000000000000 --- a/Building/erd/OLTP Building ERD.architect~ +++ /dev/null @@ -1,1242 +0,0 @@ - - - OLTP BUuilding ERD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/Building/pad_based/add_bin_addresses/get_building.sql b/Building/pad_based/add_bin_addresses/get_building.sql index 0d8c5497ab73b30f03a53510de2f4a31c338fe45..33a9e697b69ae0d32ecec14f9bfb9a7cc6054157 100644 --- a/Building/pad_based/add_bin_addresses/get_building.sql +++ b/Building/pad_based/add_bin_addresses/get_building.sql @@ -259,7 +259,7 @@ select * from get_building( NULL,null,872208) select * from get_building( null,1122410) - +-- This version of building search is deprecated and no longer being used so get_building is to be deprecated create or replace function building_search( in_bbl bigint DEFAULT NULL, in_building_id integer DEFAULT NULL, @@ -401,4 +401,4 @@ select ascii('100A') --49 select '100A'::bytea, '100Z'::bytea -select * from lot where bbl = 1000151001 \ No newline at end of file +select * from lot where bbl = 1000151001