diff --git a/Building/ddl/address.sql b/Building/ddl/address.sql new file mode 100644 index 0000000000000000000000000000000000000000..ef7ceedc7a0eb96fb3d07a714f2092d5844f8d11 --- /dev/null +++ b/Building/ddl/address.sql @@ -0,0 +1,50 @@ +create table address +( + borough_id smallint + constraint borough__address_fk + references borough, + house_number varchar(20), + house_number_sequence integer, + id serial not null + constraint address_pkey1 + primary key, + load_date date default now(), + pad_range_id integer, + street_name varchar(50), + state char(2), + zipcode numeric(5), + street_address tsvector +) +; + +alter table address owner to blocpower +; + +create index "address__full_address_IDX" + on address (btrim((house_number::text || ' '::text) || street_name::text)) +; + +create index "address__borough_id_IDX" + on address (borough_id) +; + +create index "address__zipcode_IDX" + on address (zipcode) +; + +create index "address__range_seq_IDX" + on address (pad_range_id, house_number_sequence) +; + +create index address__street_address_idx + on address (street_address) +; + +create index address__zip_idx + on address (zipcode) +; + +create index address__bourough_id_idx + on address (borough_id) +; + diff --git a/Building/ddl/building.sql b/Building/ddl/building.sql new file mode 100644 index 0000000000000000000000000000000000000000..348e6af68f90660414ab4610b4cfcb2dd763b8c4 --- /dev/null +++ b/Building/ddl/building.sql @@ -0,0 +1,43 @@ +create table building +( + basement_description_code smallint, + bbl bigint, + bin bigint, + building_dimentions_id integer + constraint building_dimentions_building_fk + references building_dimentions + deferrable initially deferred, + class char(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 + constraint building_pk + primary key, + load_date timestamp, + lot_id integer + constraint building_lot_fk + references lot + deferrable initially deferred, + number_of_floors numeric(5,2), + proximity_code smallint, + year_altered smallint, + year_altered_previous smallint, + year_built smallint, + year_built_estimate varchar(5), + construction_type_id smallint + constraint tax_construction_type_building_fk + references construction_type, + targeting_score double precision, + place_name varchar(255) +) +; + +alter table building owner to blocpower +; + +create unique index "building_bbl_bin_unique_IDX" + on building (bbl, bin) +; + diff --git a/Building/ddl/building_address.sql b/Building/ddl/building_address.sql new file mode 100644 index 0000000000000000000000000000000000000000..5c4baed10c1ee8cabc36a4d05cd98c3b6e73dbb7 --- /dev/null +++ b/Building/ddl/building_address.sql @@ -0,0 +1,20 @@ +create table building_address +( + building_id integer not null + constraint building__building_address_fk + references building, + address_id integer not null + constraint address__building_address_fk + references address + on delete set null, + pad_range_id integer, + house_number_sequence integer, + place_name varchar(255), + constraint building_address_pkey + primary key (building_id, address_id) +) +; + +alter table building_address owner to blocpower +; + diff --git a/Building/ddl/building_functions.sql b/Building/ddl/building_functions.sql index cc04947ae0e2259956358c261860a3d2355c8e68..a66f40b9ad5dcd5227cf09f827612307bd524c18 100644 --- a/Building/ddl/building_functions.sql +++ b/Building/ddl/building_functions.sql @@ -1,62 +1,57 @@ --- 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; +-- create_building() +create function create_building( + in_place_name character varying DEFAULT NULL::character varying) + returns TABLE( + building_id integer, -- insert building + lot_id integer, -- generate it? + bbl bigint, -- generate it? + bin bigint, -- generate it? + street_address text, -- generate it? + borough character varying, -- generate it? + zipcode numeric, -- generate it? + state character, -- gerenate it? + targeting_score double precision, -- generate it? + place_name VARCHAR) -- insert place_name +language plpgsql +as $$ +DECLARE + in_place_name ALIAS for $1; + new_address_id integer; + new_lot_id integer; + new_building_id integer; + BEGIN + IF (in_place_name is not null)then + -- insert new address + insert into address(borough_id, house_number, house_number_sequence, pad_range_id) + values (1,'0000',1,1) + returning id into new_address_id; + + -- insert new lot + insert into lot (boro_id,number_of_buildings,load_date) + values (1,1,now()) + returning id into new_lot_id; + + -- insert new lot address + insert into lot_address + values (new_lot_id,new_address_id); + + -- insert new building + insert into building(load_date, lot_id) + values (now(),new_lot_id) + returning id into new_building_id; + + -- insert new building address + insert into building_address(building_id, address_id, place_name, pad_range_id, house_number_sequence) + values (new_building_id, new_address_id, in_place_name, 1, 1); + + return QUERY + select new_building_id as building_id, new_lot_id as lot_id, null::bigint as bbl,null::bigint as bin, null::text as street_address, null::character varying as borough, null::numeric as zipcode, null::character as state, null::double precision as targeting_score, in_place_name as place_name; + END IF; + END; +$$; + +alter function create_building(varchar) + owner to blocpower; /* TEST CASES: create building @@ -64,8 +59,9 @@ 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'); +select * from create_building(in_place_name := '838 Park Place, Brooklyn, New York 11216, United States'); */ + -- read_building() create function read_building( in_building_id integer DEFAULT NULL::integer, @@ -88,7 +84,7 @@ DECLARE 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 + return QUERY -- Building ID Look up select v.building_id, v.lot_id, v.bbl, @@ -136,10 +132,8 @@ 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(in_place_name := '838 Park Place, Brooklyn, New York 11216, United States'); --using named param select * from read_building() limit 100 --selcting all buildings; */ - --- TODO: update_building() diff --git a/Building/ddl/lot.sql b/Building/ddl/lot.sql new file mode 100644 index 0000000000000000000000000000000000000000..772f150889101afabc0106b98022edbcea3c4332 --- /dev/null +++ b/Building/ddl/lot.sql @@ -0,0 +1,73 @@ +create table lot +( + administrative_deivision_id integer + constraint administrative_division_lot_fk + references administrative_division + deferrable initially deferred, + appointment_bbl bigint, + appointment_date timestamp, + areasource smallint, + bbl bigint, + block smallint, + boro_code char(2), + boro_id smallint, + city_service_id integer + constraint city_service_lot_fk + references city_service + deferrable initially deferred, + commerical_overlay_code_1 varchar(9), + commerical_overlay_code_2 varchar(10), + dof_tax_map_number integer, + e_designation_number varchar(11), + extenstion_code varchar(3), + finance_id integer + constraint finance_lot_fk + references finance + deferrable initially deferred, + historical_district varchar(40), + id integer default nextval('lot_id_seq'::regclass) not null + constraint lot_pkey + primary key, + irregular_shape_flag boolean, + landmark_name varchar(35), + land_use_category_code smallint, + limited_height_district_code varchar(5), + load_date timestamp, + lot_dimentions_id integer + constraint lot_dimentions_lot_fk + references lot_dimentions + deferrable initially deferred, + lot_number smallint, + lot_owner_id integer + constraint lot_owner_lot_fk + references lot_owner + deferrable initially deferred, + number_of_buildings smallint, + number_of_easements smallint, + pluto_id integer + constraint pluto_lot_fk + references pluto + deferrable initially deferred, + split_zone_flag boolean, + type smallint, + zone_map_border_flag varchar(5), + hdc_financed boolean, + housing_code_violations_id integer + constraint housing_code_violations_lot_fk + references housing_code_violations, + owner_occupied boolean, + rent_stabilized boolean +) +; + +alter table lot owner to blocpower +; + +create unique index "lot__bbl_IDX" + on lot (bbl) +; + +create index lot__bbl_idx + on lot (bbl) +; + diff --git a/Building/ddl/lot_address.sql b/Building/ddl/lot_address.sql new file mode 100644 index 0000000000000000000000000000000000000000..b3860fc485c4ab6b599e80490a2e1ea865414aac --- /dev/null +++ b/Building/ddl/lot_address.sql @@ -0,0 +1,16 @@ +create table lot_address +( + lot_id integer not null + constraint lot__lot_address_fk + references lot, + address_id integer not null + constraint address__lot_address_fk + references address, + constraint lot_address_pkey + primary key (lot_id, address_id) +) +; + +alter table lot_address owner to blocpower +; + diff --git a/Building/ddl/vw_building_address.sql b/Building/ddl/vw_building_address.sql index 5b0a08737e7cba65e5de57814b6a980820cae892..6dd8b6f4040235ccbce6231515400074666ab33f 100644 --- a/Building/ddl/vw_building_address.sql +++ b/Building/ddl/vw_building_address.sql @@ -9,38 +9,41 @@ create materialized view vw_building_address as 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)))) + FROM ( + (building b JOIN building_address ba ON ((b.id = ba.building_id))) -- From building b joined to building address ba on building id = building address building id + JOIN (SELECT building_address.building_id, -- Join to select from building address grouped by + 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, + SELECT DISTINCT am.building_id, -- building id from address mapping + am.lot_id, -- lot_id from address mapping + am.bbl, -- bbl from address mapping + am.bin, -- bin from address mapping + am.targeting_score, -- targeting score from address mapping + am.place_name, -- place name from address mapping (( 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 + lh.borough_id, -- borough id from low house address + lh.zipcode, -- zipcode from low house address + lh.state, -- state from low house address + am.pad_range_id, -- pad_range_id from address mapping + lh.id AS min_address_id, -- min address id from low house address + hh.id AS max_address_id, -- max address id + la.address_id AS lot_address_id -- 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))) + JOIN address lh ON (((am.pad_range_id = lh.pad_range_id) AND (am.min_sequence = lh.house_number_sequence)))) -- low house address on am + JOIN address hh ON (((am.pad_range_id = hh.pad_range_id) AND (am.max_sequence = hh.house_number_sequence)))) -- high house address + LEFT JOIN lot_address la ON ((la.lot_id = am.lot_id))) -- join lot_address with address mapping ORDER BY am.lot_id, am.building_id; -- CREATE UNIQUE INDEX sales_summary_seller -- Add index for concurrent refresh view