From 38ba9f317f68701e349c9d57c7bbb11bbb04041e Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 19:31:32 +0000 Subject: [PATCH 01/15] Initial commit --- .../reporting.vw_pin_school_impact.sql | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 dbt/models/reporting/reporting.vw_pin_school_impact.sql diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql new file mode 100644 index 000000000..45e3ecd92 --- /dev/null +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -0,0 +1,84 @@ +WITH ranking AS ( + SELECT + info.agency_name, + info.agency_num, + RANK() OVER ( + PARTITION BY + info.agency_name, + pin.year + ORDER BY pin.av_board DESC + ) AS av_board_rank, + pin.pin, + pin.class, + pin.av_mailed, + pin.av_certified, + pin.av_board, + CAST( + ( + pin.av_board - ( + pin.exe_homeowner + + pin.exe_senior + + pin.exe_freeze + + pin.exe_longtime_homeowner + + pin.exe_disabled + + pin.exe_vet_returning + + pin.exe_vet_dis_lt50 + + pin.exe_vet_dis_50_69 + + pin.exe_vet_dis_ge70 + + pin.exe_abate + ) + ) * fact.eq_factor_final AS INT + ) AS eav, + pin.tax_bill_total, + RANK() OVER ( + PARTITION BY + info.agency_name, + pin.year + ORDER BY pin.tax_bill_total DESC + ) AS bill_rank, + code.tax_code_rate, + pin.year + FROM {{ source('tax', 'pin') }} AS pin + LEFT JOIN {{ source('tax', 'tax_code') }} AS code + ON pin.tax_code_num = code.tax_code_num + AND pin.year = code.year + INNER JOIN + {{ source('tax', 'agency_info') }} AS info + ON code.agency_num = info.agency_num + LEFT JOIN {{ source('tax', 'eq_factor') }} AS fact ON pin.year = fact.year + WHERE info.major_type = 'SCHOOL' +), + +tif_ssa AS ( + SELECT + pin.pin, + pin.year, + info.agency_name, + info.minor_type + FROM {{ source('tax', 'pin') }} AS pin + LEFT JOIN {{ source('tax', 'tax_code') }} AS code + ON pin.tax_code_num = code.tax_code_num + AND pin.year = code.year + INNER JOIN + {{ source('tax', 'agency_info') }} AS info + ON code.agency_num = info.agency_num +) + +SELECT + ranking.*, + tif.agency_name AS tif, + ssa.agency_name AS ssa +FROM ranking +LEFT JOIN tif_ssa AS tif + ON ranking.pin = tif.pin + AND ranking.year = tif.year + AND tif.minor_type = 'TIF' +LEFT JOIN tif_ssa AS ssa + ON ranking.pin = ssa.pin + AND ranking.year = ssa.year + AND ssa.minor_type = 'SSA' +WHERE ranking.av_board_rank <= 10 +ORDER BY + ranking.year, + ranking.agency_num, + ranking.av_board_rank From f87afede75be785e3382c1e55faeecdef235d380 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 19:37:23 +0000 Subject: [PATCH 02/15] Docs --- dbt/models/reporting/docs.md | 8 ++++++++ dbt/models/reporting/schema.yml | 3 +++ 2 files changed, 11 insertions(+) diff --git a/dbt/models/reporting/docs.md b/dbt/models/reporting/docs.md index 32dfedb32..fd58a8512 100644 --- a/dbt/models/reporting/docs.md +++ b/dbt/models/reporting/docs.md @@ -78,6 +78,14 @@ PINs without sales have `NULL` sale values. **Primary Key**: `year`, `pin` {% enddocs %} +# vw_pin_school_impact + +{% docs view_vw_pin_school_impact %} +View to get the 10 highest AVs by school district. + +**Primary Key**: `year`, `agency_num` +{% enddocs %} + # vw_pin_township_class {% docs view_vw_pin_township_class %} diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index 7015cd640..8a533488a 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -203,6 +203,9 @@ models: combination_of_columns: - pin + - name: reporting.vw_pin_school_impact + description: '{{ doc("view_vw_pin_school_impact") }}' + - name: reporting.vw_pin_township_class description: '{{ doc("view_vw_pin_township_class") }}' data_tests: From 72d408ab019c84c326e3fec887c1e0d234f08f20 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 19:48:25 +0000 Subject: [PATCH 03/15] Correct primary keys --- dbt/models/reporting/docs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/models/reporting/docs.md b/dbt/models/reporting/docs.md index fd58a8512..c05cb3743 100644 --- a/dbt/models/reporting/docs.md +++ b/dbt/models/reporting/docs.md @@ -83,7 +83,7 @@ PINs without sales have `NULL` sale values. {% docs view_vw_pin_school_impact %} View to get the 10 highest AVs by school district. -**Primary Key**: `year`, `agency_num` +**Primary Key**: `year`, `agency_num`, `av_board_rank` {% enddocs %} # vw_pin_township_class From 8407e3d1e624c6ff5b9d6551c4637f02f9686a1c Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 19:50:45 +0000 Subject: [PATCH 04/15] Add tax code --- dbt/models/reporting/reporting.vw_pin_school_impact.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index 45e3ecd92..0fcf0ab37 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -36,6 +36,7 @@ WITH ranking AS ( pin.year ORDER BY pin.tax_bill_total DESC ) AS bill_rank, + code.tax_code, code.tax_code_rate, pin.year FROM {{ source('tax', 'pin') }} AS pin From 88cab7e30b87b6799254ca2a842536eeea846c67 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 19:57:55 +0000 Subject: [PATCH 05/15] Correct tax code --- dbt/models/reporting/reporting.vw_pin_school_impact.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index 0fcf0ab37..e4c6ae61e 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -36,7 +36,7 @@ WITH ranking AS ( pin.year ORDER BY pin.tax_bill_total DESC ) AS bill_rank, - code.tax_code, + code.tax_code_num, code.tax_code_rate, pin.year FROM {{ source('tax', 'pin') }} AS pin From 57f69377cd536749c116ded8d55bcc4ee5bc00d5 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:13:24 +0000 Subject: [PATCH 06/15] Add uniqueness test --- dbt/models/reporting/schema.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index 8a533488a..2f7363d54 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -205,6 +205,13 @@ models: - name: reporting.vw_pin_school_impact description: '{{ doc("view_vw_pin_school_impact") }}' + data_tests: + - unique_combination_of_columns: + name: reporting_vw_school_impact_unique_by_agency_num_av_board_rank_year + combination_of_columns: + - agency_num + - av_board_rank + - year - name: reporting.vw_pin_township_class description: '{{ doc("view_vw_pin_township_class") }}' From 6aae1553c66056d715dd810ffc827424379b82f7 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:30:06 +0000 Subject: [PATCH 07/15] Make view unique by primary keys --- dbt/models/reporting/reporting.vw_pin_school_impact.sql | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index e4c6ae61e..6240f75cc 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -50,12 +50,14 @@ WITH ranking AS ( WHERE info.major_type = 'SCHOOL' ), +-- We need to use array_agg for this CTE since some parcels can be in multiple +-- SSAs tif_ssa AS ( SELECT pin.pin, pin.year, - info.agency_name, - info.minor_type + info.minor_type, + ARRAY_AGG(info.agency_name) AS agency_name FROM {{ source('tax', 'pin') }} AS pin LEFT JOIN {{ source('tax', 'tax_code') }} AS code ON pin.tax_code_num = code.tax_code_num @@ -63,6 +65,7 @@ tif_ssa AS ( INNER JOIN {{ source('tax', 'agency_info') }} AS info ON code.agency_num = info.agency_num + GROUP BY pin.pin, pin.year, info.minor_type ) SELECT From 15ddf3e1c1fe4b355cc88a79c3ce05b27066a2b5 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:33:43 +0000 Subject: [PATCH 08/15] Temp allow dupes --- dbt/models/reporting/schema.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index 2f7363d54..172e0fab8 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -212,6 +212,8 @@ models: - agency_num - av_board_rank - year + config: + error_if: ">28" - name: reporting.vw_pin_township_class description: '{{ doc("view_vw_pin_township_class") }}' From 9847b6b3c64ca215c13ab14d8ca6d2bf63b237fe Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:39:20 +0000 Subject: [PATCH 09/15] Remove class 0 --- dbt/models/reporting/reporting.vw_pin_school_impact.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index 6240f75cc..ce7da82dc 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -48,6 +48,7 @@ WITH ranking AS ( ON code.agency_num = info.agency_num LEFT JOIN {{ source('tax', 'eq_factor') }} AS fact ON pin.year = fact.year WHERE info.major_type = 'SCHOOL' + AND pin.class != '0' ), -- We need to use array_agg for this CTE since some parcels can be in multiple From 79dadaba77d2580899f4b3f6b690e819c0a2ce49 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:55:54 +0000 Subject: [PATCH 10/15] Add tests --- dbt/models/reporting/schema.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index 172e0fab8..d6c7f3a93 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -207,13 +207,16 @@ models: description: '{{ doc("view_vw_pin_school_impact") }}' data_tests: - unique_combination_of_columns: - name: reporting_vw_school_impact_unique_by_agency_num_av_board_rank_year + name: reporting_vw_school_impact_unique_by_agency_num_pin_year combination_of_columns: - agency_num - - av_board_rank + - pin - year - config: - error_if: ">28" + - accepted_range: + name: reporting_vw_school_impact_uav_board_rank_range + column_name: phase_av_share + min_value: 1 + max_value: 10 - name: reporting.vw_pin_township_class description: '{{ doc("view_vw_pin_township_class") }}' From 24f457f82ba462933b4c9f9f55023d26427eb02f Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 20:57:29 +0000 Subject: [PATCH 11/15] Linting --- dbt/models/reporting/schema.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index d6c7f3a93..e23edbb63 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -206,17 +206,18 @@ models: - name: reporting.vw_pin_school_impact description: '{{ doc("view_vw_pin_school_impact") }}' data_tests: + - accepted_range: + name: reporting_vw_school_impact_uav_board_rank_range + column_name: phase_av_share + min_value: 1 + max_value: 10 - unique_combination_of_columns: name: reporting_vw_school_impact_unique_by_agency_num_pin_year combination_of_columns: - agency_num - pin - year - - accepted_range: - name: reporting_vw_school_impact_uav_board_rank_range - column_name: phase_av_share - min_value: 1 - max_value: 10 + - name: reporting.vw_pin_township_class description: '{{ doc("view_vw_pin_township_class") }}' From 8fae6de150b16f6364e04da5dde5cff586e642d8 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 21:00:05 +0000 Subject: [PATCH 12/15] Dumb --- dbt/models/reporting/schema.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/models/reporting/schema.yml b/dbt/models/reporting/schema.yml index e23edbb63..0b5f0a510 100644 --- a/dbt/models/reporting/schema.yml +++ b/dbt/models/reporting/schema.yml @@ -207,8 +207,8 @@ models: description: '{{ doc("view_vw_pin_school_impact") }}' data_tests: - accepted_range: - name: reporting_vw_school_impact_uav_board_rank_range - column_name: phase_av_share + name: reporting_vw_school_impact_av_board_rank_range + column_name: av_board_rank min_value: 1 max_value: 10 - unique_combination_of_columns: From fb4ca808c5c9a8de5a0097de2ead3c0b3b11f5ef Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 21:18:57 +0000 Subject: [PATCH 13/15] Commenting --- dbt/models/reporting/reporting.vw_pin_school_impact.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index ce7da82dc..10eb6ebc5 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -1,3 +1,5 @@ +-- Script that returns the 10 PINs with the highest AV per school district +-- taxing agency, by year. WITH ranking AS ( SELECT info.agency_name, @@ -48,6 +50,8 @@ WITH ranking AS ( ON code.agency_num = info.agency_num LEFT JOIN {{ source('tax', 'eq_factor') }} AS fact ON pin.year = fact.year WHERE info.major_type = 'SCHOOL' + -- Class 0 PINs have 0 AV but can lead to huge ties if there are many in + -- a district with few PINs AND pin.class != '0' ), From 90286fc3da583bb73ca114df2e98fb9f25e5cfc7 Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Thu, 24 Oct 2024 21:19:58 +0000 Subject: [PATCH 14/15] More documentation --- dbt/models/reporting/docs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dbt/models/reporting/docs.md b/dbt/models/reporting/docs.md index c05cb3743..8650ea8be 100644 --- a/dbt/models/reporting/docs.md +++ b/dbt/models/reporting/docs.md @@ -81,9 +81,9 @@ PINs without sales have `NULL` sale values. # vw_pin_school_impact {% docs view_vw_pin_school_impact %} -View to get the 10 highest AVs by school district. +View to get the 10 highest AVs by school district taxing agency and year. -**Primary Key**: `year`, `agency_num`, `av_board_rank` +**Primary Key**: `year`, `agency_num`, `pin` {% enddocs %} # vw_pin_township_class From ba11fd243e7a5499864272d21531c4c5e183e4fd Mon Sep 17 00:00:00 2001 From: Sweaty Handshake Date: Mon, 28 Oct 2024 13:35:23 +0000 Subject: [PATCH 15/15] Correct eav calc --- .../reporting.vw_pin_school_impact.sql | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/dbt/models/reporting/reporting.vw_pin_school_impact.sql b/dbt/models/reporting/reporting.vw_pin_school_impact.sql index 10eb6ebc5..00d4c8996 100644 --- a/dbt/models/reporting/reporting.vw_pin_school_impact.sql +++ b/dbt/models/reporting/reporting.vw_pin_school_impact.sql @@ -15,22 +15,18 @@ WITH ranking AS ( pin.av_mailed, pin.av_certified, pin.av_board, - CAST( - ( - pin.av_board - ( - pin.exe_homeowner - + pin.exe_senior - + pin.exe_freeze - + pin.exe_longtime_homeowner - + pin.exe_disabled - + pin.exe_vet_returning - + pin.exe_vet_dis_lt50 - + pin.exe_vet_dis_50_69 - + pin.exe_vet_dis_ge70 - + pin.exe_abate - ) - ) * fact.eq_factor_final AS INT - ) AS eav, + CAST((pin.av_board * fact.eq_factor_final) - ( + pin.exe_homeowner + + pin.exe_senior + + pin.exe_freeze + + pin.exe_longtime_homeowner + + pin.exe_disabled + + pin.exe_vet_returning + + pin.exe_vet_dis_lt50 + + pin.exe_vet_dis_50_69 + + pin.exe_vet_dis_ge70 + + pin.exe_abate + ) AS INT) AS taxable_eav, pin.tax_bill_total, RANK() OVER ( PARTITION BY