From 7b3d0d73557e813d0d59096610481a9e5d410f9c Mon Sep 17 00:00:00 2001 From: Jacob Wentz Date: Mon, 26 Feb 2018 15:57:54 -0500 Subject: [PATCH 1/5] Adds MVs for NIBSR Data so that it can be viewed at the Regional Levels --- crime_data/app.py | 1 + crime_data/common/cdemodels.py | 345 +++++++++++++++++- crime_data/common/marshmallow_schemas.py | 54 +++ crime_data/resources/nibrs_counts.py | 62 ++++ dba/create_new/offender_nibrs_rollup_mv.sql | 130 +++++++ .../offense_counts_nibrs_rollup_mv.sql | 14 +- dba/create_new/victim_nibrs_rollup_mv.sql | 126 +++++++ .../victim_offender_relationship_mv.sql | 36 ++ 8 files changed, 765 insertions(+), 3 deletions(-) diff --git a/crime_data/app.py b/crime_data/app.py index b43f312..6880798 100644 --- a/crime_data/app.py +++ b/crime_data/app.py @@ -256,6 +256,7 @@ def output_csv(data, code, headers=None): api.add_resource(crime_data.resources.meta.StateLK,'/lookup/state') api.add_resource(crime_data.resources.nibrs_counts.NIBRSCountNational, '/nibrs///national/') + api.add_resource(crime_data.resources.nibrs_counts.NIBRSCountRegion, '/nibrs///regions//') api.add_resource(crime_data.resources.nibrs_counts.NIBRSCountState, '/nibrs///states//') api.add_resource(crime_data.resources.nibrs_counts.NIBRSCountAgency, '/nibrs///agency//') diff --git a/crime_data/common/cdemodels.py b/crime_data/common/cdemodels.py index 117893f..9563dc5 100644 --- a/crime_data/common/cdemodels.py +++ b/crime_data/common/cdemodels.py @@ -1471,8 +1471,6 @@ def get(ori=None,offense_name=None): NIBRSAgencyOffenderDenormEthnicity_index = Index('ori_index_nibrs_agency_denorm_offender_ethnicity', NIBRSAgencyOffenderDenormEthnicity.ori) - - class NIBRSAgencyOffenderDenormAge(db.Model): """Represents Agency Level NIBRS Offender Age Data""" __tablename__ = 'nibrs_agency_denorm_offender_age' @@ -1506,6 +1504,197 @@ def get(ori=None,offense_name=None): NIBRSAgencyOffenderDenormAge_index = Index('ori_index_nibrs_agency_denorm_offender_age', NIBRSAgencyOffenderDenormAge.ori) +class NIBRSRegionVictimDenormCount(db.Model): + """Represents Region Level NIBRS Victim Count Data""" + __tablename__ = 'nibrs_region_denorm_victim_count' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormCount.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormCount.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormCount.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + count = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionVictimDenormSex(db.Model): + """Represents Region Level NIBRS Victim Data""" + __tablename__ = 'nibrs_region_denorm_victim_sex' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormSex.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormSex.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormSex.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + male_count = db.Column(db.Integer) + female_count = db.Column(db.Integer) + unknown_count = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionVictimDenormRace(db.Model): + """Represents Region Level NIBRS Victim Race Data""" + __tablename__ = 'nibrs_region_denorm_victim_race' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormRace.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormRace.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormRace.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + asian = db.Column(db.Integer) + native_hawaiian = db.Column(db.Integer) + black = db.Column(db.Integer) + american_indian = db.Column(db.Integer) + unknown = db.Column(db.Integer) + white = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionVictimDenormEthnicity(db.Model): + """Represents Region Level NIBRS Victim Ethnicity Data""" + __tablename__ = 'nibrs_region_denorm_victim_ethnicity' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormEthnicity.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormEthnicity.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormEthnicity.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + hispanic = db.Column(db.Integer) + multiple = db.Column(db.Integer) + not_hispanic = db.Column(db.Integer) + unknown = db.Column(db.Integer) + data_year = db.Column(db.Integer) + + +class NIBRSRegionVictimDenormAge(db.Model): + """Represents Region Level NIBRS Victim Age Data""" + __tablename__ = 'nibrs_region_denorm_victim_age' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormAge.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormAge.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormAge.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + range_0_9 = db.Column(db.Integer) + range_10_19 = db.Column(db.Integer) + range_20_29 = db.Column(db.Integer) + range_30_39 = db.Column(db.Integer) + range_40_49 = db.Column(db.Integer) + range_50_59 = db.Column(db.Integer) + range_60_69 = db.Column(db.Integer) + range_70_79 = db.Column(db.Integer) + range_80_89 = db.Column(db.Integer) + range_90_99 = db.Column(db.Integer) + unknown = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionVictimDenormLocation(db.Model): + """Represents Region Level NIBRS Victim Location Data""" + __tablename__ = 'nibrs_region_denorm_victim_location' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionVictimDenormLocation.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionVictimDenormLocation.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionVictimDenormLocation.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + residence_home = db.Column(db.Integer) + parking_garage__lot = db.Column(db.Integer) + abandoned_condemned__structure = db.Column(db.Integer) + air__bus__train_terminal = db.Column(db.Integer) + amusement_park = db.Column(db.Integer) + arena__stadium__fairgrounds = db.Column(db.Integer) + atm_separate_from_bank = db.Column(db.Integer) + auto_dealership = db.Column(db.Integer) + bank = db.Column(db.Integer) + bar_nightclub = db.Column(db.Integer) + campground = db.Column(db.Integer) + church__synagogue__temple__mosque = db.Column(db.Integer) + commercial__office_building = db.Column(db.Integer) + community_center = db.Column(db.Integer) + construction_site = db.Column(db.Integer) + cyberspace = db.Column(db.Integer) + daycare_facility = db.Column(db.Integer) + department__discount_store = db.Column(db.Integer) + dock__wharf__shipping_terminal = db.Column(db.Integer) + drug_store__doctors_office__hospital = db.Column(db.Integer) + farm_facility = db.Column(db.Integer) + field__woods = db.Column(db.Integer) + gambling_facility__casino__race_track = db.Column(db.Integer) + government__public_building = db.Column(db.Integer) + grocery_store = db.Column(db.Integer) + highway__alley__street__sidewalk = db.Column(db.Integer) + hotel__motel = db.Column(db.Integer) + industrial_site = db.Column(db.Integer) + jail__prison__corrections_facility = db.Column(db.Integer) + lake__waterway__beach = db.Column(db.Integer) + liquor_store = db.Column(db.Integer) + military_base = db.Column(db.Integer) + unknown = db.Column(db.Integer) + park__playground = db.Column(db.Integer) + rental_storage_facility = db.Column(db.Integer) + rest_area = db.Column(db.Integer) + restaurant = db.Column(db.Integer) + school__college = db.Column(db.Integer) + school_college__university = db.Column(db.Integer) + school_elementary__secondary = db.Column(db.Integer) + gas_station = db.Column(db.Integer) + mission__homeless_shelter = db.Column(db.Integer) + shopping_mall = db.Column(db.Integer) + specialty_store = db.Column(db.Integer) + tribal_lands = db.Column(db.Integer) + convenience_store = db.Column(db.Integer) + data_year = db.Column(db.Integer) class NIBRSStateOffenderDenormCount(db.Model): """Represents Agency Level NIBRS Offender Count Data""" @@ -1638,6 +1827,137 @@ def get(state_abbr=None,offense_name=None): unknown = db.Column(db.Integer) data_year = db.Column(db.Integer) +class NIBRSRegionOffenderDenormCount(db.Model): + """Represents Region Level NIBRS Offender Count Data""" + __tablename__ = 'nibrs_region_denorm_offender_count' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenderDenormCount.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenderDenormCount.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenderDenormCount.offense_name.in_(offense_name)) + + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + count = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionOffenderDenormSex(db.Model): + """Represents Region Level NIBRS Offender Data""" + __tablename__ = 'nibrs_region_denorm_offender_sex' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenderDenormSex.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenderDenormSex.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenderDenormSex.offense_name.in_(offense_name)) + + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + male_count = db.Column(db.Integer) + female_count = db.Column(db.Integer) + unknown_count = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionOffenderDenormRace(db.Model): + """Represents Region Level NIBRS Offender Race Data""" + __tablename__ = 'nibrs_region_denorm_offender_race' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenderDenormRace.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenderDenormRace.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenderDenormRace.offense_name.in_(offense_name)) + + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + asian = db.Column(db.Integer) + native_hawaiian = db.Column(db.Integer) + black = db.Column(db.Integer) + american_indian = db.Column(db.Integer) + unknown = db.Column(db.Integer) + white = db.Column(db.Integer) + data_year = db.Column(db.Integer) + +class NIBRSRegionOffenderDenormEthnicity(db.Model): + """Represents Region Level NIBRS Offender Ethnicity Data""" + __tablename__ = 'nibrs_region_denorm_offender_ethnicity' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenderDenormEthnicity.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenderDenormEthnicity.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenderDenormEthnicity.offense_name.in_(offense_name)) + + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + hispanic = db.Column(db.Integer) + multiple = db.Column(db.Integer) + not_hispanic = db.Column(db.Integer) + unknown = db.Column(db.Integer) + data_year = db.Column(db.Integer) + + +class NIBRSRegionOffenderDenormAge(db.Model): + """Represents Region Level NIBRS Offender Age Data""" + __tablename__ = 'nibrs_region_denorm_offender_age' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenderDenormAge.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenderDenormAge.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenderDenormAge.offense_name.in_(offense_name)) + + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + range_0_9 = db.Column(db.Integer) + range_10_19 = db.Column(db.Integer) + range_20_29 = db.Column(db.Integer) + range_30_39 = db.Column(db.Integer) + range_40_49 = db.Column(db.Integer) + range_50_59 = db.Column(db.Integer) + range_60_69 = db.Column(db.Integer) + range_70_79 = db.Column(db.Integer) + range_80_89 = db.Column(db.Integer) + range_90_99 = db.Column(db.Integer) + unknown = db.Column(db.Integer) + data_year = db.Column(db.Integer) + class NIBRSNationalOffenderDenormCount(db.Model): """Represents Agency Level NIBRS Offender Count Data""" __tablename__ = 'nibrs_national_denorm_offender_count' @@ -1934,6 +2254,27 @@ def get(state_abbr=None,offense_name=None): incident_count = db.Column(db.Integer) offense_count = db.Column(db.Integer) +class NIBRSRegionOffenseCount(db.Model): + """Represents Region Level NIBRS Offender Ethnicity Data""" + __tablename__ = 'nibrs_region_denorm_offense_count' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + def get(region_name=None,offense_name=None): + query = NIBRSRegionOffenseCount.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionOffenseCount.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionOffenseCount.offense_name.in_(offense_name)) + return query + region_code = db.Column(db.Integer) + offense_name = db.Column(db.String) + region_name = db.Column(db.String) + data_year = db.Column(db.Integer) + incident_count = db.Column(db.Integer) + offense_count = db.Column(db.Integer) + class PoliceEmploymentDataNation(db.Model): __tablename__ = 'police_employment_nation' __table_args__ = (UniqueConstraint('data_year'), ) diff --git a/crime_data/common/marshmallow_schemas.py b/crime_data/common/marshmallow_schemas.py index 23fb71e..5ed8b4d 100644 --- a/crime_data/common/marshmallow_schemas.py +++ b/crime_data/common/marshmallow_schemas.py @@ -1274,6 +1274,60 @@ class NIBRSStateOffenseCountSchema(ma.ModelSchema): class Meta: model = cdemodels.NIBRSStateOffenseCount ordered = True +class NIBRSRegionVictimDenormCountSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormCount + ordered = True + +class NIBRSRegionVictimDenormSexSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormSex + ordered = True + +class NIBRSRegionVictimDenormRaceSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormRace + ordered = True + +class NIBRSRegionVictimDenormEthnicitySchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormEthnicity + ordered = True + +class NIBRSRegionVictimDenormAgeSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormAge + ordered = True + +class NIBRSRegionVictimDenormLocationSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionVictimDenormLocation + ordered = True + +class NIBRSRegionOffenderDenormCountSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenderDenormCount + ordered = True + +class NIBRSRegionOffenderDenormSexSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenderDenormSex + ordered = True + +class NIBRSRegionOffenderDenormRaceSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenderDenormRace + ordered = True + +class NIBRSRegionOffenderDenormAgeSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenderDenormAge + ordered = True + +class NIBRSRegionOffenseCountSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenseCount + ordered = True class PoliceEmploymentDataNationSchema(ma.ModelSchema): class Meta: diff --git a/crime_data/resources/nibrs_counts.py b/crime_data/resources/nibrs_counts.py index b551199..694d72b 100644 --- a/crime_data/resources/nibrs_counts.py +++ b/crime_data/resources/nibrs_counts.py @@ -69,6 +69,68 @@ def get(self, args, variable,queryType, offense_name=None): ui = creator.munge_set() return self.without_metadata(ui, args) +class NIBRSCountRegion(CdeResource): + @use_args(ArgumentsSchema) + @cache(max_age=DEFAULT_MAX_AGE, public=True) + def get(self, args, variable,queryType,region_name=None, offense_name=None): + self.verify_api_key(args) + if queryType == 'victim' and variable == 'count': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormCountSchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormCount.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_count_victim','') + elif queryType == 'victim' and variable == 'sex': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormSexSchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormSex.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_sex_victim','') + elif queryType =='victim' and variable == 'race': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormRaceSchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormRace.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_race_victim','') + elif queryType == 'victim' and variable == 'ethnicity': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormEthnicitySchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormEthnicity.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_ethnicity_victim','') + elif queryType == 'victim' and variable == 'age': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormAgeSchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormAge.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_age_victim','') + elif queryType == 'victim' and variable == 'location': + self.set_schema(marshmallow_schemas.NIBRSRegionVictimDenormLocationSchema(many=True)) + query = cdemodels.NIBRSRegionVictimDenormLocation.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_location','') + elif queryType == 'offender' and variable == 'count': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenderDenormCountSchema(many=True)) + query = cdemodels.NIBRSRegionOffenderDenormCount.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_count_offender','') + elif queryType == 'offender' and variable == 'sex': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenderDenormSexSchema(many=True)) + query = cdemodels.NIBRSRegionOffenderDenormSex.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_sex_offender','') + elif queryType == 'offender' and variable == 'race': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenderDenormRaceSchema(many=True)) + query = cdemodels.NIBRSRegionOffenderDenormRace.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_race_offender','') + elif queryType == 'offender' and variable == 'ethnicity': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenderDenormEthnicitySchema(many=True)) + query = cdemodels.NIBRSRegionOffenderDenormEthnicity.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_ethnicity_offender','') + elif queryType == 'offender' and variable == 'age': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenderDenormAgeSchema(many=True)) + query = cdemodels.NIBRSRegionOffenderDenormAge.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_age_offender','') + elif queryType == 'offense' and variable == 'count': + self.set_schema(marshmallow_schemas.NIBRSRegionOffenseCountSchema(many=True)) + query = cdemodels.NIBRSRegionOffenseCount.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_offense_count','') + elif queryType == 'victim' and variable == 'relationships': + self.set_schema(marshmallow_schemas.NIBRSRegionDenormVictimOffenderRelationshipSchema(many=True)) + query = cdemodels.NIBRSRegionDenormVictimOffenderRelationship.get(region_name=region_name,offense_name=get_offenses(offense_name)) + creator = munger.UIComponentCreator(query.all(),'nibrs_relatiopnship','') + else: + return self.with_metadata([], args) + ui = creator.munge_set() + return self.without_metadata(ui, args) + class NIBRSCountState(CdeResource): @use_args(ArgumentsSchema) diff --git a/dba/create_new/offender_nibrs_rollup_mv.sql b/dba/create_new/offender_nibrs_rollup_mv.sql index 754dfa3..435c2c2 100644 --- a/dba/create_new/offender_nibrs_rollup_mv.sql +++ b/dba/create_new/offender_nibrs_rollup_mv.sql @@ -174,6 +174,136 @@ coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown from public.nibrs_offender_count group by agency_id, ori, offense_name, data_year; +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_sex AS +select region_code as region_code, +region_name as region_name, +offense_name as offense_name, +data_year as data_year, +coalesce(sum(case when sex_code = 'M' then count end), 0) as male_count, +coalesce(sum(case when sex_code = 'F' then count end), 0) as female_count, +coalesce(sum(case when sex_code = 'U' then count end), 0) as unknown_count +from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_count AS +select region_code as region_code, +region_name as region_name, +offense_name as offense_name, +data_year as data_year, +sum(count) as count +from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_race AS +select region_code as region_code, +region_name as region_name, +offense_name as offense_name, +data_year as data_year, +coalesce(sum(case when race_desc = 'Asian' then count end), 0) as asian, +coalesce(sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +coalesce(sum(case when race_desc = 'Black or African American' then count end), 0) as black, +coalesce(sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +coalesce(sum(case when race_desc = 'Unknown' then count end), 0) as unknown, +coalesce(sum(case when race_desc = 'White' then count end), 0) as white +from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_ethnicity AS +select region_code as region_code, +region_name as region_name, +offense_name as offense_name, +data_year as data_year, +coalesce(sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, +coalesce(sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, +coalesce(sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +coalesce(sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown +from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_age AS +select region_code as region_code, +region_name as region_name, +offense_name as offense_name, +data_year as data_year, +coalesce(sum(case when age_range = '0-9' then count end), 0) as range_0_9, +coalesce(sum(case when age_range = '10-19' then count end), 0) as range_10_19, +coalesce(sum(case when age_range = '20-29' then count end), 0) as range_20_29, +coalesce(sum(case when age_range = '30-39' then count end), 0) as range_30_39, +coalesce(sum(case when age_range = '40-49' then count end), 0) as range_40_49, +coalesce(sum(case when age_range = '50-59' then count end), 0) as range_50_59, +coalesce(sum(case when age_range = '60-69' then count end), 0) as range_60_69, +coalesce(sum(case when age_range = '70-79' then count end), 0) as range_70_79, +coalesce(sum(case when age_range = '80-89' then count end), 0) as range_80_89, +coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, +coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown +from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; + + +--Region +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_sex AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +SUM(n.male_count) as male_count, +SUM(n.female_count) as female_count, +SUM(n.unknown_count) as unknown_count +from public.nibrs_state_denorm_offender_sex n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; + + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_count AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +sum(n.count) as count +from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_race AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.race_desc = 'Asian' then count end), 0) as asian, +coalesce(sum(case when n.race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +coalesce(sum(case when n.race_desc = 'Black or African American' then count end), 0) as black, +coalesce(sum(case when n.race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +coalesce(sum(case when n.race_desc = 'Unknown' then count end), 0) as unknown, +coalesce(sum(case when n.race_desc = 'White' then count end), 0) as white +from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_ethnicity AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, +coalesce(sum(case when n.ethnicity_name = 'Multiple' then count end), 0) as multiple, +coalesce(sum(case when n.ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +coalesce(sum(case when n.ethnicity_name = 'Unknown' then count end), 0) as unknown +from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_age AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.age_range = '0-9' then count end), 0) as range_0_9, +coalesce(sum(case when n.age_range = '10-19' then count end), 0) as range_10_19, +coalesce(sum(case when n.age_range = '20-29' then count end), 0) as range_20_29, +coalesce(sum(case when n.age_range = '30-39' then count end), 0) as range_30_39, +coalesce(sum(case when n.age_range = '40-49' then count end), 0) as range_40_49, +coalesce(sum(case when n.age_range = '50-59' then count end), 0) as range_50_59, +coalesce(sum(case when n.age_range = '60-69' then count end), 0) as range_60_69, +coalesce(sum(case when n.age_range = '70-79' then count end), 0) as range_70_79, +coalesce(sum(case when n.age_range = '80-89' then count end), 0) as range_80_89, +coalesce(sum(case when n.age_range = '90-99' then count end), 0) as range_90_99, +coalesce(sum(case when n.age_range = 'UNKNOWN' then count end), 0) as unknown +from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; UPDATE nibrs_offender_count SET state_abbr = TRIM(state_abbr),ori = TRIM(ori), diff --git a/dba/create_new/offense_counts_nibrs_rollup_mv.sql b/dba/create_new/offense_counts_nibrs_rollup_mv.sql index 21d3080..03a72b9 100644 --- a/dba/create_new/offense_counts_nibrs_rollup_mv.sql +++ b/dba/create_new/offense_counts_nibrs_rollup_mv.sql @@ -7,7 +7,7 @@ sum(offense_count) as offense_count from public.nibrs_offense_count group by offense_name, data_year; -CREATE MATERIALIZED VIEW nibrs_state_denorm_offense_count AS +CREATE MATERIALIZED VIEW nibrs_region_denorm_offense_count AS select state_id as state_id, state_abbr as state_abbr, offense_name as offense_name, @@ -25,6 +25,18 @@ sum(incident_count) as incident_count, sum(offense_count) as offense_count from public.nibrs_offense_count group by agency_id, ori, offense_name, data_year; +--region +CREATE MATERIALIZED VIEW nibrs_state_denorm_offense_count AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +sum(n.incident_count) as incident_count, +sum(n.offense_count) as offense_count +from public.nibrs_offense_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year;; + UPDATE nibrs_offense_count SET state_abbr = TRIM(state_abbr),ori = TRIM(ori), offense_name = TRIM(offense_name), diff --git a/dba/create_new/victim_nibrs_rollup_mv.sql b/dba/create_new/victim_nibrs_rollup_mv.sql index a11450e..40de28a 100644 --- a/dba/create_new/victim_nibrs_rollup_mv.sql +++ b/dba/create_new/victim_nibrs_rollup_mv.sql @@ -362,6 +362,132 @@ coalesce(sum(case when location = 'Convenience Store' then sum end), 0) as conve from public.nibrs_national_denorm_victim_location_temp group by offense_name, data_year; +--regions +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_sex AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +SUM(n.male_count) as male_count, +SUM(n.female_count) as female_count, +SUM(n.unknown_count) as unknown_count +from public.nibrs_state_denorm_victim_sex n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; + + +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_count AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +sum(n.count) as count +from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_race AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.race_desc = 'Asian' then count end), 0) as asian, +coalesce(sum(case when n.race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +coalesce(sum(case when n.race_desc = 'Black or African American' then count end), 0) as black, +coalesce(sum(case when n.race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +coalesce(sum(case when n.race_desc = 'Unknown' then count end), 0) as unknown, +coalesce(sum(case when n.race_desc = 'White' then count end), 0) as white +from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_ethnicity AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, +coalesce(sum(case when n.ethnicity_name = 'Multiple' then count end), 0) as multiple, +coalesce(sum(case when n.ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +coalesce(sum(case when n.ethnicity_name = 'Unknown' then count end), 0) as unknown +from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_age AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.age_range = '0-9' then count end), 0) as range_0_9, +coalesce(sum(case when n.age_range = '10-19' then count end), 0) as range_10_19, +coalesce(sum(case when n.age_range = '20-29' then count end), 0) as range_20_29, +coalesce(sum(case when n.age_range = '30-39' then count end), 0) as range_30_39, +coalesce(sum(case when n.age_range = '40-49' then count end), 0) as range_40_49, +coalesce(sum(case when n.age_range = '50-59' then count end), 0) as range_50_59, +coalesce(sum(case when n.age_range = '60-69' then count end), 0) as range_60_69, +coalesce(sum(case when n.age_range = '70-79' then count end), 0) as range_70_79, +coalesce(sum(case when n.age_range = '80-89' then count end), 0) as range_80_89, +coalesce(sum(case when n.age_range = '90-99' then count end), 0) as range_90_99, +coalesce(sum(case when n.age_range = 'UNKNOWN' then count end), 0) as unknown +from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_location AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.location = 'Residence/Home' then sum end), 0) as Residence_Home, +coalesce(sum(case when n.location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, +coalesce(sum(case when n.location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, +coalesce(sum(case when n.location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, +coalesce(sum(case when n.location = 'Amusement Park' then sum end), 0) as Amusement_Park, +coalesce(sum(case when n.location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, +coalesce(sum(case when n.location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, +coalesce(sum(case when n.location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, +coalesce(sum(case when n.location = 'Bank/Savings and Loan' then sum end), 0) as Bank, +coalesce(sum(case when n.location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, +coalesce(sum(case when n.location = 'Camp/Campground' then sum end), 0) as Campground, +coalesce(sum(case when n.location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, +coalesce(sum(case when n.location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, +coalesce(sum(case when n.location = 'Community Center' then sum end), 0) as Community_Center, +coalesce(sum(case when n.location = 'Construction Site' then sum end), 0) as Construction_Site, +coalesce(sum(case when n.location = 'Cyberspace' then sum end), 0) as Cyberspace, +coalesce(sum(case when n.location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, +coalesce(sum(case when n.location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, +coalesce(sum(case when n.location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, +coalesce(sum(case when n.location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, +coalesce(sum(case when n.location = 'Farm Facility' then sum end), 0) as Farm_Facility, +coalesce(sum(case when n.location = 'Field/Woods' then sum end), 0) as Field__Woods, +coalesce(sum(case when n.location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, +coalesce(sum(case when n.location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, +coalesce(sum(case when n.location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, +coalesce(sum(case when n.location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, +coalesce(sum(case when n.location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, +coalesce(sum(case when n.location = 'Industrial Site' then sum end), 0) as Industrial_Site, +coalesce(sum(case when n.location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, +coalesce(sum(case when n.location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, +coalesce(sum(case when n.location = 'Liquor Store' then sum end), 0) as Liquor_Store, +coalesce(sum(case when n.location = 'Military Installation' then sum end), 0) as Military_Base, +coalesce(sum(case when n.location = 'Other/Unknown' then sum end), 0) as Unknown, +coalesce(sum(case when n.location = 'Park/Playground' then sum end), 0) as Park__Playground, +coalesce(sum(case when n.location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, +coalesce(sum(case when n.location = 'Rest Area' then sum end), 0) as Rest_Area, +coalesce(sum(case when n.location = 'Restaurant' then sum end), 0) as Restaurant, +coalesce(sum(case when n.location = 'School/College' then sum end), 0) as School__College, +coalesce(sum(case when n.location = 'School-College/University' then sum end), 0) as School_College__University, +coalesce(sum(case when n.location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, +coalesce(sum(case when n.location = 'Service/Gas Station' then sum end), 0) as Gas_Station, +coalesce(sum(case when n.location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, +coalesce(sum(case when n.location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, +coalesce(sum(case when n.location = 'Specialty Store' then sum end), 0) as Specialty_Store, +coalesce(sum(case when n.location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, +coalesce(sum(case when n.location = 'Convenience Store' then sum end), 0) as convenience_store +from public.nibrs_national_denorm_victim_location_temp n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year; + + UPDATE nibrs_victim_count SET location = TRIM(location), victim_type_name= TRIM(victim_type_name), state_abbr = TRIM(state_abbr),ori = TRIM(ori), offense_name = TRIM(offense_name),sex_code = TRIM(sex_code), diff --git a/dba/create_new/victim_offender_relationship_mv.sql b/dba/create_new/victim_offender_relationship_mv.sql index 387184d..538f485 100644 --- a/dba/create_new/victim_offender_relationship_mv.sql +++ b/dba/create_new/victim_offender_relationship_mv.sql @@ -64,6 +64,42 @@ coalesce(sum(case when relationship = 'Victim Was Offender' then count end), 0) coalesce(sum(case when relationship = 'Victim was Ex-Spouse' then count end), 0) as ex_spouse from public.nibrs_victim_to_offender_relationship_count group by state_id, state_abbr, offense_name, data_year; +-- region +CREATE MATERIALIZED VIEW nibrs_region_denorm_victim_offender_relationship AS +select s.region_code as region_code, +r.region_name as region_name, +n.offense_name as offense_name, +n.data_year as data_year, +coalesce(sum(case when n.relationship = 'Victim Was Acquaintance' then count end), 0) as acquaintance, +coalesce(sum(case when n.relationship = 'Victim Was Babysittee' then count end), 0) as babysittee, +coalesce(sum(case when n.relationship = 'Victim Was Boyfriend/Girlfriend' then count end), 0) as boyfriend_girlfriend, +coalesce(sum(case when n.relationship = 'Victim Was Child of Boyfriend or Girlfriend' then count end), 0) as child_boyfriend_girlfriend, +coalesce(sum(case when n.relationship = 'Victim Was Child' then count end), 0) as child, +coalesce(sum(case when n.relationship = 'Victim Was Common-Law Spouse' then count end), 0) as common_law_spouse, +coalesce(sum(case when n.relationship = 'Victim was Employee' then count end), 0) as employee, +coalesce(sum(case when n.relationship = 'Victim was Employer' then count end), 0) as employer, +coalesce(sum(case when n.relationship = 'Victim Was Friend' then count end), 0) as friend, +coalesce(sum(case when n.relationship = 'Victim Was Grandchild' then count end), 0) as grandchild, +coalesce(sum(case when n.relationship = 'Victim Was Grandparent' then count end), 0) as grandparent, +coalesce(sum(case when n.relationship = 'Homosexual Relationship' then count end), 0) as homosexual_relationship, +coalesce(sum(case when n.relationship = 'Victim Was In-law' then count end), 0) as in_law, +coalesce(sum(case when n.relationship = 'Victim Was Neighbor' then count end), 0) as neighbor, +coalesce(sum(case when n.relationship = 'Victim Was Other Family Member' then count end), 0) as other_family_member, +coalesce(sum(case when n.relationship = 'Victim was Otherwise Known' then count end), 0) as otherwise_known, +coalesce(sum(case when n.relationship = 'Victim Was Parent' then count end), 0) as parent, +coalesce(sum(case when n.relationship = 'Relationship Unknown' then count end), 0) as relationship_unknown, +coalesce(sum(case when n.relationship = 'Victim Was Sibling' then count end), 0) as sibling, +coalesce(sum(case when n.relationship = 'Victim Was Stepchild' then count end), 0) as stepchild, +coalesce(sum(case when n.relationship = 'Victim Was Spouse' then count end), 0) as spouse, +coalesce(sum(case when n.relationship = 'Victim Was Stepparent' then count end), 0) as stepparent, +coalesce(sum(case when n.relationship = 'Victim Was Stepsibling' then count end), 0) as stepsibling, +coalesce(sum(case when n.relationship = 'Victim Was Stranger' then count end), 0) as stranger, +coalesce(sum(case when n.relationship = 'Victim Was Offender' then count end), 0) as offender, +coalesce(sum(case when n.relationship = 'Victim was Ex-Spouse' then count end), 0) as ex_spouse +from public.nibrs_victim_to_offender_relationship_count n, public.state_lk s,public.region_lk r +where s.region_code = r.region_code +group by s.region_code,r.region_name, n.offense_name, n.data_year;; + CREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_offender_relationship AS select agency_id as agency_id, ori as ori, From d1d8b621e3683e457cc768577008f44bf76e31bb Mon Sep 17 00:00:00 2001 From: Jacob Wentz Date: Tue, 27 Feb 2018 09:10:28 -0500 Subject: [PATCH 2/5] Fix to Region Relationship view --- crime_data/common/cdemodels.py | 47 ++++++++++++++++++++++++ crime_data/common/marshmallow_schemas.py | 10 +++++ 2 files changed, 57 insertions(+) diff --git a/crime_data/common/cdemodels.py b/crime_data/common/cdemodels.py index 9563dc5..bbc2f5b 100644 --- a/crime_data/common/cdemodels.py +++ b/crime_data/common/cdemodels.py @@ -2191,6 +2191,53 @@ def get(state_abbr=None,offense_name=None): ex_spouse = db.Column(db.Integer) data_year = db.Column(db.Integer) +class NIBRSRegionDenormVictimOffenderRelationship(db.Model): + """Represents Region Level NIBRS Victim Data""" + __tablename__ = 'nibrs_region_denorm_victim_offender_relationship' + __table_args__ = ( + PrimaryKeyConstraint('offense_name', 'data_year','region_name'), + ) + + def get(region_name=None,offense_name=None): + query = NIBRSRegionDenormVictimOffenderRelationship.query + + if region_name: + query = query.filter(func.lower(NIBRSRegionDenormVictimOffenderRelationship.region_name) == func.lower(region_name)) + if offense_name: + query = query.filter(NIBRSRegionDenormVictimOffenderRelationship.offense_name.in_(offense_name)) + return query + + region_code = db.Column(db.Integer) + region_name = db.Column(db.String) + offense_name = db.Column(db.String) + acquaintance = db.Column(db.Integer) + babysittee = db.Column(db.Integer) + boyfriend_girlfriend = db.Column(db.Integer) + child_boyfriend_girlfriend = db.Column(db.Integer) + child = db.Column(db.Integer) + common_law_spouse = db.Column(db.Integer) + employee = db.Column(db.Integer) + employer = db.Column(db.Integer) + friend = db.Column(db.Integer) + grandchild = db.Column(db.Integer) + grandparent = db.Column(db.Integer) + homosexual_relationship = db.Column(db.Integer) + in_law = db.Column(db.Integer) + neighbor = db.Column(db.Integer) + other_family_member = db.Column(db.Integer) + otherwise_known = db.Column(db.Integer) + parent = db.Column(db.Integer) + relationship_unknown = db.Column(db.Integer) + sibling = db.Column(db.Integer) + stepchild = db.Column(db.Integer) + spouse = db.Column(db.Integer) + stepparent = db.Column(db.Integer) + stepsibling = db.Column(db.Integer) + stranger = db.Column(db.Integer) + offender = db.Column(db.Integer) + ex_spouse = db.Column(db.Integer) + data_year = db.Column(db.Integer) + class NIBRSNationalOffenseCount(db.Model): """Represents Agency Level NIBRS Offender Ethnicity Data""" diff --git a/crime_data/common/marshmallow_schemas.py b/crime_data/common/marshmallow_schemas.py index 5ed8b4d..473d0d6 100644 --- a/crime_data/common/marshmallow_schemas.py +++ b/crime_data/common/marshmallow_schemas.py @@ -1299,6 +1299,11 @@ class Meta: model = cdemodels.NIBRSRegionVictimDenormAge ordered = True +class NIBRSRegionDenormVictimOffenderRelationshipSchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionDenormVictimOffenderRelationship + ordered = True + class NIBRSRegionVictimDenormLocationSchema(ma.ModelSchema): class Meta: model = cdemodels.NIBRSRegionVictimDenormLocation @@ -1324,6 +1329,11 @@ class Meta: model = cdemodels.NIBRSRegionOffenderDenormAge ordered = True +class NIBRSRegionOffenderDenormEthnicitySchema(ma.ModelSchema): + class Meta: + model = cdemodels.NIBRSRegionOffenderDenormEthnicity + ordered = True + class NIBRSRegionOffenseCountSchema(ma.ModelSchema): class Meta: model = cdemodels.NIBRSRegionOffenseCount From 384cd0aa530bd226c4e733540c25607ad57232a6 Mon Sep 17 00:00:00 2001 From: Jacob Wentz Date: Wed, 28 Feb 2018 10:15:16 -0500 Subject: [PATCH 3/5] Fix to NIBRS Regions MVs --- dba/create_new/offender_nibrs_rollup_mv.sql | 49 +- .../offense_counts_nibrs_rollup_mv.sql | 4 +- dba/create_new/victim_nibrs_rollup_mv.sql | 562 +++++++++--------- 3 files changed, 307 insertions(+), 308 deletions(-) diff --git a/dba/create_new/offender_nibrs_rollup_mv.sql b/dba/create_new/offender_nibrs_rollup_mv.sql index 435c2c2..bfee986 100644 --- a/dba/create_new/offender_nibrs_rollup_mv.sql +++ b/dba/create_new/offender_nibrs_rollup_mv.sql @@ -262,13 +262,13 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when n.race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when n.race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when n.race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when n.race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when n.race_desc = 'White' then count end), 0) as white -from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +sum(n.asian) as asian, +sum(n.native_hawaiian) as native_hawaiian, +sum(n.black) as black, +sum(n.american_indian) as american_indian, +sum(n.unknown) as unknown, +sum(n.white) as white +from public.nibrs_state_denorm_offender_race n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; @@ -277,11 +277,11 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, -coalesce(sum(case when n.ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when n.ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when n.ethnicity_name = 'Unknown' then count end), 0) as unknown -from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +sum(n.hispanic) as hispanic, +sum(n.multiple) as multiple, +sum(n.not_Hispanic) as not_Hispanic, +sum(n.unknown) as unknown +from public.nibrs_state_denorm_offender_ethnicity n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; @@ -290,21 +290,20 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when n.age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when n.age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when n.age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when n.age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when n.age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when n.age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when n.age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when n.age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when n.age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when n.age_range = 'UNKNOWN' then count end), 0) as unknown -from public.nibrs_offender_count n, public.state_lk s,public.region_lk r +sum(n.range_0_9) as range_0_9, +sum(n.range_10_19) as range_10_19, +sum(n.range_20_29) as range_20_29, +sum(n.range_30_39) as range_30_39, +sum(n.range_40_49) as range_40_49, +sum(n.range_50_59) as range_50_59, +sum(n.range_60_69) as range_60_69, +sum(n.range_70_79) as range_70_79, +sum(n.range_80_89) as range_80_89, +sum(n.range_90_99) as range_90_99, +sum(n.unknown) as unknown +from public.nibrs_state_denorm_offender_age n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; - UPDATE nibrs_offender_count SET state_abbr = TRIM(state_abbr),ori = TRIM(ori), offense_name = TRIM(offense_name),sex_code = TRIM(sex_code), diff --git a/dba/create_new/offense_counts_nibrs_rollup_mv.sql b/dba/create_new/offense_counts_nibrs_rollup_mv.sql index 03a72b9..a286722 100644 --- a/dba/create_new/offense_counts_nibrs_rollup_mv.sql +++ b/dba/create_new/offense_counts_nibrs_rollup_mv.sql @@ -26,14 +26,14 @@ sum(offense_count) as offense_count from public.nibrs_offense_count group by agency_id, ori, offense_name, data_year; --region -CREATE MATERIALIZED VIEW nibrs_state_denorm_offense_count AS +CREATE MATERIALIZED VIEW nibrs_region_denorm_offense_count AS select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, sum(n.incident_count) as incident_count, sum(n.offense_count) as offense_count -from public.nibrs_offense_count n, public.state_lk s,public.region_lk r +from public.nibrs_state_denorm_offense_count n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year;; diff --git a/dba/create_new/victim_nibrs_rollup_mv.sql b/dba/create_new/victim_nibrs_rollup_mv.sql index 40de28a..98ae117 100644 --- a/dba/create_new/victim_nibrs_rollup_mv.sql +++ b/dba/create_new/victim_nibrs_rollup_mv.sql @@ -2,9 +2,9 @@ CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_sex AS select offense_name as offense_name, data_year as data_year, -coalesce(sum(case when sex_code = 'M' then count end), 0) as male_count, -coalesce(sum(case when sex_code = 'F' then count end), 0) as female_count, -coalesce(sum(case when sex_code = 'U' then count end), 0) as unknown_count +sum(case when sex_code = 'M' then count end), 0) as male_count, +sum(case when sex_code = 'F' then count end), 0) as female_count, +sum(case when sex_code = 'U' then count end), 0) as unknown_count from public.nibrs_victim_count group by offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_count AS @@ -18,39 +18,39 @@ CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_race AS select offense_name as offense_name, data_year as data_year, -coalesce(sum(case when race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when race_desc = 'White' then count end), 0) as white +sum(case when race_desc = 'Asian' then count end), 0) as asian, +sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +sum(case when race_desc = 'Black or African American' then count end), 0) as black, +sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +sum(case when race_desc = 'Unknown' then count end), 0) as unknown, +sum(case when race_desc = 'White' then count end), 0) as white from public.nibrs_victim_count group by offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_ethnicity AS select offense_name as offense_name, data_year as data_year, -coalesce(sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic,a -coalesce(sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown +sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic,a +sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, +sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown from public.nibrs_victim_count group by offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_age AS select offense_name as offense_name, data_year as data_year, -coalesce(sum(case when age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown +sum(case when age_range = '0-9' then count end), 0) as range_0_9, +sum(case when age_range = '10-19' then count end), 0) as range_10_19, +sum(case when age_range = '20-29' then count end), 0) as range_20_29, +sum(case when age_range = '30-39' then count end), 0) as range_30_39, +sum(case when age_range = '40-49' then count end), 0) as range_40_49, +sum(case when age_range = '50-59' then count end), 0) as range_50_59, +sum(case when age_range = '60-69' then count end), 0) as range_60_69, +sum(case when age_range = '70-79' then count end), 0) as range_70_79, +sum(case when age_range = '80-89' then count end), 0) as range_80_89, +sum(case when age_range = '90-99' then count end), 0) as range_90_99, +sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown from public.nibrs_victim_count group by offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_state_denorm_victim_sex AS @@ -58,9 +58,9 @@ select state_id as state_id, state_abbr as state_abbr, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when sex_code = 'M' then count end), 0) as male_count, -coalesce(sum(case when sex_code = 'F' then count end), 0) as female_count, -coalesce(sum(case when sex_code = 'U' then count end), 0) as unknown_count +sum(case when sex_code = 'M' then count end), 0) as male_count, +sum(case when sex_code = 'F' then count end), 0) as female_count, +sum(case when sex_code = 'U' then count end), 0) as unknown_count from public.nibrs_victim_count group by state_id, state_abbr, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_state_denorm_victim_count AS @@ -76,12 +76,12 @@ select state_id as state_id, state_abbr as state_abbr, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when race_desc = 'White' then count end), 0) as white +sum(case when race_desc = 'Asian' then count end), 0) as asian, +sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +sum(case when race_desc = 'Black or African American' then count end), 0) as black, +sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +sum(case when race_desc = 'Unknown' then count end), 0) as unknown, +sum(case when race_desc = 'White' then count end), 0) as white from public.nibrs_victim_count group by state_id, state_abbr, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_state_denorm_victim_ethnicity AS @@ -89,10 +89,10 @@ select state_id as state_id, state_abbr as state_abbr, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, -coalesce(sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown +sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, +sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, +sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown from public.nibrs_victim_count group by state_id, state_abbr, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_state_denorm_victim_age AS @@ -100,17 +100,17 @@ select state_id as state_id, state_abbr as state_abbr, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown +sum(case when age_range = '0-9' then count end), 0) as range_0_9, +sum(case when age_range = '10-19' then count end), 0) as range_10_19, +sum(case when age_range = '20-29' then count end), 0) as range_20_29, +sum(case when age_range = '30-39' then count end), 0) as range_30_39, +sum(case when age_range = '40-49' then count end), 0) as range_40_49, +sum(case when age_range = '50-59' then count end), 0) as range_50_59, +sum(case when age_range = '60-69' then count end), 0) as range_60_69, +sum(case when age_range = '70-79' then count end), 0) as range_70_79, +sum(case when age_range = '80-89' then count end), 0) as range_80_89, +sum(case when age_range = '90-99' then count end), 0) as range_90_99, +sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown from public.nibrs_victim_count group by state_id, state_abbr, offense_name, data_year; @@ -119,9 +119,9 @@ select agency_id as agency_id, ori as ori, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when sex_code = 'M' then count end), 0) as male_count, -coalesce(sum(case when sex_code = 'F' then count end), 0) as female_count, -coalesce(sum(case when sex_code = 'U' then count end), 0) as unknown_count +sum(case when sex_code = 'M' then count end), 0) as male_count, +sum(case when sex_code = 'F' then count end), 0) as female_count, +sum(case when sex_code = 'U' then count end), 0) as unknown_count from public.nibrs_victim_count group by agency_id, ori, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_count AS @@ -137,12 +137,12 @@ select agency_id as agency_id, ori as ori, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when race_desc = 'White' then count end), 0) as white +sum(case when race_desc = 'Asian' then count end), 0) as asian, +sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, +sum(case when race_desc = 'Black or African American' then count end), 0) as black, +sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, +sum(case when race_desc = 'Unknown' then count end), 0) as unknown, +sum(case when race_desc = 'White' then count end), 0) as white from public.nibrs_victim_count group by agency_id, ori, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_ethnicity AS @@ -150,10 +150,10 @@ select agency_id as agency_id, ori as ori, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, -coalesce(sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown +sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, +sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, +sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, +sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown from public.nibrs_victim_count group by agency_id, ori, offense_name, data_year; CREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_age AS @@ -161,17 +161,17 @@ select agency_id as agency_id, ori as ori, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown +sum(case when age_range = '0-9' then count end), 0) as range_0_9, +sum(case when age_range = '10-19' then count end), 0) as range_10_19, +sum(case when age_range = '20-29' then count end), 0) as range_20_29, +sum(case when age_range = '30-39' then count end), 0) as range_30_39, +sum(case when age_range = '40-49' then count end), 0) as range_40_49, +sum(case when age_range = '50-59' then count end), 0) as range_50_59, +sum(case when age_range = '60-69' then count end), 0) as range_60_69, +sum(case when age_range = '70-79' then count end), 0) as range_70_79, +sum(case when age_range = '80-89' then count end), 0) as range_80_89, +sum(case when age_range = '90-99' then count end), 0) as range_90_99, +sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown from public.nibrs_victim_count group by agency_id, ori, offense_name, data_year; CCREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_location_temp AS @@ -188,52 +188,52 @@ select agency_id as agency_id, ori as ori, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, -coalesce(sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, -coalesce(sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, -coalesce(sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, -coalesce(sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, -coalesce(sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, -coalesce(sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, -coalesce(sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, -coalesce(sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, -coalesce(sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, -coalesce(sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, -coalesce(sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, -coalesce(sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, -coalesce(sum(case when location = 'Community Center' then sum end), 0) as Community_Center, -coalesce(sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, -coalesce(sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, -coalesce(sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, -coalesce(sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, -coalesce(sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, -coalesce(sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, -coalesce(sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, -coalesce(sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, -coalesce(sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, -coalesce(sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, -coalesce(sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, -coalesce(sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, -coalesce(sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, -coalesce(sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, -coalesce(sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, -coalesce(sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, -coalesce(sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, -coalesce(sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, -coalesce(sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, -coalesce(sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, -coalesce(sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, -coalesce(sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, -coalesce(sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, -coalesce(sum(case when location = 'School/College' then sum end), 0) as School__College, -coalesce(sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, -coalesce(sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, -coalesce(sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, -coalesce(sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, -coalesce(sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, -coalesce(sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, -coalesce(sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, -coalesce(sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store +sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, +sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, +sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, +sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, +sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, +sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, +sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, +sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, +sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, +sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, +sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, +sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, +sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, +sum(case when location = 'Community Center' then sum end), 0) as Community_Center, +sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, +sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, +sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, +sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, +sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, +sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, +sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, +sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, +sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, +sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, +sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, +sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, +sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, +sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, +sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, +sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, +sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, +sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, +sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, +sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, +sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, +sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, +sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, +sum(case when location = 'School/College' then sum end), 0) as School__College, +sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, +sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, +sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, +sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, +sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, +sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, +sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, +sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store from public.nibrs_agency_denorm_victim_location_temp group by agency_id, ori, offense_name, data_year; @@ -252,52 +252,52 @@ select state_id as state_id, agency_id as state_abbr, offense_name as offense_name, data_year as data_year, -coalesce(sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, -coalesce(sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, -coalesce(sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, -coalesce(sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, -coalesce(sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, -coalesce(sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, -coalesce(sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, -coalesce(sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, -coalesce(sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, -coalesce(sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, -coalesce(sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, -coalesce(sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, -coalesce(sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, -coalesce(sum(case when location = 'Community Center' then sum end), 0) as Community_Center, -coalesce(sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, -coalesce(sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, -coalesce(sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, -coalesce(sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, -coalesce(sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, -coalesce(sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, -coalesce(sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, -coalesce(sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, -coalesce(sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, -coalesce(sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, -coalesce(sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, -coalesce(sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, -coalesce(sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, -coalesce(sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, -coalesce(sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, -coalesce(sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, -coalesce(sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, -coalesce(sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, -coalesce(sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, -coalesce(sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, -coalesce(sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, -coalesce(sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, -coalesce(sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, -coalesce(sum(case when location = 'School/College' then sum end), 0) as School__College, -coalesce(sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, -coalesce(sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, -coalesce(sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, -coalesce(sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, -coalesce(sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, -coalesce(sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, -coalesce(sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, -coalesce(sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store +sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, +sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, +sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, +sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, +sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, +sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, +sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, +sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, +sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, +sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, +sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, +sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, +sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, +sum(case when location = 'Community Center' then sum end), 0) as Community_Center, +sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, +sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, +sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, +sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, +sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, +sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, +sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, +sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, +sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, +sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, +sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, +sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, +sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, +sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, +sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, +sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, +sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, +sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, +sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, +sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, +sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, +sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, +sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, +sum(case when location = 'School/College' then sum end), 0) as School__College, +sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, +sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, +sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, +sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, +sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, +sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, +sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, +sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store from public.nibrs_state_denorm_victim_location_temp group by state_id, state_abbr,offense_name, data_year; @@ -313,52 +313,52 @@ CREATE MATERIALIZED VIEW nibrs_national_denorm_victim_location AS select offense_name as offense_name, data_year as data_year, -coalesce(sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, -coalesce(sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, -coalesce(sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, -coalesce(sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, -coalesce(sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, -coalesce(sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, -coalesce(sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, -coalesce(sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, -coalesce(sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, -coalesce(sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, -coalesce(sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, -coalesce(sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, -coalesce(sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, -coalesce(sum(case when location = 'Community Center' then sum end), 0) as Community_Center, -coalesce(sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, -coalesce(sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, -coalesce(sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, -coalesce(sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, -coalesce(sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, -coalesce(sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, -coalesce(sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, -coalesce(sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, -coalesce(sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, -coalesce(sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, -coalesce(sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, -coalesce(sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, -coalesce(sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, -coalesce(sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, -coalesce(sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, -coalesce(sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, -coalesce(sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, -coalesce(sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, -coalesce(sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, -coalesce(sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, -coalesce(sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, -coalesce(sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, -coalesce(sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, -coalesce(sum(case when location = 'School/College' then sum end), 0) as School__College, -coalesce(sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, -coalesce(sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, -coalesce(sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, -coalesce(sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, -coalesce(sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, -coalesce(sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, -coalesce(sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, -coalesce(sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store +sum(case when location = 'Residence/Home' then sum end), 0) as Residence_Home, +sum(case when location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, +sum(case when location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, +sum(case when location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, +sum(case when location = 'Amusement Park' then sum end), 0) as Amusement_Park, +sum(case when location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, +sum(case when location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, +sum(case when location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, +sum(case when location = 'Bank/Savings and Loan' then sum end), 0) as Bank, +sum(case when location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, +sum(case when location = 'Camp/Campground' then sum end), 0) as Campground, +sum(case when location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, +sum(case when location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, +sum(case when location = 'Community Center' then sum end), 0) as Community_Center, +sum(case when location = 'Construction Site' then sum end), 0) as Construction_Site, +sum(case when location = 'Cyberspace' then sum end), 0) as Cyberspace, +sum(case when location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, +sum(case when location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, +sum(case when location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, +sum(case when location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, +sum(case when location = 'Farm Facility' then sum end), 0) as Farm_Facility, +sum(case when location = 'Field/Woods' then sum end), 0) as Field__Woods, +sum(case when location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, +sum(case when location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, +sum(case when location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, +sum(case when location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, +sum(case when location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, +sum(case when location = 'Industrial Site' then sum end), 0) as Industrial_Site, +sum(case when location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, +sum(case when location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, +sum(case when location = 'Liquor Store' then sum end), 0) as Liquor_Store, +sum(case when location = 'Military Installation' then sum end), 0) as Military_Base, +sum(case when location = 'Other/Unknown' then sum end), 0) as Unknown, +sum(case when location = 'Park/Playground' then sum end), 0) as Park__Playground, +sum(case when location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, +sum(case when location = 'Rest Area' then sum end), 0) as Rest_Area, +sum(case when location = 'Restaurant' then sum end), 0) as Restaurant, +sum(case when location = 'School/College' then sum end), 0) as School__College, +sum(case when location = 'School-College/University' then sum end), 0) as School_College__University, +sum(case when location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, +sum(case when location = 'Service/Gas Station' then sum end), 0) as Gas_Station, +sum(case when location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, +sum(case when location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, +sum(case when location = 'Specialty Store' then sum end), 0) as Specialty_Store, +sum(case when location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, +sum(case when location = 'Convenience Store' then sum end), 0) as convenience_store from public.nibrs_national_denorm_victim_location_temp group by offense_name, data_year; @@ -389,13 +389,13 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when n.race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when n.race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when n.race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when n.race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when n.race_desc = 'White' then count end), 0) as white -from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +sum(n.asian) as asian, +sum(n.native_hawaiian) as native_hawaiian, +sum(n.black) as black, +sum(n.american_indian) as american_indian, +sum(n.unknown) as unknown, +sum(n.white) as white +from public.nibrs_state_denorm_victim_race n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; @@ -404,11 +404,11 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, -coalesce(sum(case when n.ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when n.ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when n.ethnicity_name = 'Unknown' then count end), 0) as unknown -from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +sum(n.hispanic) as hispanic, +sum(n.multiple) as multiple, +sum(n.not_Hispanic) as not_Hispanic, +sum(n.unknown) as unknown +from public.nibrs_state_denorm_victim_ethnicity n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; @@ -417,18 +417,18 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when n.age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when n.age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when n.age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when n.age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when n.age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when n.age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when n.age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when n.age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when n.age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when n.age_range = 'UNKNOWN' then count end), 0) as unknown -from public.nibrs_victim_count n, public.state_lk s,public.region_lk r +sum(n.range_0_9) as range_0_9, +sum(n.range_10_19) as range_10_19, +sum(n.range_20_29) as range_20_29, +sum(n.range_30_39) as range_30_39, +sum(n.range_40_49) as range_40_49, +sum(n.range_50_59) as range_50_59, +sum(n.range_60_69) as range_60_69, +sum(n.range_70_79) as range_70_79, +sum(n.range_80_89) as range_80_89, +sum(n.range_90_99) as range_90_99, +sum(n.unknown) as unknown +from public.nibrs_state_denorm_victim_age n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; @@ -437,53 +437,53 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.location = 'Residence/Home' then sum end), 0) as Residence_Home, -coalesce(sum(case when n.location = 'Parking Garage/Lot' then sum end), 0) as Parking_Garage__Lot, -coalesce(sum(case when n.location = 'Abandoned/Condemned Structure' then sum end), 0) as Abandoned_Condemned__Structure, -coalesce(sum(case when n.location = 'Air/Bus/Train Terminal' then sum end), 0) as Air__Bus__Train_Terminal, -coalesce(sum(case when n.location = 'Amusement Park' then sum end), 0) as Amusement_Park, -coalesce(sum(case when n.location = 'Arena/Stadium/Fairgrounds/Coliseum' then sum end), 0) as Arena__Stadium__Fairgrounds, -coalesce(sum(case when n.location = 'ATM Separate from Bank' then sum end), 0) as ATM_Separate_from_Bank, -coalesce(sum(case when n.location = 'Auto Dealership New/Used' then sum end), 0) as Auto_Dealership, -coalesce(sum(case when n.location = 'Bank/Savings and Loan' then sum end), 0) as Bank, -coalesce(sum(case when n.location = 'Bar/Nightclub' then sum end), 0) as Bar_Nightclub, -coalesce(sum(case when n.location = 'Camp/Campground' then sum end), 0) as Campground, -coalesce(sum(case when n.location = 'Church/Synagogue/Temple/Mosque' then sum end), 0) as Church__Synagogue__Temple__Mosque, -coalesce(sum(case when n.location = 'Commercial/Office Building' then sum end), 0) as Commercial__Office_Building, -coalesce(sum(case when n.location = 'Community Center' then sum end), 0) as Community_Center, -coalesce(sum(case when n.location = 'Construction Site' then sum end), 0) as Construction_Site, -coalesce(sum(case when n.location = 'Cyberspace' then sum end), 0) as Cyberspace, -coalesce(sum(case when n.location = 'Daycare Facility' then sum end), 0) as Daycare_Facility, -coalesce(sum(case when n.location = 'Department/Discount Store' then sum end), 0) as Department__Discount_Store, -coalesce(sum(case when n.location = 'Dock/Wharf/Freight/Modal Terminal' then sum end), 0) as Dock__Wharf__Shipping_Terminal, -coalesce(sum(case when n.location = 'Drug Store/Doctor’s Office/Hospital' then sum end), 0) as Drug_Store__Doctors_Office__Hospital, -coalesce(sum(case when n.location = 'Farm Facility' then sum end), 0) as Farm_Facility, -coalesce(sum(case when n.location = 'Field/Woods' then sum end), 0) as Field__Woods, -coalesce(sum(case when n.location = 'Gambling Facility/Casino/Race Track' then sum end), 0) as Gambling_Facility__Casino__Race_Track, -coalesce(sum(case when n.location = 'Government/Public Building' then sum end), 0) as Government__Public_Building, -coalesce(sum(case when n.location = 'Grocery/Supermarket' then sum end), 0) as Grocery_Store, -coalesce(sum(case when n.location = 'Highway/Road/Alley/Street/Sidewalk' then sum end), 0) as Highway__Alley__Street__Sidewalk, -coalesce(sum(case when n.location = 'Hotel/Motel/Etc.' then sum end), 0) as Hotel__Motel, -coalesce(sum(case when n.location = 'Industrial Site' then sum end), 0) as Industrial_Site, -coalesce(sum(case when n.location = 'Jail/Prison/Penitentiary/Corrections Facility' then sum end), 0) as Jail__Prison__Corrections_Facility, -coalesce(sum(case when n.location = 'Lake/Waterway/Beach' then sum end), 0) as Lake__Waterway__Beach, -coalesce(sum(case when n.location = 'Liquor Store' then sum end), 0) as Liquor_Store, -coalesce(sum(case when n.location = 'Military Installation' then sum end), 0) as Military_Base, -coalesce(sum(case when n.location = 'Other/Unknown' then sum end), 0) as Unknown, -coalesce(sum(case when n.location = 'Park/Playground' then sum end), 0) as Park__Playground, -coalesce(sum(case when n.location = 'Rental Storage Facility' then sum end), 0) as Rental_Storage_Facility, -coalesce(sum(case when n.location = 'Rest Area' then sum end), 0) as Rest_Area, -coalesce(sum(case when n.location = 'Restaurant' then sum end), 0) as Restaurant, -coalesce(sum(case when n.location = 'School/College' then sum end), 0) as School__College, -coalesce(sum(case when n.location = 'School-College/University' then sum end), 0) as School_College__University, -coalesce(sum(case when n.location = 'School-Elementary/Secondary' then sum end), 0) as School_Elementary__Secondary, -coalesce(sum(case when n.location = 'Service/Gas Station' then sum end), 0) as Gas_Station, -coalesce(sum(case when n.location = 'Shelter-Mission/Homeless' then sum end), 0) as Mission__Homeless_Shelter, -coalesce(sum(case when n.location = 'Shopping Mall' then sum end), 0) as Shopping_Mall, -coalesce(sum(case when n.location = 'Specialty Store' then sum end), 0) as Specialty_Store, -coalesce(sum(case when n.location = 'Tribal Lands' then sum end), 0) as Tribal_Lands, -coalesce(sum(case when n.location = 'Convenience Store' then sum end), 0) as convenience_store -from public.nibrs_national_denorm_victim_location_temp n, public.state_lk s,public.region_lk r +sum(n.Residence_Home) as Residence_Home, +sum(n.Parking_Garage__Lot) as Parking_Garage__Lot, +sum(n.Abandoned_Condemned__Structure) as Abandoned_Condemned__Structure, +sum(n.Air__Bus__Train_Terminal) as Air__Bus__Train_Terminal, +sum(n.Amusement_Park) as Amusement_Park, +sum(n.Arena__Stadium__Fairgrounds) as Arena__Stadium__Fairgrounds, +sum(n.ATM_Separate_from_Bank) as ATM_Separate_from_Bank, +sum(n.Auto_Dealership) as Auto_Dealership, +sum(n.Bank) as Bank, +sum(n.Bar_Nightclub) as Bar_Nightclub, +sum(n.Campground) as Campground, +sum(n.Church__Synagogue__Temple__Mosque) as Church__Synagogue__Temple__Mosque, +sum(n.Commercial__Office_Building) as Commercial__Office_Building, +sum(n.Community_Center) as Community_Center, +sum(n.Construction_Site) as Construction_Site, +sum(n.Cyberspace) as Cyberspace, +sum(n.Daycare_Facility) as Daycare_Facility, +sum(n.Department__Discount_Store) as Department__Discount_Store, +sum(n.Dock__Wharf__Shipping_Terminal) as Dock__Wharf__Shipping_Terminal, +sum(n.Drug_Store__Doctors_Office__Hospital) as Drug_Store__Doctors_Office__Hospital, +sum(n.Farm_Facility) as Farm_Facility, +sum(n.Field__Woods) as Field__Woods, +sum(n.Gambling_Facility__Casino__Race_Track) as Gambling_Facility__Casino__Race_Track, +sum(n.Government__Public_Building) as Government__Public_Building, +sum(n.Grocery_Store) as Grocery_Store, +sum(n.Highway__Alley__Street__Sidewalk) as Highway__Alley__Street__Sidewalk, +sum(n.Hotel__Motel) as Hotel__Motel, +sum(n.Industrial_Site) as Industrial_Site, +sum(n.Jail__Prison__Corrections_Facility) as Jail__Prison__Corrections_Facility, +sum(n.Lake__Waterway__Beach) as Lake__Waterway__Beach, +sum(n.Liquor_Store) as Liquor_Store, +sum(n.Military_Base) as Military_Base, +sum(n.Unknown) as Unknown, +sum(n.Park__Playground) as Park__Playground, +sum(n.Rental_Storage_Facility) as Rental_Storage_Facility, +sum(n.Rest_Area) as Rest_Area, +sum(n.Restaurant) as Restaurant, +sum(n.School__College) as School__College, +sum(n.School_College__University) as School_College__University, +sum(n.School_Elementary__Secondary) as School_Elementary__Secondary, +sum(n.Gas_Station) as Gas_Station, +sum(n.Mission__Homeless_Shelter) as Mission__Homeless_Shelter, +sum(n.Shopping_Mall) as Shopping_Mall, +sum(n.Specialty_Store) as Specialty_Store, +sum(n.Tribal_Lands) as Tribal_Lands, +sum(n.convenience_store) as convenience_store +from public.nibrs_state_denorm_victim_location n, public.state_lk s,public.region_lk r where s.region_code = r.region_code group by s.region_code,r.region_name, n.offense_name, n.data_year; From f7d8c33a610ba595b7f4bc1812f6e4756c8d10c4 Mon Sep 17 00:00:00 2001 From: Jacob Wentz Date: Wed, 28 Feb 2018 10:37:11 -0500 Subject: [PATCH 4/5] Fix to Region NIBRS MV Scripts --- dba/create_new/offender_nibrs_rollup_mv.sql | 61 ------------------- .../victim_offender_relationship_mv.sql | 56 ++++++++--------- 2 files changed, 28 insertions(+), 89 deletions(-) diff --git a/dba/create_new/offender_nibrs_rollup_mv.sql b/dba/create_new/offender_nibrs_rollup_mv.sql index bfee986..14c9202 100644 --- a/dba/create_new/offender_nibrs_rollup_mv.sql +++ b/dba/create_new/offender_nibrs_rollup_mv.sql @@ -174,67 +174,6 @@ coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown from public.nibrs_offender_count group by agency_id, ori, offense_name, data_year; -CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_sex AS -select region_code as region_code, -region_name as region_name, -offense_name as offense_name, -data_year as data_year, -coalesce(sum(case when sex_code = 'M' then count end), 0) as male_count, -coalesce(sum(case when sex_code = 'F' then count end), 0) as female_count, -coalesce(sum(case when sex_code = 'U' then count end), 0) as unknown_count -from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; - -CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_count AS -select region_code as region_code, -region_name as region_name, -offense_name as offense_name, -data_year as data_year, -sum(count) as count -from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; - -CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_race AS -select region_code as region_code, -region_name as region_name, -offense_name as offense_name, -data_year as data_year, -coalesce(sum(case when race_desc = 'Asian' then count end), 0) as asian, -coalesce(sum(case when race_desc = 'Native Hawaiian or Pacific Islander' then count end), 0) as native_hawaiian, -coalesce(sum(case when race_desc = 'Black or African American' then count end), 0) as black, -coalesce(sum(case when race_desc = 'American Indian or Alaska Native' then count end), 0) as american_indian, -coalesce(sum(case when race_desc = 'Unknown' then count end), 0) as unknown, -coalesce(sum(case when race_desc = 'White' then count end), 0) as white -from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; - -CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_ethnicity AS -select region_code as region_code, -region_name as region_name, -offense_name as offense_name, -data_year as data_year, -coalesce(sum(case when ethnicity_name = 'Hispanic or Latino' then count end), 0) as hispanic, -coalesce(sum(case when ethnicity_name = 'Multiple' then count end), 0) as multiple, -coalesce(sum(case when ethnicity_name = 'Not Hispanic or Latino' then count end), 0) as not_Hispanic, -coalesce(sum(case when ethnicity_name = 'Unknown' then count end), 0) as unknown -from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; - -CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_age AS -select region_code as region_code, -region_name as region_name, -offense_name as offense_name, -data_year as data_year, -coalesce(sum(case when age_range = '0-9' then count end), 0) as range_0_9, -coalesce(sum(case when age_range = '10-19' then count end), 0) as range_10_19, -coalesce(sum(case when age_range = '20-29' then count end), 0) as range_20_29, -coalesce(sum(case when age_range = '30-39' then count end), 0) as range_30_39, -coalesce(sum(case when age_range = '40-49' then count end), 0) as range_40_49, -coalesce(sum(case when age_range = '50-59' then count end), 0) as range_50_59, -coalesce(sum(case when age_range = '60-69' then count end), 0) as range_60_69, -coalesce(sum(case when age_range = '70-79' then count end), 0) as range_70_79, -coalesce(sum(case when age_range = '80-89' then count end), 0) as range_80_89, -coalesce(sum(case when age_range = '90-99' then count end), 0) as range_90_99, -coalesce(sum(case when age_range = 'UNKNOWN' then count end), 0) as unknown -from public.nibrs_offender_count group by region_code, region_name, offense_name, data_year; - - --Region CREATE MATERIALIZED VIEW nibrs_region_denorm_offender_sex AS select s.region_code as region_code, diff --git a/dba/create_new/victim_offender_relationship_mv.sql b/dba/create_new/victim_offender_relationship_mv.sql index 538f485..1da4508 100644 --- a/dba/create_new/victim_offender_relationship_mv.sql +++ b/dba/create_new/victim_offender_relationship_mv.sql @@ -70,35 +70,35 @@ select s.region_code as region_code, r.region_name as region_name, n.offense_name as offense_name, n.data_year as data_year, -coalesce(sum(case when n.relationship = 'Victim Was Acquaintance' then count end), 0) as acquaintance, -coalesce(sum(case when n.relationship = 'Victim Was Babysittee' then count end), 0) as babysittee, -coalesce(sum(case when n.relationship = 'Victim Was Boyfriend/Girlfriend' then count end), 0) as boyfriend_girlfriend, -coalesce(sum(case when n.relationship = 'Victim Was Child of Boyfriend or Girlfriend' then count end), 0) as child_boyfriend_girlfriend, -coalesce(sum(case when n.relationship = 'Victim Was Child' then count end), 0) as child, -coalesce(sum(case when n.relationship = 'Victim Was Common-Law Spouse' then count end), 0) as common_law_spouse, -coalesce(sum(case when n.relationship = 'Victim was Employee' then count end), 0) as employee, -coalesce(sum(case when n.relationship = 'Victim was Employer' then count end), 0) as employer, -coalesce(sum(case when n.relationship = 'Victim Was Friend' then count end), 0) as friend, -coalesce(sum(case when n.relationship = 'Victim Was Grandchild' then count end), 0) as grandchild, -coalesce(sum(case when n.relationship = 'Victim Was Grandparent' then count end), 0) as grandparent, -coalesce(sum(case when n.relationship = 'Homosexual Relationship' then count end), 0) as homosexual_relationship, -coalesce(sum(case when n.relationship = 'Victim Was In-law' then count end), 0) as in_law, -coalesce(sum(case when n.relationship = 'Victim Was Neighbor' then count end), 0) as neighbor, -coalesce(sum(case when n.relationship = 'Victim Was Other Family Member' then count end), 0) as other_family_member, -coalesce(sum(case when n.relationship = 'Victim was Otherwise Known' then count end), 0) as otherwise_known, -coalesce(sum(case when n.relationship = 'Victim Was Parent' then count end), 0) as parent, -coalesce(sum(case when n.relationship = 'Relationship Unknown' then count end), 0) as relationship_unknown, -coalesce(sum(case when n.relationship = 'Victim Was Sibling' then count end), 0) as sibling, -coalesce(sum(case when n.relationship = 'Victim Was Stepchild' then count end), 0) as stepchild, -coalesce(sum(case when n.relationship = 'Victim Was Spouse' then count end), 0) as spouse, -coalesce(sum(case when n.relationship = 'Victim Was Stepparent' then count end), 0) as stepparent, -coalesce(sum(case when n.relationship = 'Victim Was Stepsibling' then count end), 0) as stepsibling, -coalesce(sum(case when n.relationship = 'Victim Was Stranger' then count end), 0) as stranger, -coalesce(sum(case when n.relationship = 'Victim Was Offender' then count end), 0) as offender, -coalesce(sum(case when n.relationship = 'Victim was Ex-Spouse' then count end), 0) as ex_spouse -from public.nibrs_victim_to_offender_relationship_count n, public.state_lk s,public.region_lk r +sum(n.acquaintance) as acquaintance, +sum(n.babysittee) as babysittee, +sum(n.boyfriend_girlfriend) as boyfriend_girlfriend, +sum(n.child_boyfriend_girlfriend) as child_boyfriend_girlfriend, +sum(n.child) as child, +sum(n.common_law_spouse) as common_law_spouse, +sum(n.employee) as employee, +sum(n.employer) as employer, +sum(n.friend) as friend, +sum(n.grandchild) as grandchild, +sum(n.grandparent) as grandparent, +sum(n.homosexual_relationship) as homosexual_relationship, +sum(n.in_law) as in_law, +sum(n.neighbor) as neighbor, +sum(n.other_family_member) as other_family_member, +sum(n.otherwise_known) as otherwise_known, +sum(n.parent) as parent, +sum(n.relationship_unknown) as relationship_unknown, +sum(n.sibling) as sibling, +sum(n.stepchild) as stepchild, +sum(n.spouse) as spouse, +sum(n.stepparent) as stepparent, +sum(n.stepsibling) as stepsibling, +sum(n.stranger) as stranger, +sum(n.offender) as offender, +sum(n.ex_spouse) as ex_spouse +from public.nibrs_state_denorm_victim_offender_relationship n, public.state_lk s,public.region_lk r where s.region_code = r.region_code -group by s.region_code,r.region_name, n.offense_name, n.data_year;; +group by s.region_code,r.region_name, n.offense_name, n.data_year; CREATE MATERIALIZED VIEW nibrs_agency_denorm_victim_offender_relationship AS select agency_id as agency_id, From 4cd03e663244e641e38f4646bf4411eeb47340a3 Mon Sep 17 00:00:00 2001 From: Jacob Wentz Date: Wed, 28 Feb 2018 12:06:36 -0500 Subject: [PATCH 5/5] Fix to broken NIBRS Endpoint --- crime_data/common/cdemodels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crime_data/common/cdemodels.py b/crime_data/common/cdemodels.py index bbc2f5b..1d9712b 100644 --- a/crime_data/common/cdemodels.py +++ b/crime_data/common/cdemodels.py @@ -1982,7 +1982,7 @@ class NIBRSNationalOffenderDenormSex(db.Model): def get(offense_name=None): query = NIBRSNationalOffenderDenormSex.query if offense_name: - query = query.filter(NIBRSNatNIBRSNationalOffenderDenormSexionalOffenderDenormCount.offense_name.in_(offense_name)) + query = query.filter(NIBRSNationalOffenderDenormSex.offense_name.in_(offense_name)) return query offense_name = db.Column(db.String) male_count = db.Column(db.Integer)