diff --git a/scripts/jobs/parking/parking_correspondence_performance_qa_with_totals_gds.py b/scripts/jobs/parking/parking_correspondence_performance_qa_with_totals_gds.py deleted file mode 100644 index e2af19ae9..000000000 --- a/scripts/jobs/parking/parking_correspondence_performance_qa_with_totals_gds.py +++ /dev/null @@ -1,456 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame -from scripts.helpers.helpers import ( - get_glue_env_var, - get_latest_partitions, - PARTITION_KEYS, -) - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) -environment = get_glue_env_var("environment") - -# Script generated for node Amazon S3 - Raw - liberator_pcn_qa -AmazonS3Rawliberator_pcn_qa_node1668440603311 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - push_down_predicate=( - "to_date(import_date, 'yyyyMMdd') >= date_sub(current_date, 7)" - ), - table_name="liberator_pcn_qa", - transformation_ctx="AmazonS3Rawliberator_pcn_qa_node1668440603311", - ) -) - -# Script generated for node S3 bucket - refined - parking_correspondence_performance_records_with_pcn -S3bucketrefinedparking_correspondence_performance_records_with_pcn_node1 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - push_down_predicate="to_date(import_date, 'yyyyMMdd') >= date_sub(current_date, 7)", - table_name="parking_correspondence_performance_records_with_pcn", - transformation_ctx=( - "S3bucketrefinedparking_correspondence_performance_records_with_pcn_node1" - ), -) - -# Script generated for node parking_raw_zone - parking_correspondence_performance_teams -parking_raw_zoneparking_correspondence_performance_teams_node1682093516418 = glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - push_down_predicate="to_date(import_date, 'yyyyMMdd') >= date_sub(current_date, 7)", - table_name="parking_correspondence_performance_teams", - transformation_ctx=( - "parking_raw_zoneparking_correspondence_performance_teams_node1682093516418" - ), -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ -/* -For use in Google Studio to calculate the Correspondence performance for each calendar month Total number of cases and Total number of QA reviews for each month. - -14/11/2022 - Created job -21/04/2023 - added teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing -09/08/2023 - added new officers -*/ -with qa_tot as (select -case -when qa_doc_created_by like 'AFalade' then 'Ayo Falade' -when qa_doc_created_by like 'BAhmed' then 'Bilal Ahmed Choudhury' ---Claire Glover when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'djulian' then 'Damien Julian' -when qa_doc_created_by like 'dgardner' then 'Daniel Gardner' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagatolla' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagattolla' ---Earle Nottingham when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'EPassos' then 'Edson Passos'--Edson Passos -when qa_doc_created_by like 'esamson' then 'Emma Samson' -when qa_doc_created_by like 'hpatel' then 'Hamza Patel' -when qa_doc_created_by like 'iaHenry' then 'Ian Henry' -when qa_doc_created_by like 'Imali' then 'Imran Ali' -when qa_doc_created_by like 'JAhmed' then 'Jahed Ahmed' -when qa_doc_created_by like 'mmagnusson' then 'Magnus Magnusson' -when qa_doc_created_by like 'mwalters' then 'Melanie Walters' ---Nohaad Al-othmani when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'OOnisemo' then 'Olabisi Onisemo' -when qa_doc_created_by like 'oolagbaju' then 'Olamide Olagbaju' -when qa_doc_created_by like 'PShakes' then 'Pameta Shakes' -when qa_doc_created_by like 'rcampbell' then 'Rhys Campbell' -when qa_doc_created_by like 'sspanos' then 'Savva Spanos' -when qa_doc_created_by like 'ssunilkumar' then 'Shirley Sunilkumar' -when qa_doc_created_by like 'sbaxter' then 'Sonia Baxter' -when qa_doc_created_by like 'WElegbede' then 'Wasilat Elegbede' -when qa_doc_created_by like 'yyahya' then 'Yusuf Yahya' -when qa_doc_created_by like 'admin' then 'ADMIN' -when qa_doc_created_by like 'albrooks' then 'Alan Brooks' -when qa_doc_created_by like 'bmoloney' then 'bmoloney' -when qa_doc_created_by like 'cbeasley' then 'cbeasley' -when qa_doc_created_by like 'EAbankwa' then 'EAbankwa' -when qa_doc_created_by like 'EOsagiede' then 'EOsagiede' -when qa_doc_created_by like 'khamad-okunnu' then 'khamad-okunnu' -when qa_doc_created_by like 'toike' then 'Tayo Oike' -when qa_doc_created_by like 'MIbabu' then 'Muhammad Ismail Bin Abu' -when qa_doc_created_by like 'stopic' then 'Sandi Topic' -when qa_doc_created_by like 'mdayang' then 'Maria Dayang' -when qa_doc_created_by like 'jskrbic' then 'Jovana Skrbic' -when qa_doc_created_by like 'ihaji' then 'Irfan Haji' -when qa_doc_created_by like 'gpugliese' then 'Gianmarco Pugliese' -when qa_doc_created_by like 'csims' then 'Colin Sims' -when qa_doc_created_by like 'ahenry' then 'Ainsley Henry' - -else qa_doc_created_by end as qa_link_officer_name_corresp -,concat(qa_doc_created_by,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') ) as qatot_unique_id -,qa_doc_created_by as qatot_qa_doc_created_by -,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') as MonthYear_qadone -,count(*) as qatot_total_reviews - - FROM liberator_pcn_qa where import_date =(SELECT max(import_date) FROM liberator_pcn_qa ) - group by - case -when qa_doc_created_by like 'AFalade' then 'Ayo Falade' -when qa_doc_created_by like 'BAhmed' then 'Bilal Ahmed Choudhury' ---Claire Glover when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'djulian' then 'Damien Julian' -when qa_doc_created_by like 'dgardner' then 'Daniel Gardner' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagatolla' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagattolla' ---Earle Nottingham when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'EPassos' then 'Edson Passos'--Edson Passos -when qa_doc_created_by like 'esamson' then 'Emma Samson' -when qa_doc_created_by like 'hpatel' then 'Hamza Patel' -when qa_doc_created_by like 'iaHenry' then 'Ian Henry' -when qa_doc_created_by like 'Imali' then 'Imran Ali' -when qa_doc_created_by like 'JAhmed' then 'Jahed Ahmed' -when qa_doc_created_by like 'mmagnusson' then 'Magnus Magnusson' -when qa_doc_created_by like 'mwalters' then 'Melanie Walters' ---Nohaad Al-othmani when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'OOnisemo' then 'Olabisi Onisemo' -when qa_doc_created_by like 'oolagbaju' then 'Olamide Olagbaju' -when qa_doc_created_by like 'PShakes' then 'Pameta Shakes' -when qa_doc_created_by like 'rcampbell' then 'Rhys Campbell' -when qa_doc_created_by like 'sspanos' then 'Savva Spanos' -when qa_doc_created_by like 'ssunilkumar' then 'Shirley Sunilkumar' -when qa_doc_created_by like 'sbaxter' then 'Sonia Baxter' -when qa_doc_created_by like 'WElegbede' then 'Wasilat Elegbede' -when qa_doc_created_by like 'yyahya' then 'Yusuf Yahya' -when qa_doc_created_by like 'admin' then 'ADMIN' -when qa_doc_created_by like 'albrooks' then 'Alan Brooks' -when qa_doc_created_by like 'bmoloney' then 'bmoloney' -when qa_doc_created_by like 'cbeasley' then 'cbeasley' -when qa_doc_created_by like 'EAbankwa' then 'EAbankwa' -when qa_doc_created_by like 'EOsagiede' then 'EOsagiede' -when qa_doc_created_by like 'khamad-okunnu' then 'khamad-okunnu' -when qa_doc_created_by like 'toike' then 'Tayo Oike' -when qa_doc_created_by like 'MIbabu' then 'Muhammad Ismail Bin Abu' -when qa_doc_created_by like 'stopic' then 'Sandi Topic' -when qa_doc_created_by like 'mdayang' then 'Maria Dayang' -when qa_doc_created_by like 'jskrbic' then 'Jovana Skrbic' -when qa_doc_created_by like 'ihaji' then 'Irfan Haji' -when qa_doc_created_by like 'gpugliese' then 'Gianmarco Pugliese' -when qa_doc_created_by like 'csims' then 'Colin Sims' -when qa_doc_created_by like 'ahenry' then 'Ainsley Henry' -else qa_doc_created_by end -- as qa_link_officer_name_corresp -,concat(qa_doc_created_by,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') ) -,qa_doc_created_by -,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') - -) -, corresp_tot as (select -case -when response_written_by like 'Ayo Falade' then 'AFalade' -when response_written_by like 'Bilal Ahmed Choudhury' then 'BAhmed' -when response_written_by like 'Damien Julian' then 'djulian' -when response_written_by like 'Daniel Gardner' then 'dgardner' -when response_written_by like 'Davide Lagatolla' then 'DLagatolla' -when response_written_by like 'Davide Lagattolla' then 'DLagatolla' -when response_written_by like 'Emma Samson' then 'esamson' -when response_written_by like 'Hamza Patel' then 'hpatel' -when response_written_by like 'Ian Henry' then 'iaHenry' -when response_written_by like 'Imran Ali' then 'Imali' -when response_written_by like 'Jahed Ahmed' then 'JAhmed' -when response_written_by like 'Magnus Magnusson' then 'mmagnusson' -when response_written_by like 'Olabisi Onisemo' then 'OOnisemo' -when response_written_by like 'Olamide Olagbaju' then 'oolagbaju' -when response_written_by like 'Pameta Shakes' then 'PShakes' -when response_written_by like 'Rhys Campbell' then 'rcampbell' -when response_written_by like 'Savva Spanos' then 'sspanos' -when response_written_by like 'Shirley Sunilkumar' then 'ssunilkumar' -when response_written_by like 'Sonia Baxter' then 'sbaxter' -when response_written_by like 'Wasilat Elegbede' then 'WElegbede' -when response_written_by like 'Claire Glover' then 'Claire Glover' -when response_written_by like 'Earle Nottingham' then 'Earle Nottingham' -when response_written_by like 'Edson Passos' then 'EPassos' -when response_written_by like 'Melanie Walters' then 'Melanie Walters' -when response_written_by like 'Nohaad Al-othmani' then 'Nohaad Al-othmani' -when response_written_by like 'Yusuf Yahya' then 'Yusuf Yahya' -when response_written_by like 'Ainsley Henry' then 'ahenry' -when response_written_by like 'Colin Sims' then 'csims' -when response_written_by like 'Gianmarco Pugliese' then 'gpugliese' -when response_written_by like 'Irfan Haji' then 'ihaji' -when response_written_by like 'Jovana Skrbic' then 'jskrbic' -when response_written_by like 'Maria Dayang' then 'mdayang' -when response_written_by like 'Sandi Topic' then 'stopic' -when response_written_by like 'Muhammad Ismail Bin Abu' then 'MIbabu' -when response_written_by like 'Tayo Oike' then 'toike' -else response_written_by end as corresp_link_officer_name_qa -,concat(case -when response_written_by like 'Ayo Falade' then 'AFalade' -when response_written_by like 'Bilal Ahmed Choudhury' then 'BAhmed' -when response_written_by like 'Damien Julian' then 'djulian' -when response_written_by like 'Daniel Gardner' then 'dgardner' -when response_written_by like 'Davide Lagatolla' then 'DLagatolla' -when response_written_by like 'Davide Lagattolla' then 'DLagatolla' -when response_written_by like 'Emma Samson' then 'esamson' -when response_written_by like 'Hamza Patel' then 'hpatel' -when response_written_by like 'Ian Henry' then 'iaHenry' -when response_written_by like 'Imran Ali' then 'Imali' -when response_written_by like 'Jahed Ahmed' then 'JAhmed' -when response_written_by like 'Magnus Magnusson' then 'mmagnusson' -when response_written_by like 'Olabisi Onisemo' then 'OOnisemo' -when response_written_by like 'Olamide Olagbaju' then 'oolagbaju' -when response_written_by like 'Pameta Shakes' then 'PShakes' -when response_written_by like 'Rhys Campbell' then 'rcampbell' -when response_written_by like 'Savva Spanos' then 'sspanos' -when response_written_by like 'Shirley Sunilkumar' then 'ssunilkumar' -when response_written_by like 'Sonia Baxter' then 'sbaxter' -when response_written_by like 'Wasilat Elegbede' then 'WElegbede' -when response_written_by like 'Claire Glover' then 'Claire Glover' -when response_written_by like 'Earle Nottingham' then 'Earle Nottingham' -when response_written_by like 'Edson Passos' then 'EPassos' -when response_written_by like 'Melanie Walters' then 'Melanie Walters' -when response_written_by like 'Nohaad Al-othmani' then 'Nohaad Al-othmani' -when response_written_by like 'Yusuf Yahya' then 'Yusuf Yahya' -when response_written_by like 'Ainsley Henry' then 'ahenry' -when response_written_by like 'Colin Sims' then 'csims' -when response_written_by like 'Gianmarco Pugliese' then 'gpugliese' -when response_written_by like 'Irfan Haji' then 'ihaji' -when response_written_by like 'Jovana Skrbic' then 'jskrbic' -when response_written_by like 'Maria Dayang' then 'mdayang' -when response_written_by like 'Sandi Topic' then 'stopic' -when response_written_by like 'Muhammad Ismail Bin Abu' then 'MIbabu' -when response_written_by like 'Tayo Oike' then 'toike' -else response_written_by end,concat(substr(Cast(response_generated_at as varchar(10)),1, 7), '-01') ) as corresptot_qa_unique_id -,concat(response_written_by,concat(substr(Cast(response_generated_at as varchar(10)),1, 7), '-01') ) as corresptot_unique_id -,response_written_by as corresptot_response_written_by -,monthyear as monthyear_corresp -,count(*) as corresptot_total_cases - -from parking_correspondence_performance_records_with_pcn -where import_date =(select max(import_date) from parking_correspondence_performance_records_with_pcn) -AND parking_correspondence_performance_records_with_pcn.response_generated_at != '' - -group by -case -when response_written_by like 'Ayo Falade' then 'AFalade' -when response_written_by like 'Bilal Ahmed Choudhury' then 'BAhmed' -when response_written_by like 'Damien Julian' then 'djulian' -when response_written_by like 'Daniel Gardner' then 'dgardner' -when response_written_by like 'Davide Lagatolla' then 'DLagatolla' -when response_written_by like 'Davide Lagattolla' then 'DLagatolla' -when response_written_by like 'Emma Samson' then 'esamson' -when response_written_by like 'Hamza Patel' then 'hpatel' -when response_written_by like 'Ian Henry' then 'iaHenry' -when response_written_by like 'Imran Ali' then 'Imali' -when response_written_by like 'Jahed Ahmed' then 'JAhmed' -when response_written_by like 'Magnus Magnusson' then 'mmagnusson' -when response_written_by like 'Olabisi Onisemo' then 'OOnisemo' -when response_written_by like 'Olamide Olagbaju' then 'oolagbaju' -when response_written_by like 'Pameta Shakes' then 'PShakes' -when response_written_by like 'Rhys Campbell' then 'rcampbell' -when response_written_by like 'Savva Spanos' then 'sspanos' -when response_written_by like 'Shirley Sunilkumar' then 'ssunilkumar' -when response_written_by like 'Sonia Baxter' then 'sbaxter' -when response_written_by like 'Wasilat Elegbede' then 'WElegbede' -when response_written_by like 'Claire Glover' then 'Claire Glover' -when response_written_by like 'Earle Nottingham' then 'Earle Nottingham' -when response_written_by like 'Edson Passos' then 'EPassos' -when response_written_by like 'Melanie Walters' then 'Melanie Walters' -when response_written_by like 'Nohaad Al-othmani' then 'Nohaad Al-othmani' -when response_written_by like 'Yusuf Yahya' then 'Yusuf Yahya' -when response_written_by like 'Ainsley Henry' then 'ahenry' -when response_written_by like 'Colin Sims' then 'csims' -when response_written_by like 'Gianmarco Pugliese' then 'gpugliese' -when response_written_by like 'Irfan Haji' then 'ihaji' -when response_written_by like 'Jovana Skrbic' then 'jskrbic' -when response_written_by like 'Maria Dayang' then 'mdayang' -when response_written_by like 'Sandi Topic' then 'stopic' -when response_written_by like 'Muhammad Ismail Bin Abu' then 'MIbabu' -when response_written_by like 'Tayo Oike' then 'toike' -else response_written_by end -- as corresp_link_officer_name_qa -,concat(case -when response_written_by like 'Ayo Falade' then 'AFalade' -when response_written_by like 'Bilal Ahmed Choudhury' then 'BAhmed' -when response_written_by like 'Damien Julian' then 'djulian' -when response_written_by like 'Daniel Gardner' then 'dgardner' -when response_written_by like 'Davide Lagatolla' then 'DLagatolla' -when response_written_by like 'Davide Lagattolla' then 'DLagatolla' -when response_written_by like 'Emma Samson' then 'esamson' -when response_written_by like 'Hamza Patel' then 'hpatel' -when response_written_by like 'Ian Henry' then 'iaHenry' -when response_written_by like 'Imran Ali' then 'Imali' -when response_written_by like 'Jahed Ahmed' then 'JAhmed' -when response_written_by like 'Magnus Magnusson' then 'mmagnusson' -when response_written_by like 'Olabisi Onisemo' then 'OOnisemo' -when response_written_by like 'Olamide Olagbaju' then 'oolagbaju' -when response_written_by like 'Pameta Shakes' then 'PShakes' -when response_written_by like 'Rhys Campbell' then 'rcampbell' -when response_written_by like 'Savva Spanos' then 'sspanos' -when response_written_by like 'Shirley Sunilkumar' then 'ssunilkumar' -when response_written_by like 'Sonia Baxter' then 'sbaxter' -when response_written_by like 'Wasilat Elegbede' then 'WElegbede' -when response_written_by like 'Claire Glover' then 'Claire Glover' -when response_written_by like 'Earle Nottingham' then 'Earle Nottingham' -when response_written_by like 'Edson Passos' then 'EPassos' -when response_written_by like 'Melanie Walters' then 'Melanie Walters' -when response_written_by like 'Nohaad Al-othmani' then 'Nohaad Al-othmani' -when response_written_by like 'Yusuf Yahya' then 'Yusuf Yahya' -when response_written_by like 'Ainsley Henry' then 'ahenry' -when response_written_by like 'Colin Sims' then 'csims' -when response_written_by like 'Gianmarco Pugliese' then 'gpugliese' -when response_written_by like 'Irfan Haji' then 'ihaji' -when response_written_by like 'Jovana Skrbic' then 'jskrbic' -when response_written_by like 'Maria Dayang' then 'mdayang' -when response_written_by like 'Sandi Topic' then 'stopic' -when response_written_by like 'Muhammad Ismail Bin Abu' then 'MIbabu' -when response_written_by like 'Tayo Oike' then 'toike' -else response_written_by end,concat(substr(Cast(response_generated_at as varchar(10)),1, 7), '-01') ) -,concat(response_written_by,concat(substr(Cast(response_generated_at as varchar(10)),1, 7), '-01') ) -,response_written_by -,monthyear -) -, team as ( -select distinct start_date as t_start_date -,end_date as t_end_date -,team as t_team -,team_name as t_team_name -,role as t_role -,forename as t_forename -,surname as t_surname -,full_name as t_full_name -,qa_doc_created_by as t_qa_doc_created_by -,qa_doc_full_name as t_qa_doc_full_name -,post_title as t_post_title -,notes as t_notes -,import_date as t_import_date--* -from parking_correspondence_performance_teams where import_date = (select max(import_date) from parking_correspondence_performance_teams ) -) - - -SELECT ---from qa_total -qa_tot.qa_link_officer_name_corresp -,qa_tot.qatot_unique_id -,qa_tot.qatot_qa_doc_created_by -,qa_tot.MonthYear_qadone -,qa_tot.qatot_total_reviews - ---from correspondence_total -,corresp_tot.corresp_link_officer_name_qa -,corresp_tot.corresptot_qa_unique_id -,corresp_tot.corresptot_unique_id -,corresp_tot.corresptot_response_written_by -,corresp_tot.monthyear_corresp -,corresp_tot.corresptot_total_cases - ---from qa -,case -when qa_doc_created_by like 'AFalade' then 'Ayo Falade' -when qa_doc_created_by like 'BAhmed' then 'Bilal Ahmed Choudhury' ---Claire Glover when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'djulian' then 'Damien Julian' -when qa_doc_created_by like 'dgardner' then 'Daniel Gardner' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagatolla' -when qa_doc_created_by like 'DLagatolla' then 'Davide Lagattolla' ---Earle Nottingham when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'EPassos' then 'Edson Passos'--Edson Passos -when qa_doc_created_by like 'esamson' then 'Emma Samson' -when qa_doc_created_by like 'hpatel' then 'Hamza Patel' -when qa_doc_created_by like 'iaHenry' then 'Ian Henry' -when qa_doc_created_by like 'Imali' then 'Imran Ali' -when qa_doc_created_by like 'JAhmed' then 'Jahed Ahmed' -when qa_doc_created_by like 'mmagnusson' then 'Magnus Magnusson' -when qa_doc_created_by like 'mwalters' then 'Melanie Walters' ---Nohaad Al-othmani when qa_doc_created_by like '' then '' -when qa_doc_created_by like 'OOnisemo' then 'Olabisi Onisemo' -when qa_doc_created_by like 'oolagbaju' then 'Olamide Olagbaju' -when qa_doc_created_by like 'PShakes' then 'Pameta Shakes' -when qa_doc_created_by like 'rcampbell' then 'Rhys Campbell' -when qa_doc_created_by like 'sspanos' then 'Savva Spanos' -when qa_doc_created_by like 'ssunilkumar' then 'Shirley Sunilkumar' -when qa_doc_created_by like 'sbaxter' then 'Sonia Baxter' -when qa_doc_created_by like 'WElegbede' then 'Wasilat Elegbede' -when qa_doc_created_by like 'yyahya' then 'Yusuf Yahya' -when qa_doc_created_by like 'admin' then 'ADMIN' -when qa_doc_created_by like 'albrooks' then 'Alan Brooks' -when qa_doc_created_by like 'bmoloney' then 'bmoloney' -when qa_doc_created_by like 'cbeasley' then 'cbeasley' -when qa_doc_created_by like 'EAbankwa' then 'EAbankwa' -when qa_doc_created_by like 'EOsagiede' then 'EOsagiede' -when qa_doc_created_by like 'khamad-okunnu' then 'khamad-okunnu' -when qa_doc_created_by like 'toike' then 'Tayo Oike' -when qa_doc_created_by like 'MIbabu' then 'Muhammad Ismail Bin Abu' -when qa_doc_created_by like 'stopic' then 'Sandi Topic' -when qa_doc_created_by like 'mdayang' then 'Maria Dayang' -when qa_doc_created_by like 'jskrbic' then 'Jovana Skrbic' -when qa_doc_created_by like 'ihaji' then 'Irfan Haji' -when qa_doc_created_by like 'gpugliese' then 'Gianmarco Pugliese' -when qa_doc_created_by like 'csims' then 'Colin Sims' -when qa_doc_created_by like 'ahenry' then 'Ainsley Henry' -else qa_doc_created_by end as link_officer_name -,* - -FROM liberator_pcn_qa - -left join qa_tot on qa_tot.qatot_unique_id = concat(qa_doc_created_by,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') ) -left join corresp_tot on corresp_tot.corresptot_qa_unique_id = concat(qa_doc_created_by,concat(substr(Cast(qa_done as varchar(10)),1, 7), '-01') ) -left join team on upper(team.t_qa_doc_created_by) = upper(liberator_pcn_qa.qa_doc_created_by) - -where import_date =(SELECT max(import_date) FROM liberator_pcn_qa) - -order by qa_done desc -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_qa": AmazonS3Rawliberator_pcn_qa_node1668440603311, - "parking_correspondence_performance_records_with_pcn": S3bucketrefinedparking_correspondence_performance_records_with_pcn_node1, - "parking_correspondence_performance_teams": parking_raw_zoneparking_correspondence_performance_teams_node1682093516418, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-" - + environment - + "-refined-zone/parking/liberator/parking_correspondence_performance_qa_with_totals_gds/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - compression="snappy", - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="parking_correspondence_performance_qa_with_totals_gds", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn.py b/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn.py deleted file mode 100644 index f124b749e..000000000 --- a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn.py +++ /dev/null @@ -1,499 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_correspondence_performance_records_with_pcn" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/* -parking_correspondence_performance_records_with_pcn.sql -Correspondence Performance records last 13 months with PCN FOI records - --->> "dataplatform-prod-liberator-refined-zone"."parking_correspondence_performance_records_with_pcn" ---<< "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" --- < "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" --- < "parking-raw-zone"."parking_correspondence_performance_teams" - -16/06/2022 - Created -21/04/2023 - added teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing -27/02/2025 - Refactored SQL for migration back to AthenaSQL for dap-airflow -14/04/2025 - Strictly applied column formatting consistent with previous Glue outputs. -16/04/2025 - Workaround to orchestration issue caused by latest "pcnfoidetails_pcn_foi_full" not yet ready -*/ -WITH -team AS ( - SELECT DISTINCT - "start_date" AS t_start_date, - end_date AS t_end_date, - team AS t_team, - team_name AS t_team_name, - "role" AS t_role, - forename AS t_forename, - surname AS t_surname, - full_name AS t_full_name, - qa_doc_created_by AS t_qa_doc_created_by, - qa_doc_full_name AS t_qa_doc_full_name, - post_title AS t_post_title, - notes AS t_notes, - import_date AS t_import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" g - ) -), -liberator AS ( - SELECT *, - - CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3)) AS current_utc_timestamp, - TRY(CAST(date_received AS TIMESTAMP(3))) AS date_received_timestamp, -- pre-filtered by WHERE clause - - IF(whenassigned <> '', -- contains probable valid timestamp - TRY(CAST(whenassigned AS TIMESTAMP(3))), - CAST(NULL AS TIMESTAMP(3)) -- edge case - ) AS whenassigned_timestamp, - - IF(Response_generated_at <> '', -- contains probable valid timestamp - TRY(CAST(Response_generated_at AS TIMESTAMP(3))), - CAST(NULL AS TIMESTAMP(3)) -- edge case - ) AS Response_generated_at_timestamp, - - CAST(CASE WHEN date_received <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), -- under condition of date_received <>'' - CURRENT_DATE - ) - --ELSE NULL - END AS INTEGER) AS days_since_date_received, - - CAST(CASE WHEN date_received <> '' AND whenassigned <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), -- under condition of date_received <>'' - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE) - ) - --ELSE NULL - END AS INTEGER) AS days_since_date_received_whenassigned, - - CAST(CASE WHEN whenassigned <> '' AND response_generated_at = '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE), - CURRENT_DATE - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_whenassigned, - - CAST(CASE WHEN whenassigned <> '' AND response_generated_at <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE), - TRY_CAST(SUBSTR(response_generated_at, 1, 10) AS DATE) - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_whenassigned_response_generated_at, - - CAST(CASE WHEN date_received <> '' AND response_generated_at <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), - TRY_CAST(SUBSTR(response_generated_at, 1, 10) AS DATE) - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_date_received_response_generated_at - - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - WHERE import_Date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" g - ) - AND LENGTH(ticketserialnumber) = 10 -- ticket filter - AND date_received <> '' -- is the overriding condition for "13 months from todays date"! - AND TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE) > CURRENT_DATE - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - AND DATE_DIFF( - 'month', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), - CURRENT_DATE - ) <= 13 -*/ -) -SELECT - CAST(CASE - WHEN l.date_received <> '' AND l.whenassigned = '' - THEN 'Unassigned' - WHEN l.date_received <> '' AND l.whenassigned <> '' AND l.response_generated_at = '' - THEN 'Assigned' - WHEN l.date_received <> '' AND l.whenassigned <> '' AND l.response_generated_at <> '' - THEN 'Responded' - --ELSE NULL - END AS VARCHAR) AS response_status, - - CAST(l.current_utc_timestamp AS VARCHAR) AS current_time_stamp, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS unassigned_time, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.whenassigned_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS to_assigned_time, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_time, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_response_time, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS response_time, - - /*unassigned days*/ - CAST(CASE WHEN l.whenassigned = '' - THEN l.days_since_date_received - --ELSE NULL - END AS VARCHAR) AS unassigned_days, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - THEN - CASE - WHEN l.days_since_date_received <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received > 56 THEN '56 plus days' - --WHEN l.days_since_date_received IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS unassigned_days_group, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - AND l.days_since_date_received <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS unassigned_days_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - AND l.days_since_date_received <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS unassigned_days_kpiTotFourteenLess, - - /*Days to assign*/ - CAST(l.days_since_date_received_whenassigned AS VARCHAR) AS days_to_assign, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - THEN - CASE - WHEN l.days_since_date_received_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_whenassigned > 56 THEN '56 plus days' - --WHEN l.days_since_date_received_whenassigned IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS days_to_assign_group, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - AND l.days_since_date_received_whenassigned <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS Days_to_assign_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - AND l.days_since_date_received_whenassigned <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS Days_to_assign_kpiTotFourteenLess, - - /*assigned in progress days*/ - CAST(l.days_since_whenassigned AS VARCHAR) AS assigned_in_progress_days, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN - CASE - WHEN l.days_since_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned > 56 THEN '56 plus days' - --WHEN l.days_since_whenassigned IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days_group, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS assigned_in_progress_days_kpiTotFiftySixLess, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS assigned_in_progress_days_kpiTotFourteenLess, - - /*assigned response days*/ - CAST(l.days_since_whenassigned_response_generated_at AS VARCHAR) AS assignedResponseDays, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN - CASE - WHEN l.days_since_whenassigned_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned_response_generated_at > 56 THEN '56 plus days' - --WHEN l.days_since_whenassigned_response_generated_at IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays_group, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS assignedResponseDays_kpiTotFiftySixLess, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS assignedResponseDays_kpiTotFourteenLess, - - /*Response days*/ - CAST(l.days_since_date_received_response_generated_at AS VARCHAR) AS ResponseDays, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - THEN - CASE - WHEN l.days_since_date_received_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_response_generated_at > 56 THEN '56 plus days' - --WHEN l.days_since_date_received_response_generated_at IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS ResponseDays_group, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS ResponseDays_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS ResponseDays_kpiTotFourteenLess, - - l.Response_generated_at, --AS VARCHAR - l.Date_Received, --AS VARCHAR - - CAST(SUBSTR(TRY_CAST(l.date_received AS VARCHAR(10)), 1, 7) || '-01' AS VARCHAR) AS MonthYear, - - l.Type AS "type", --AS VARCHAR - l.Serviceable, --AS VARCHAR - l.Service_category, --AS VARCHAR - l.Response_written_by, --AS VARCHAR - l.Letter_template, --AS VARCHAR - l.Action_taken, --AS VARCHAR - l.Related_to_PCN, --AS VARCHAR - l.Cancellation_group, --AS VARCHAR - l.Cancellation_reason, --AS VARCHAR - l.whenassigned, --AS VARCHAR - l.ticketserialnumber, --AS VARCHAR - l.noderef, --AS VARCHAR - TRY_CAST(l.record_created AS TIMESTAMP(3)) AS record_created, -- cast from VARCHAR - l.import_timestamp, --AS VARCHAR - - /*** pcn columns taken from "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ***/ - -- Data sourced from the refined zone needs to be handled carefully... - -- Do not assume the following output column datatypes will be the same as "pcnfoidetails_pcn_foi_full". - -- Being left-joined to "pcnfoidetails_pcn_foi_full" will cause NULLs to appear in these columns. - -- And similar transforms elsewhere require different translations to the ones below... - /* These are the current automated translations developed for this transform:- - TRY_CAST(p.[source_column] AS DATE) AS [target_column], --[position]-- try date to date - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS DATE), '%Y-%m-%d'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try date to string - TRY_CAST(p.[source_column] AS INTEGER) AS [target_column], --[position]-- try int to int - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try timestamp to string - TRY_CAST(p.[source_column] AS TIMESTAMP) AS [target_column], --[position]-- try timestamp to timestamp - */ - -- These translations try to deal with problems without resorting to throwing errors, tolerating a reasonable degree of schema evolution in the "pcnfoidetails_pcn_foi_full" source. - TRY_CAST(p.pcn AS VARCHAR) AS pcn_pcn, --45-- try string to string (default) - TRY_CAST(p.pcnissuedate AS DATE) AS pcn_pcnissuedate, --46-- try date to date - TRY_CAST(p.pcnissuedatetime AS TIMESTAMP) AS pcn_pcnissuedatetime, --47-- try timestamp to timestamp - TRY_CAST(p.pcn_canx_date AS DATE) AS pcn_pcn_canx_date, --48-- try date to date - TRY_CAST(p.cancellationgroup AS VARCHAR) AS pcn_cancellationgroup, --49-- try string to string (default) - TRY_CAST(p.cancellationreason AS VARCHAR) AS pcn_cancellationreason, --50-- try string to string (default) - TRY_CAST(p.pcn_casecloseddate AS DATE) AS pcn_pcn_casecloseddate, --51-- try date to date - TRY_CAST(p.street_location AS VARCHAR) AS pcn_street_location, --52-- try string to string (default) - TRY_CAST(p.whereonlocation AS VARCHAR) AS pcn_whereonlocation, --53-- try string to string (default) - TRY_CAST(p.zone AS VARCHAR) AS pcn_zone, --54-- try string to string (default) - TRY_CAST(p.usrn AS VARCHAR) AS pcn_usrn, --55-- try string to string (default) - TRY_CAST(p.contraventioncode AS VARCHAR) AS pcn_contraventioncode, --56-- try string to string (default) - TRY_CAST(p.contraventionsuffix AS VARCHAR) AS pcn_contraventionsuffix, --57-- try string to string (default) - TRY_CAST(p.debttype AS VARCHAR) AS pcn_debttype, --58-- try string to string (default) - TRY_CAST(p.vrm AS VARCHAR) AS pcn_vrm, --59-- try string to string (default) - TRY_CAST(p.vehiclemake AS VARCHAR) AS pcn_vehiclemake, --60-- try string to string (default) - TRY_CAST(p.vehiclemodel AS VARCHAR) AS pcn_vehiclemodel, --61-- try string to string (default) - TRY_CAST(p.vehiclecolour AS VARCHAR) AS pcn_vehiclecolour, --62-- try string to string (default) - TRY_CAST(p.ceo AS VARCHAR) AS pcn_ceo, --63-- try string to string (default) - TRY_CAST(p.ceodevice AS VARCHAR) AS pcn_ceodevice, --64-- try string to string (default) - TRY_CAST(p.current_30_day_flag AS INTEGER) AS pcn_current_30_day_flag, --65-- try int to int - TRY_CAST(p.isvda AS INTEGER) AS pcn_isvda, --66-- try int to int - TRY_CAST(p.isvoid AS INTEGER) AS pcn_isvoid, --67-- try int to int - TRY_CAST(p.isremoval AS VARCHAR) AS pcn_isremoval, --68-- try string to string (default) - TRY_CAST(p.driverseen AS VARCHAR) AS pcn_driverseen, --69-- try string to string (default) - TRY_CAST(p.allwindows AS VARCHAR) AS pcn_allwindows, --70-- try string to string (default) - TRY_CAST(p.parkedonfootway AS VARCHAR) AS pcn_parkedonfootway, --71-- try string to string (default) - TRY_CAST(p.doctor AS VARCHAR) AS pcn_doctor, --72-- try string to string (default) - TRY_CAST(p.warningflag AS INTEGER) AS pcn_warningflag, --73-- try int to int - TRY_CAST(p.progressionstage AS VARCHAR) AS pcn_progressionstage, --74-- try string to string (default) - TRY_CAST(p.nextprogressionstage AS VARCHAR) AS pcn_nextprogressionstage, --75-- try string to string (default) - TRY_CAST(p.nextprogressionstagestarts AS VARCHAR) AS pcn_nextprogressionstagestarts, --76-- try string to string (default) - TRY_CAST(p.holdreason AS VARCHAR) AS pcn_holdreason, --77-- try string to string (default) - TRY_CAST(p.lib_initial_debt_amount AS VARCHAR) AS pcn_lib_initial_debt_amount, --78-- try string to string (default) - TRY_CAST(p.lib_payment_received AS VARCHAR) AS pcn_lib_payment_received, --79-- try string to string (default) - TRY_CAST(p.lib_write_off_amount AS VARCHAR) AS pcn_lib_write_off_amount, --80-- try string to string (default) - TRY_CAST(p.lib_payment_void AS VARCHAR) AS pcn_lib_payment_void, --81-- try string to string (default) - TRY_CAST(p.lib_payment_method AS VARCHAR) AS pcn_lib_payment_method, --82-- try string to string (default) - TRY_CAST(p.lib_payment_ref AS VARCHAR) AS pcn_lib_payment_ref, --83-- try string to string (default) - TRY_CAST(p.baliff_from AS VARCHAR) AS pcn_baliff_from, --84-- try string to string (default) - TRY_CAST(p.bailiff_to AS VARCHAR) AS pcn_bailiff_to, --85-- try string to string (default) - TRY_CAST(p.bailiff_processedon AS TIMESTAMP) AS pcn_bailiff_processedon, --86-- try timestamp to timestamp - TRY_CAST(p.bailiff_redistributionreason AS VARCHAR) AS pcn_bailiff_redistributionreason, --87-- try string to string (default) - TRY_CAST(p.bailiff AS VARCHAR) AS pcn_bailiff, --88-- try string to string (default) - TRY_CAST(p.warrantissuedate AS TIMESTAMP) AS pcn_warrantissuedate, --89-- try timestamp to timestamp - TRY_CAST(p.allocation AS INTEGER) AS pcn_allocation, --90-- try int to int - TRY_CAST(p.eta_datenotified AS TIMESTAMP) AS pcn_eta_datenotified, --91-- try timestamp to timestamp - TRY_CAST(p.eta_packsubmittedon AS TIMESTAMP) AS pcn_eta_packsubmittedon, --92-- try timestamp to timestamp - TRY_CAST(p.eta_evidencedate AS TIMESTAMP) AS pcn_eta_evidencedate, --93-- try timestamp to timestamp - TRY_CAST(p.eta_adjudicationdate AS TIMESTAMP) AS pcn_eta_adjudicationdate, --94-- try timestamp to timestamp - TRY_CAST(p.eta_appealgrounds AS VARCHAR) AS pcn_eta_appealgrounds, --95-- try string to string (default) - TRY_CAST(p.eta_decisionreceived AS TIMESTAMP) AS pcn_eta_decisionreceived, --96-- try timestamp to timestamp - TRY_CAST(p.eta_outcome AS VARCHAR) AS pcn_eta_outcome, --97-- try string to string (default) - TRY_CAST(p.eta_packsubmittedby AS VARCHAR) AS pcn_eta_packsubmittedby, --98-- try string to string (default) - TRY_CAST(p.cancelledby AS VARCHAR) AS pcn_cancelledby, --99-- try string to string (default) - TRY_CAST(p.registered_keeper_address AS VARCHAR) AS pcn_registered_keeper_address, --100-- try string to string (default) - TRY_CAST(p.current_ticket_address AS VARCHAR) AS pcn_current_ticket_address, --101-- try string to string (default) - TRY_CAST(p.corresp_dispute_flag AS INTEGER) AS pcn_corresp_dispute_flag, --102-- try int to int - TRY_CAST(p.keyworker_corresp_dispute_flag AS INTEGER) AS pcn_keyworker_corresp_dispute_flag, --103-- try int to int - TRY_CAST(p.fin_year_flag AS VARCHAR) AS pcn_fin_year_flag, --104-- try string to string (default) - TRY_CAST(p.fin_year AS VARCHAR) AS pcn_fin_year, --105-- try string to string (default) - TRY_CAST(p.ticket_ref AS VARCHAR) AS pcn_ticket_ref, --106-- try string to string (default) - TRY_CAST(p.nto_printed AS TIMESTAMP) AS pcn_nto_printed, --107-- try timestamp to timestamp - TRY_CAST(p.appeal_accepted AS TIMESTAMP) AS pcn_appeal_accepted, --108-- try timestamp to timestamp - TRY_CAST(p.arrived_in_pound AS TIMESTAMP) AS pcn_arrived_in_pound, --109-- try timestamp to timestamp - TRY_CAST(p.cancellation_reversed AS TIMESTAMP) AS pcn_cancellation_reversed, --110-- try timestamp to timestamp - TRY_CAST(p.cc_printed AS TIMESTAMP) AS pcn_cc_printed, --111-- try timestamp to timestamp - TRY_CAST(p.drr AS TIMESTAMP) AS pcn_drr, --112-- try timestamp to timestamp - TRY_CAST(p.en_printed AS TIMESTAMP) AS pcn_en_printed, --113-- try timestamp to timestamp - TRY_CAST(p.hold_released AS TIMESTAMP) AS pcn_hold_released, --114-- try timestamp to timestamp - TRY_CAST(p.dvla_response AS TIMESTAMP) AS pcn_dvla_response, --115-- try timestamp to timestamp - TRY_CAST(p.dvla_request AS TIMESTAMP) AS pcn_dvla_request, --116-- try timestamp to timestamp - TRY_CAST(p.full_rate_uplift AS TIMESTAMP) AS pcn_full_rate_uplift, --117-- try timestamp to timestamp - TRY_CAST(p.hold_until AS TIMESTAMP) AS pcn_hold_until, --118-- try timestamp to timestamp - TRY_CAST(p.lifted_at AS TIMESTAMP) AS pcn_lifted_at, --119-- try timestamp to timestamp - TRY_CAST(p.lifted_by AS TIMESTAMP) AS pcn_lifted_by, --120-- try timestamp to timestamp - TRY_CAST(p.loaded AS TIMESTAMP) AS pcn_loaded, --121-- try timestamp to timestamp - TRY_CAST(p.nor_sent AS TIMESTAMP) AS pcn_nor_sent, --122-- try timestamp to timestamp - TRY_CAST(p.notice_held AS TIMESTAMP) AS pcn_notice_held, --123-- try timestamp to timestamp - TRY_CAST(p.ofr_printed AS TIMESTAMP) AS pcn_ofr_printed, --124-- try timestamp to timestamp - TRY_CAST(p.pcn_printed AS TIMESTAMP) AS pcn_pcn_printed, --125-- try timestamp to timestamp - TRY_CAST(p.reissue_nto_requested AS TIMESTAMP) AS pcn_reissue_nto_requested, --126-- try timestamp to timestamp - TRY_CAST(p.reissue_pcn AS TIMESTAMP) AS pcn_reissue_pcn, --127-- try timestamp to timestamp - TRY_CAST(p.set_back_to_pre_cc_stage AS TIMESTAMP) AS pcn_set_back_to_pre_cc_stage, --128-- try timestamp to timestamp - TRY_CAST(p.vehicle_released_for_auction AS TIMESTAMP) AS pcn_vehicle_released_for_auction, --129-- try timestamp to timestamp - TRY_CAST(p.warrant_issued AS TIMESTAMP) AS pcn_warrant_issued, --130-- try timestamp to timestamp - TRY_CAST(p.warrant_redistributed AS TIMESTAMP) AS pcn_warrant_redistributed, --131-- try timestamp to timestamp - TRY_CAST(p.warrant_request_granted AS TIMESTAMP) AS pcn_warrant_request_granted, --132-- try timestamp to timestamp - TRY_CAST(p.ad_hoc_vq4_request AS TIMESTAMP) AS pcn_ad_hoc_vq4_request, --133-- try timestamp to timestamp - TRY_CAST(p.paper_vq5_received AS TIMESTAMP) AS pcn_paper_vq5_received, --134-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_buslane AS TIMESTAMP) AS pcn_pcn_extracted_for_buslane, --135-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_pre_debt AS TIMESTAMP) AS pcn_pcn_extracted_for_pre_debt, --136-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_collection AS TIMESTAMP) AS pcn_pcn_extracted_for_collection, --137-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_drr AS TIMESTAMP) AS pcn_pcn_extracted_for_drr, --138-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_cc AS TIMESTAMP) AS pcn_pcn_extracted_for_cc, --139-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_nto AS TIMESTAMP) AS pcn_pcn_extracted_for_nto, --140-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_print AS TIMESTAMP) AS pcn_pcn_extracted_for_print, --141-- try timestamp to timestamp - TRY_CAST(p.warning_notice_extracted_for_print AS TIMESTAMP) AS pcn_warning_notice_extracted_for_print, --142-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_ofr AS TIMESTAMP) AS pcn_pcn_extracted_for_ofr, --143-- try timestamp to timestamp - TRY_CAST(p.pcn_extracted_for_warrant_request AS TIMESTAMP) AS pcn_pcn_extracted_for_warrant_request, --144-- try timestamp to timestamp - TRY_CAST(p.pre_debt_new_debtor_details AS TIMESTAMP) AS pcn_pre_debt_new_debtor_details, --145-- try timestamp to timestamp - TRY_CAST(p.importdattime AS TIMESTAMP) AS pcn_importdattime, --146-- try timestamp to timestamp - TRY_CAST(p.importdatetime AS TIMESTAMP) AS pcn_importdatetime, --147-- try timestamp to timestamp - TRY_CAST(p.import_year AS VARCHAR) AS pcn_import_year, --148-- try string to string (default) - TRY_CAST(p.import_month AS VARCHAR) AS pcn_import_month, --149-- try string to string (default) - TRY_CAST(p.import_day AS VARCHAR) AS pcn_import_day, --150-- try string to string (default) - TRY_CAST(p.import_date AS VARCHAR) AS pcn_import_date, --151-- try string to string (default) - - /* teams columns expanded by above subquery */ - team.*, - - /* Partition columns moved to the end to keep schema alligned */ - l.import_year, - l.import_month, - l.import_day, - l.import_date - -FROM liberator l -- < "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" -LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p -/* - ON p.import_date = l.import_date -- joined on import partition - Fails when "pcnfoidetails_pcn_foi_full" hasn't produced data for l.import_date yet... - ...resulting in empty pcn_ columns in the output. - Airflow's orchestration should avoid this after PCNFOIDetails_PCN_FOI_FULL.sql has been migrated... - ...but for now we'll just use the following simple workaround... -*/ - ON p.import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" g - ) - AND l.ticketserialnumber = p.pcn -- ticketserialnumber is valid -LEFT JOIN team -- < "parking-raw-zone"."parking_correspondence_performance_teams" - ON UPPER(team.t_full_name) = UPPER(l.Response_written_by) -; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime.py b/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime.py deleted file mode 100644 index cde331620..000000000 --- a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime.py +++ /dev/null @@ -1,1054 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_correspondence_performance_records_with_pcn_downtime" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ --->> "dataplatform-prod-liberator-refined-zone"."parking_correspondence_performance_records_with_pcn_downtime" ---< "parking-raw-zone"."parking_correspondence_performance_teams" (data from google spreadsheet) ---< "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" ---<< "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ---< "parking-raw-zone"."parking_officer_downtime" (data from google form) -/* -Correspondence Performance records last 13 months with PCN FOI records -16/06/2022 - Created -21/04/2023 - added teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing -02/10/2023 - modified to union data into one output for Downtime data gathered from Google form https://forms.gle/bB53jAayiZ2Ykwjk6 -22/10/2024 - translated into PrestoSQL to move away from Glue -01/11/2024 + Refactored SQL to separate the inward column conversions from onward business logic using CTE subqueries and allow improved fault handling. - + Downtime's Google Sheets alternate date formats properly handled -28/11/2024 - updated downtime records for t_team details to pull from parking_correspondence_performance_teams instead on leaving blank -12/12/2024 + Integrated the previous downtime update into the refactored version - + Commented out alternative last 13 month filter that caused unexpectedly higher row count -05/03/2025 - Replaced conditional '' empty string outputs with NULL defaults to be consistent with previous Glue/SparkSQL implementations. -14/04/2025 - Strictly applied column formatting consistent with previous Glue outputs. Fixed faulty arithmetic translation of Glue/SparkSQL to AthenaSQL. -16/04/2025 - Workaround orchestration issue caused by latest "pcnfoidetails_pcn_foi_full" not yet ready -28/04/2055 - Checked over logic of conditionally-shown columns -*/ -/*Teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing*/ -WITH -team AS ( - SELECT DISTINCT - start_date AS t_start_date, - end_date AS t_end_date, - team AS t_team, - team_name AS t_team_name, - ROLE AS t_role, - forename AS t_forename, - surname AS t_surname, - full_name AS t_full_name, - qa_doc_created_by AS t_qa_doc_created_by, - qa_doc_full_name AS t_qa_doc_full_name, - post_title AS t_post_title, - notes AS t_notes, - import_date AS t_import_date --* - FROM "parking-raw-zone"."parking_correspondence_performance_teams" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" g - ) -), -liberator_pcn_icdate_received_last13months AS ( - SELECT - *, - CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS current_utc_timestamp, - TRY(CAST(date_received AS TIMESTAMP)) AS date_received_timestamp, -- pre-filtered by WHERE clause - - IF(whenassigned <> '', -- contains probable valid timestamp - TRY(CAST(whenassigned AS TIMESTAMP)), - CAST(NULL AS TIMESTAMP) -- edge case - ) AS whenassigned_timestamp, - - IF(Response_generated_at <> '', -- contains probable valid timestamp - TRY(CAST(Response_generated_at AS TIMESTAMP)), - CAST(NULL AS TIMESTAMP) -- edge case - ) AS Response_generated_at_timestamp, - - CAST(DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - CAST(CURRENT_TIMESTAMP AS DATE) - ) AS BIGINT) AS days_since_date_received, - - CAST(IF( - whenassigned <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_date_recieved_whenassigned, - - CAST(IF( - whenassigned <> '' AND response_generated_at = '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_whenassigned, - - CAST(IF( - whenassigned <> '' AND response_generated_at <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)), - TRY(CAST(SUBSTR(response_generated_at, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_whenassigned_response_generated_at, - - CAST(IF( - response_generated_at <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - TRY(CAST(SUBSTR(response_generated_at, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_date_received_response_generated_at--, - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - WHERE import_Date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" g - ) - AND LENGTH(ticketserialnumber) = 10 -- ticket filter - AND date_received <>'' -- is the overriding condition for "13 months from todays date"! - AND CAST(SUBSTR(date_received, 1, 10) AS DATE) > CAST(CURRENT_TIMESTAMP AS DATE) - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - DATE_DIFF( - 'month', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ) <= 13 -*/ -), -liberator_with_team AS ( - SELECT - /*Fields for union downtime data*/ - CAST('' AS VARCHAR) AS downtime_timestamp, - CAST('' AS VARCHAR) AS downtime_email_address, - CAST('' AS VARCHAR) AS officer_s_first_name, - CAST('' AS VARCHAR) AS officer_s_last_name, - CAST('' AS VARCHAR) AS liberator_system_username, - CAST('' AS VARCHAR) AS liberator_system_id, - CAST('' AS VARCHAR) AS import_datetime, - CAST('' AS VARCHAR) AS multiple_downtime_days_flag, - CAST('' AS VARCHAR) AS response_secs, - CAST('' AS VARCHAR) AS response_mins, - CAST('' AS VARCHAR) AS response_hours, - CAST('' AS VARCHAR) AS response_days_plus_one, - CAST('' AS VARCHAR) AS downtime_total_non_working_mins_with_lunch, - CAST('' AS VARCHAR) AS downtime_total_non_working_mins, - CAST('' AS VARCHAR) AS downtime_total_working_mins_with_lunch, - CAST('' AS VARCHAR) AS downtime_total_working_mins, - CAST('' AS VARCHAR) AS downtime_total_working_mins_with_lunch_net, - CAST('' AS VARCHAR) AS downtime_total_working_mins_net, - CAST('' AS VARCHAR) AS start_date_time, - CAST('' AS VARCHAR) AS startdate, - CAST('' AS VARCHAR) AS start_date_datetime, - CAST('' AS VARCHAR) AS end_date_time, - CAST('' AS VARCHAR) AS enddate, - CAST('' AS VARCHAR) AS end_date_datetime, - - /*Liberator Incoming Correspondence Data*/ - CAST(CASE -- having already asserted date_received <>'' - WHEN l.whenassigned = '' THEN 'Unassigned' - WHEN l.whenassigned <> '' AND l.response_generated_at = '' THEN 'Assigned' - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' THEN 'Responded' - --ELSE NULL - END AS VARCHAR) AS response_status, - - CAST(CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS VARCHAR) AS Current_time_stamp, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS unassigned_time, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.whenassigned_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS to_assigned_time, - - CAST(CASE - WHEN l.whenassigned <> '' - AND l.response_generated_at = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_time, - - CAST(CASE - WHEN l.whenassigned <> '' - AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_response_time, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS response_time, - - /*unassigned days*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN CAST(days_since_date_received AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS unassigned_days, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN CASE - WHEN l.days_since_date_received <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS unassigned_days_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - AND l.days_since_date_received <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS unassigned_days_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - AND l.days_since_date_received <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS unassigned_days_kpiTotFourteenLess, - - /*Days to assign*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN CAST(l.days_since_date_recieved_whenassigned AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS Days_to_assign, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN CASE - WHEN l.days_since_date_recieved_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_recieved_whenassigned > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS Days_to_assign_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - AND l.days_since_date_recieved_whenassigned <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS Days_to_assign_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - AND l.days_since_date_recieved_whenassigned <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS Days_to_assign_kpiTotFourteenLess, - - /*assigned in progress days*/ - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN CAST(l.days_since_whenassigned AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN CASE - WHEN l.days_since_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days_group, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assigned_in_progress_days_kpiTotFiftySixLess, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assigned_in_progress_days_kpiTotFourteenLess, - - /*assigned response days*/ - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN CAST(l.days_since_whenassigned_response_generated_at AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN CASE - WHEN l.days_since_whenassigned_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned_response_generated_at > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays_group, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assignedResponseDays_kpiTotFiftySixLess, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assignedResponseDays_kpiTotFourteenLess, - - /*Response days*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN CAST(l.days_since_date_received_response_generated_at AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS ResponseDays, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN CASE - WHEN l.days_since_date_received_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_response_generated_at > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS ResponseDays_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS ResponseDays_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS ResponseDays_kpiTotFourteenLess, - - Response_generated_at, - Date_Received, - - SUBSTR(CAST(l.date_received AS VARCHAR (10)), 1, 7) || '-01' AS MonthYear, - - l.Type AS "Type", - l.Serviceable, - l.Service_category, - l.Response_written_by, - l.Letter_template, - l.Action_taken, - l.Related_to_PCN, - l.Cancellation_group, - l.Cancellation_reason, - l.whenassigned, - l.ticketserialnumber, - l.noderef, - CAST(l.record_created AS VARCHAR) AS record_created, - - l.import_timestamp, - - /*pcn data*/ - /*** pcn columns taken from "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ***/ - -- Data sourced from the refined zone needs to be handled carefully... - -- Do not assume the following output column datatypes will be the same as "pcnfoidetails_pcn_foi_full". - -- Being left-joined to "pcnfoidetails_pcn_foi_full" will cause NULLs to appear in these columns. - -- And similar transforms elsewhere require different translations to the ones below... - /* These are the current automated translations developed for this transform:- - TRY_CAST(p.[source_column] AS DATE) AS [target_column], --[position]-- try date to date - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try int to string - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS DATE), '%Y-%m-%d'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try date to string - TRY_CAST(p.[source_column] AS INTEGER) AS [target_column], --[position]-- try int to int - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try timestamp to string - TRY_CAST(p.[source_column] AS TIMESTAMP) AS [target_column], --[position]-- try timestamp to timestamp - */ - -- These translations try to deal with problems without resorting to throwing errors, tolerating a reasonable degree of schema evolution in the "pcnfoidetails_pcn_foi_full" source. - -- For example, date and timestamp string formatting works with either string, date and timestamp source columns and works like this... - /* If source can be cast to a date then it can be correctly date-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS DATE), '%Y-%m-%d'), - CAST(p.xxxx AS VARCHAR) --...and can be extended to handle other date formats. - ) AS pcn_xxxx, -- from DATE - */ - /* If source can be cast to a timestamp then it can be correctly timestamp-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), - CAST(p.xxxx AS VARCHAR) -- Alternative for transforming a time-zoned timestamp... - --...COALESCE(DATE_FORMAT(TRY_CAST(TRY(From_iso8601_timestamp(p.xxxx)) AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.xxxx AS VARCHAR)) - --...and can be extended to handle other timestamp formats. - ) AS pcn_xxxx, -- from TIMESTAMP - */ - TRY_CAST(p.pcn AS VARCHAR) AS pcn_pcn, --69-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedate AS DATE), '%Y-%m-%d'), CAST(p.pcnissuedate AS VARCHAR)) AS pcn_pcnissuedate, --70-- try date to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcnissuedatetime AS VARCHAR)) AS pcn_pcnissuedatetime, --71-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_canx_date AS DATE), '%Y-%m-%d'), CAST(p.pcn_canx_date AS VARCHAR)) AS pcn_pcn_canx_date, --72-- try date to string - TRY_CAST(p.cancellationgroup AS VARCHAR) AS pcn_cancellationgroup, --73-- try string to string (default) - TRY_CAST(p.cancellationreason AS VARCHAR) AS pcn_cancellationreason, --74-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_casecloseddate AS DATE), '%Y-%m-%d'), CAST(p.pcn_casecloseddate AS VARCHAR)) AS pcn_pcn_casecloseddate, --75-- try date to string - TRY_CAST(p.street_location AS VARCHAR) AS pcn_street_location, --76-- try string to string (default) - TRY_CAST(p.whereonlocation AS VARCHAR) AS pcn_whereonlocation, --77-- try string to string (default) - TRY_CAST(p.zone AS VARCHAR) AS pcn_zone, --78-- try string to string (default) - TRY_CAST(p.usrn AS VARCHAR) AS pcn_usrn, --79-- try string to string (default) - TRY_CAST(p.contraventioncode AS VARCHAR) AS pcn_contraventioncode, --80-- try string to string (default) - TRY_CAST(p.contraventionsuffix AS VARCHAR) AS pcn_contraventionsuffix, --81-- try string to string (default) - TRY_CAST(p.debttype AS VARCHAR) AS pcn_debttype, --82-- try string to string (default) - TRY_CAST(p.vrm AS VARCHAR) AS pcn_vrm, --83-- try string to string (default) - TRY_CAST(p.vehiclemake AS VARCHAR) AS pcn_vehiclemake, --84-- try string to string (default) - TRY_CAST(p.vehiclemodel AS VARCHAR) AS pcn_vehiclemodel, --85-- try string to string (default) - TRY_CAST(p.vehiclecolour AS VARCHAR) AS pcn_vehiclecolour, --86-- try string to string (default) - TRY_CAST(p.ceo AS VARCHAR) AS pcn_ceo, --87-- try string to string (default) - TRY_CAST(p.ceodevice AS VARCHAR) AS pcn_ceodevice, --88-- try string to string (default) - TRY_CAST(p.current_30_day_flag AS VARCHAR) AS pcn_current_30_day_flag, --89-- try int to string - TRY_CAST(p.isvda AS VARCHAR) AS pcn_isvda, --90-- try int to string - TRY_CAST(p.isvoid AS VARCHAR) AS pcn_isvoid, --91-- try int to string - TRY_CAST(p.isremoval AS VARCHAR) AS pcn_isremoval, --92-- try string to string (default) - TRY_CAST(p.driverseen AS VARCHAR) AS pcn_driverseen, --93-- try string to string (default) - TRY_CAST(p.allwindows AS VARCHAR) AS pcn_allwindows, --94-- try string to string (default) - TRY_CAST(p.parkedonfootway AS VARCHAR) AS pcn_parkedonfootway, --95-- try string to string (default) - TRY_CAST(p.doctor AS VARCHAR) AS pcn_doctor, --96-- try string to string (default) - TRY_CAST(p.warningflag AS VARCHAR) AS pcn_warningflag, --97-- try int to string - TRY_CAST(p.progressionstage AS VARCHAR) AS pcn_progressionstage, --98-- try string to string (default) - TRY_CAST(p.nextprogressionstage AS VARCHAR) AS pcn_nextprogressionstage, --99-- try string to string (default) - TRY_CAST(p.nextprogressionstagestarts AS VARCHAR) AS pcn_nextprogressionstagestarts, --100-- try string to string (default) - TRY_CAST(p.holdreason AS VARCHAR) AS pcn_holdreason, --101-- try string to string (default) - TRY_CAST(p.lib_initial_debt_amount AS VARCHAR) AS pcn_lib_initial_debt_amount, --102-- try string to string (default) - TRY_CAST(p.lib_payment_received AS VARCHAR) AS pcn_lib_payment_received, --103-- try string to string (default) - TRY_CAST(p.lib_write_off_amount AS VARCHAR) AS pcn_lib_write_off_amount, --104-- try string to string (default) - TRY_CAST(p.lib_payment_void AS VARCHAR) AS pcn_lib_payment_void, --105-- try string to string (default) - TRY_CAST(p.lib_payment_method AS VARCHAR) AS pcn_lib_payment_method, --106-- try string to string (default) - TRY_CAST(p.lib_payment_ref AS VARCHAR) AS pcn_lib_payment_ref, --107-- try string to string (default) - TRY_CAST(p.baliff_from AS VARCHAR) AS pcn_baliff_from, --108-- try string to string (default) - TRY_CAST(p.bailiff_to AS VARCHAR) AS pcn_bailiff_to, --109-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.bailiff_processedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.bailiff_processedon AS VARCHAR)) AS pcn_bailiff_processedon, --110-- try timestamp to string - TRY_CAST(p.bailiff_redistributionreason AS VARCHAR) AS pcn_bailiff_redistributionreason, --111-- try string to string (default) - TRY_CAST(p.bailiff AS VARCHAR) AS pcn_bailiff, --112-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.warrantissuedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrantissuedate AS VARCHAR)) AS pcn_warrantissuedate, --113-- try timestamp to string - TRY_CAST(p.allocation AS VARCHAR) AS pcn_allocation, --114-- try int to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_datenotified AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_datenotified AS VARCHAR)) AS pcn_eta_datenotified, --115-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_packsubmittedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_packsubmittedon AS VARCHAR)) AS pcn_eta_packsubmittedon, --116-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_evidencedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_evidencedate AS VARCHAR)) AS pcn_eta_evidencedate, --117-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_adjudicationdate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_adjudicationdate AS VARCHAR)) AS pcn_eta_adjudicationdate, --118-- try timestamp to string - TRY_CAST(p.eta_appealgrounds AS VARCHAR) AS pcn_eta_appealgrounds, --119-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_decisionreceived AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_decisionreceived AS VARCHAR)) AS pcn_eta_decisionreceived, --120-- try timestamp to string - TRY_CAST(p.eta_outcome AS VARCHAR) AS pcn_eta_outcome, --121-- try string to string (default) - TRY_CAST(p.eta_packsubmittedby AS VARCHAR) AS pcn_eta_packsubmittedby, --122-- try string to string (default) - TRY_CAST(p.cancelledby AS VARCHAR) AS pcn_cancelledby, --123-- try string to string (default) - TRY_CAST(p.registered_keeper_address AS VARCHAR) AS pcn_registered_keeper_address, --124-- try string to string (default) - TRY_CAST(p.current_ticket_address AS VARCHAR) AS pcn_current_ticket_address, --125-- try string to string (default) - TRY_CAST(p.corresp_dispute_flag AS VARCHAR) AS pcn_corresp_dispute_flag, --126-- try int to string - TRY_CAST(p.keyworker_corresp_dispute_flag AS VARCHAR) AS pcn_keyworker_corresp_dispute_flag, --127-- try int to string - TRY_CAST(p.fin_year_flag AS VARCHAR) AS pcn_fin_year_flag, --128-- try string to string (default) - TRY_CAST(p.fin_year AS VARCHAR) AS pcn_fin_year, --129-- try string to string (default) - TRY_CAST(p.ticket_ref AS VARCHAR) AS pcn_ticket_ref, --130-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.nto_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nto_printed AS VARCHAR)) AS pcn_nto_printed, --131-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.appeal_accepted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.appeal_accepted AS VARCHAR)) AS pcn_appeal_accepted, --132-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.arrived_in_pound AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.arrived_in_pound AS VARCHAR)) AS pcn_arrived_in_pound, --133-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cancellation_reversed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cancellation_reversed AS VARCHAR)) AS pcn_cancellation_reversed, --134-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cc_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cc_printed AS VARCHAR)) AS pcn_cc_printed, --135-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.drr AS VARCHAR)) AS pcn_drr, --136-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.en_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.en_printed AS VARCHAR)) AS pcn_en_printed, --137-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_released AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_released AS VARCHAR)) AS pcn_hold_released, --138-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_response AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_response AS VARCHAR)) AS pcn_dvla_response, --139-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_request AS VARCHAR)) AS pcn_dvla_request, --140-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.full_rate_uplift AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.full_rate_uplift AS VARCHAR)) AS pcn_full_rate_uplift, --141-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_until AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_until AS VARCHAR)) AS pcn_hold_until, --142-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_at AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_at AS VARCHAR)) AS pcn_lifted_at, --143-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_by AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_by AS VARCHAR)) AS pcn_lifted_by, --144-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.loaded AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.loaded AS VARCHAR)) AS pcn_loaded, --145-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.nor_sent AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nor_sent AS VARCHAR)) AS pcn_nor_sent, --146-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.notice_held AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.notice_held AS VARCHAR)) AS pcn_notice_held, --147-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ofr_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ofr_printed AS VARCHAR)) AS pcn_ofr_printed, --148-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_printed AS VARCHAR)) AS pcn_pcn_printed, --149-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_nto_requested AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_nto_requested AS VARCHAR)) AS pcn_reissue_nto_requested, --150-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_pcn AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_pcn AS VARCHAR)) AS pcn_reissue_pcn, --151-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.set_back_to_pre_cc_stage AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.set_back_to_pre_cc_stage AS VARCHAR)) AS pcn_set_back_to_pre_cc_stage, --152-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.vehicle_released_for_auction AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.vehicle_released_for_auction AS VARCHAR)) AS pcn_vehicle_released_for_auction, --153-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_issued AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_issued AS VARCHAR)) AS pcn_warrant_issued, --154-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_redistributed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_redistributed AS VARCHAR)) AS pcn_warrant_redistributed, --155-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_request_granted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_request_granted AS VARCHAR)) AS pcn_warrant_request_granted, --156-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ad_hoc_vq4_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ad_hoc_vq4_request AS VARCHAR)) AS pcn_ad_hoc_vq4_request, --157-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.paper_vq5_received AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.paper_vq5_received AS VARCHAR)) AS pcn_paper_vq5_received, --158-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_buslane AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_buslane AS VARCHAR)) AS pcn_pcn_extracted_for_buslane, --159-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_pre_debt AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_pre_debt AS VARCHAR)) AS pcn_pcn_extracted_for_pre_debt, --160-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_collection AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_collection AS VARCHAR)) AS pcn_pcn_extracted_for_collection, --161-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_drr AS VARCHAR)) AS pcn_pcn_extracted_for_drr, --162-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_cc AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_cc AS VARCHAR)) AS pcn_pcn_extracted_for_cc, --163-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_nto AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_nto AS VARCHAR)) AS pcn_pcn_extracted_for_nto, --164-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_print AS VARCHAR)) AS pcn_pcn_extracted_for_print, --165-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warning_notice_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warning_notice_extracted_for_print AS VARCHAR)) AS pcn_warning_notice_extracted_for_print, --166-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_ofr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_ofr AS VARCHAR)) AS pcn_pcn_extracted_for_ofr, --167-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_warrant_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_warrant_request AS VARCHAR)) AS pcn_pcn_extracted_for_warrant_request, --168-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pre_debt_new_debtor_details AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pre_debt_new_debtor_details AS VARCHAR)) AS pcn_pre_debt_new_debtor_details, --169-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdattime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdattime AS VARCHAR)) AS pcn_importdattime, --170-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdatetime AS VARCHAR)) AS pcn_importdatetime, --171-- try timestamp to string - TRY_CAST(p.import_year AS VARCHAR) AS pcn_import_year, --172-- try string to string (default) - TRY_CAST(p.import_month AS VARCHAR) AS pcn_import_month, --173-- try string to string (default) - TRY_CAST(p.import_day AS VARCHAR) AS pcn_import_day, --174-- try string to string (default) - TRY_CAST(p.import_date AS VARCHAR) AS pcn_import_date, --175-- try string to string (default) - - /* teams */ - t.t_start_date, - t.t_end_date, - t.t_team, - t.t_team_name, - t.t_role, - t.t_forename, - t.t_surname, - t.t_full_name, - t.t_qa_doc_created_by, - t.t_qa_doc_full_name, - t.t_post_title, - t.t_notes, - t.t_import_date, - - /* Partition columns moved to the end to keep schema alligned */ - l.import_year, - l.import_month, - l.import_day, - l.import_date - - FROM liberator_pcn_icdate_received_last13months l - LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p - /* - ON p.import_date = l.import_date -- joined on import partition - Fails when "pcnfoidetails_pcn_foi_full" hasn't produced data for l.import_date yet... - ...resulting in empty pcn_ columns in the output. - Airflow's orchestration should avoid this after PCNFOIDetails_PCN_FOI_FULL.sql has been migrated... - ...but for now we'll just use the following simple workaround... - */ - ON p.import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" g - ) - AND l.ticketserialnumber = p.pcn -- ticketserialnumber is valid - LEFT JOIN team t - ON UPPER(t.t_full_name) = UPPER(l.Response_written_by) -), - -downtime_start_date_end_date_last13months AS ( - SELECT *, - - IF(start_date LIKE '%/%/%', -- contains possible valid timestamp - TRY(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')), --> TIMESTAMP with time zone - IF(start_date LIKE '%-%-%', -- contains other possible valid timestamp - TRY(PARSE_DATETIME(start_date, 'yyyy-MM-dd HH:mm:ss')), --> TIMESTAMP with time zone - CAST(NULL AS TIMESTAMP) -- edge case - ) - ) AS start_timestampZ, - - IF(end_date LIKE '%/%/%', -- contains possible valid timestamp - TRY(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')), --> TIMESTAMP with time zone - IF(end_date LIKE '%-%-%', -- contains other possible valid timestamp - TRY(PARSE_DATETIME(end_date, 'yyyy-MM-dd HH:mm:ss')), --> TIMESTAMP with time zone - CAST(NULL AS TIMESTAMP) -- edge case - ) - ) AS end_timestampZ--, - - FROM "parking-raw-zone"."parking_officer_downtime" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_officer_downtime" g - ) - AND "timestamp" NOT LIKE '' - AND "timestamp" NOT LIKE '#REF!' - AND CAST(SUBSTR(CAST("timestamp" as VARCHAR(30)), 1, 10) AS DATE) > CAST(CURRENT_TIMESTAMP AS DATE) - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - DATE_DIFF( - 'month', - TRY(CAST(SUBSTR("timestamp", 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ) <= 13 -*/ -), - -downtime AS ( - SELECT *, - /* - TO_UNIXTIME(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')) AS start_end_unixtime_seconds, - TO_UNIXTIME(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')) AS end_unixtime_seconds, - - TO_UNIXTIME(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')) - - TO_UNIXTIME(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')) AS downtime_secs, - - -- downtime_secs / 60 AS downtime_mins - -- downtime_secs / 3600 AS downtime_hours - */ - - DATE_DIFF( - 'second', - start_timestampZ, - end_timestampZ - ) AS downtime_secs, - - DATE_DIFF( - 'minute', - start_timestampZ, - end_timestampZ - ) AS downtime_mins, - - DATE_DIFF( - 'hour', - start_timestampZ, - end_timestampZ - ) AS downtime_hours, - - DATE_DIFF( - 'day', - start_timestampZ, - end_timestampZ - ) AS downtime_days, - - TRIM(officer_s_first_name) || ' ' || TRIM(officer_s_last_name) AS Response_written_by --downtime officer full name - - FROM downtime_start_date_end_date_last13months -), - -google_form AS ( - --Downtime data gathered from Google form https://forms.gle/bB53jAayiZ2Ykwjk6 - SELECT - CAST(d."timestamp" AS VARCHAR) AS downtime_timestamp, - d.email_address AS downtime_email_address, - TRIM(d.officer_s_first_name) AS officer_s_first_name, - TRIM(d.officer_s_last_name) AS officer_s_last_name, - d.liberator_system_username, - d.liberator_system_id, - CAST(d.import_datetime AS VARCHAR) AS import_datetime, - - /* Throughout asserts... - "timestamp" NOT LIKE '' - AND "timestamp" NOT LIKE '#REF!' - */ - CAST(CASE - WHEN d.downtime_mins > 1440 /* downtime mins (response_mins) */ - THEN '1' - ELSE '0' - END AS VARCHAR) AS multiple_downtime_days_flag, - - -- Because the Glue job's response_secs string output was cast from a bigint returned from unix_timestamp... - -- ...there was no need to cast it to a DOUBLE here... - CAST(d.downtime_secs AS VARCHAR) AS response_secs, - -- ...unlike the products below where SparkSQL and AthenaSQL differed in their arithmetic outcomes... - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input value was cast beforehand to DOUBLE. - CAST(CAST(d.downtime_secs AS DOUBLE)/60 AS VARCHAR) AS REJECTED_response_mins, - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(d.downtime_mins AS DOUBLE) AS VARCHAR) AS response_mins, - /* But might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(d.downtime_mins AS VARCHAR) AS ALTERNATIVE_response_mins, - */ - - -- We are keeping this version because it produces closest to the original Glue output... - -- Though beware, several decimal places may be produced in the VARCHAR output... - CAST(CAST(d.downtime_secs AS DOUBLE)/3600 AS VARCHAR) AS response_hours, - /* But might the following version be the better solution? - -- This does not produce the fractional mantissa because it is an integer calculation from start to finish... - CAST(d.downtime_hours AS VARCHAR) AS ALTERNATIVE_response_hours, - */ - - /*Downtime calendar days*/ - -- Warning: A small number outlier cases, producing negative values in original Glue output are also produced here... - CAST(d.downtime_days + 1 AS VARCHAR) AS response_days_plus_one, -- downtime days plus one calendar day - - /*Downtime non working mins*/ - CAST(CASE - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1 ) * 948 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 948 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_non_working_mins_with_lunch, --if greater than 1440 mins then (days + 1) * 948 " - - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 1008 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 1008 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_non_working_mins, --if greater than 1440 mins then (days + 1) * 1008 " - - /*Downtime working mins*/ - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 492 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_working_mins_with_lunch, -- 492 mins = 8hrs 12 mins working day including lunch" - - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 432 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_working_mins, -- 432 mins = 7hrs 12mins working hours" - - /* Downtime working mins net (less downtime mins)*/ - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input values were cast beforehand to DOUBLE... - CAST(CASE -- downtime mins (response_mins) - WHEN CAST(d.downtime_secs AS DOUBLE)/60 > 1440 THEN (CAST(d.downtime_secs AS DOUBLE)/86400 + 1) * 492 -- response_days_plus_one - WHEN CAST(d.downtime_secs AS DOUBLE)/60 <= 1440 THEN 492 - CAST(d.downtime_secs AS DOUBLE)/60 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS REJECTED_downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 - WHEN d.downtime_mins <= 1440 THEN 492 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS DOUBLE) AS VARCHAR) AS downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - /* So might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 - WHEN d.downtime_mins <= 1440 THEN 492 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS ALTERNATIVE_downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - */ - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input values were cast beforehand to DOUBLE... - CAST(CASE -- downtime mins (response_mins) - WHEN CAST(d.downtime_secs AS DOUBLE)/60 > 1440 THEN (CAST(d.downtime_secs AS DOUBLE)/86400 + 1) * 432 -- response_days_plus_one - WHEN CAST(d.downtime_secs AS DOUBLE)/60 <= 1440 THEN 432 - CAST(d.downtime_secs AS DOUBLE)/60 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS REJECTED_downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 -- response_days_plus_one - WHEN d.downtime_mins <= 1440 THEN 432 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS DOUBLE) AS VARCHAR) AS downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - /* So might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 -- response_days_plus_one - WHEN d.downtime_mins <= 1440 THEN 432 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS ALTERNATIVE_downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - */ - - CAST(SUBSTR(CAST(d.start_date AS VARCHAR), 12, 5) AS VARCHAR) AS start_date_time, - CAST(SUBSTR(CAST(d.start_date AS VARCHAR), 1, 10) AS VARCHAR) AS startdate, - - CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS VARCHAR) AS start_date_datetime, - - CAST(SUBSTR(CAST(d.end_date AS VARCHAR), 12, 5) AS VARCHAR) AS end_date_time, - CAST(SUBSTR(CAST(d.end_date AS VARCHAR), 1, 10) AS VARCHAR) AS enddate, - - CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS VARCHAR) AS end_date_datetime, - - CAST('Downtime' AS VARCHAR) AS response_status, - CAST(CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS VARCHAR) AS Current_time_stamp, - - /*Liberator Incoming Correspondence Data*/ - CAST('' AS VARCHAR) AS unassigned_time, - CAST('' AS VARCHAR) AS to_assigned_time, - CAST('' AS VARCHAR) AS assigned_in_progress_time, - CAST('' AS VARCHAR) AS assigned_response_time, - - -- Is there a better way to do this? Substring captures only the minutes... - 'INTERVAL ''' - || REGEXP_REPLACE(CAST( - TRY(CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS TIMESTAMP)) - - TRY(CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS TIMESTAMP)) - AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - AS response_time, --as downtime_duration --line 30 - - CAST('' AS VARCHAR) AS unassigned_days, - CAST('' AS VARCHAR) AS unassigned_days_group, - CAST('' AS VARCHAR) AS unassigned_days_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS unassigned_days_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS Days_to_assign, - CAST('' AS VARCHAR) AS Days_to_assign_group, - CAST('' AS VARCHAR) AS Days_to_assign_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS Days_to_assign_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS assigned_in_progress_days, - CAST('' AS VARCHAR) AS assigned_in_progress_days_group, - CAST('' AS VARCHAR) AS assigned_in_progress_days_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS assigned_in_progress_days_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS assignedResponseDays, - CAST('' AS VARCHAR) AS assignedResponseDays_group, - CAST('' AS VARCHAR) AS assignedResponseDays_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS assignedResponseDays_kpiTotFourteenLess, - - CAST(DATE_DIFF( - 'day', - CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 10 - ) AS DATE), - CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 10 - ) AS DATE) - ) AS VARCHAR) AS ResponseDays, --as downtime_duration in days - - CAST('' AS VARCHAR) AS ResponseDays_group, - CAST('' AS VARCHAR) AS ResponseDays_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS ResponseDays_kpiTotFourteenLess, - - d.end_date AS Response_generated_at, - d.start_date AS Date_Received, - - SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 7 - ) || '-01' AS MonthYear, - - 'Downtime' AS "Type", - - d.downtime AS Serviceable, -- downtime --,'Downtime' as Serviceable -- downtime - - CAST('' AS VARCHAR) AS Service_category, - - d.Response_written_by, --downtime officer full name - - CAST('' AS VARCHAR) AS Letter_template, - CAST('' AS VARCHAR) AS Action_taken, - CAST('' AS VARCHAR) AS Related_to_PCN, - CAST('' AS VARCHAR) AS Cancellation_group, - CAST('' AS VARCHAR) AS Cancellation_reason, - CAST('' AS VARCHAR) AS whenassigned, - CAST('' AS VARCHAR) AS ticketserialnumber, - CAST('' AS VARCHAR) AS noderef, - CAST('' AS VARCHAR) AS record_created, - d.import_timestamp, - - /*pcn data*/ - -- The original Glue SparkSQL had all of these pcn columns output as '' empty strings which either... - -- ...conflicted with the output column datatypes configured by Glue - -- ...and/or conflicted with the column translations done earlier in the SQL script. - /* Instead here, the null or empty translations are deliberately set to depend on the target datatype... - CAST(NULL AS DATE) AS [target_column], --[position]-- null date - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS INTEGER) AS [target_column], --[position]-- null int - CAST('' AS VARCHAR) AS [target_column], --[position]-- empty string - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS TIMESTAMP) AS [target_column], --[position]-- null timestamp - */ - CAST('' AS VARCHAR) AS pcn_pcn, --69-- empty string - CAST(NULL AS VARCHAR) AS pcn_pcnissuedate, --70-- null string - CAST(NULL AS VARCHAR) AS pcn_pcnissuedatetime, --71-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_canx_date, --72-- null string - CAST('' AS VARCHAR) AS pcn_cancellationgroup, --73-- empty string - CAST('' AS VARCHAR) AS pcn_cancellationreason, --74-- empty string - CAST(NULL AS VARCHAR) AS pcn_pcn_casecloseddate, --75-- null string - CAST('' AS VARCHAR) AS pcn_street_location, --76-- empty string - CAST('' AS VARCHAR) AS pcn_whereonlocation, --77-- empty string - CAST('' AS VARCHAR) AS pcn_zone, --78-- empty string - CAST('' AS VARCHAR) AS pcn_usrn, --79-- empty string - CAST('' AS VARCHAR) AS pcn_contraventioncode, --80-- empty string - CAST('' AS VARCHAR) AS pcn_contraventionsuffix, --81-- empty string - CAST('' AS VARCHAR) AS pcn_debttype, --82-- empty string - CAST('' AS VARCHAR) AS pcn_vrm, --83-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclemake, --84-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclemodel, --85-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclecolour, --86-- empty string - CAST('' AS VARCHAR) AS pcn_ceo, --87-- empty string - CAST('' AS VARCHAR) AS pcn_ceodevice, --88-- empty string - CAST(NULL AS VARCHAR) AS pcn_current_30_day_flag, --89-- null string - CAST(NULL AS VARCHAR) AS pcn_isvda, --90-- null string - CAST(NULL AS VARCHAR) AS pcn_isvoid, --91-- null string - CAST('' AS VARCHAR) AS pcn_isremoval, --92-- empty string - CAST('' AS VARCHAR) AS pcn_driverseen, --93-- empty string - CAST('' AS VARCHAR) AS pcn_allwindows, --94-- empty string - CAST('' AS VARCHAR) AS pcn_parkedonfootway, --95-- empty string - CAST('' AS VARCHAR) AS pcn_doctor, --96-- empty string - CAST(NULL AS VARCHAR) AS pcn_warningflag, --97-- null string - CAST('' AS VARCHAR) AS pcn_progressionstage, --98-- empty string - CAST('' AS VARCHAR) AS pcn_nextprogressionstage, --99-- empty string - CAST('' AS VARCHAR) AS pcn_nextprogressionstagestarts, --100-- empty string - CAST('' AS VARCHAR) AS pcn_holdreason, --101-- empty string - CAST('' AS VARCHAR) AS pcn_lib_initial_debt_amount, --102-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_received, --103-- empty string - CAST('' AS VARCHAR) AS pcn_lib_write_off_amount, --104-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_void, --105-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_method, --106-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_ref, --107-- empty string - CAST('' AS VARCHAR) AS pcn_baliff_from, --108-- empty string - CAST('' AS VARCHAR) AS pcn_bailiff_to, --109-- empty string - CAST(NULL AS VARCHAR) AS pcn_bailiff_processedon, --110-- null string - CAST('' AS VARCHAR) AS pcn_bailiff_redistributionreason, --111-- empty string - CAST('' AS VARCHAR) AS pcn_bailiff, --112-- empty string - CAST(NULL AS VARCHAR) AS pcn_warrantissuedate, --113-- null string - CAST(NULL AS VARCHAR) AS pcn_allocation, --114-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_datenotified, --115-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_packsubmittedon, --116-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_evidencedate, --117-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_adjudicationdate, --118-- null string - CAST('' AS VARCHAR) AS pcn_eta_appealgrounds, --119-- empty string - CAST(NULL AS VARCHAR) AS pcn_eta_decisionreceived, --120-- null string - CAST('' AS VARCHAR) AS pcn_eta_outcome, --121-- empty string - CAST('' AS VARCHAR) AS pcn_eta_packsubmittedby, --122-- empty string - CAST('' AS VARCHAR) AS pcn_cancelledby, --123-- empty string - CAST('' AS VARCHAR) AS pcn_registered_keeper_address, --124-- empty string - CAST('' AS VARCHAR) AS pcn_current_ticket_address, --125-- empty string - CAST(NULL AS VARCHAR) AS pcn_corresp_dispute_flag, --126-- null string - CAST(NULL AS VARCHAR) AS pcn_keyworker_corresp_dispute_flag, --127-- null string - CAST('' AS VARCHAR) AS pcn_fin_year_flag, --128-- empty string - CAST('' AS VARCHAR) AS pcn_fin_year, --129-- empty string - CAST('' AS VARCHAR) AS pcn_ticket_ref, --130-- empty string - CAST(NULL AS VARCHAR) AS pcn_nto_printed, --131-- null string - CAST(NULL AS VARCHAR) AS pcn_appeal_accepted, --132-- null string - CAST(NULL AS VARCHAR) AS pcn_arrived_in_pound, --133-- null string - CAST(NULL AS VARCHAR) AS pcn_cancellation_reversed, --134-- null string - CAST(NULL AS VARCHAR) AS pcn_cc_printed, --135-- null string - CAST(NULL AS VARCHAR) AS pcn_drr, --136-- null string - CAST(NULL AS VARCHAR) AS pcn_en_printed, --137-- null string - CAST(NULL AS VARCHAR) AS pcn_hold_released, --138-- null string - CAST(NULL AS VARCHAR) AS pcn_dvla_response, --139-- null string - CAST(NULL AS VARCHAR) AS pcn_dvla_request, --140-- null string - CAST(NULL AS VARCHAR) AS pcn_full_rate_uplift, --141-- null string - CAST(NULL AS VARCHAR) AS pcn_hold_until, --142-- null string - CAST(NULL AS VARCHAR) AS pcn_lifted_at, --143-- null string - CAST(NULL AS VARCHAR) AS pcn_lifted_by, --144-- null string - CAST(NULL AS VARCHAR) AS pcn_loaded, --145-- null string - CAST(NULL AS VARCHAR) AS pcn_nor_sent, --146-- null string - CAST(NULL AS VARCHAR) AS pcn_notice_held, --147-- null string - CAST(NULL AS VARCHAR) AS pcn_ofr_printed, --148-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_printed, --149-- null string - CAST(NULL AS VARCHAR) AS pcn_reissue_nto_requested, --150-- null string - CAST(NULL AS VARCHAR) AS pcn_reissue_pcn, --151-- null string - CAST(NULL AS VARCHAR) AS pcn_set_back_to_pre_cc_stage, --152-- null string - CAST(NULL AS VARCHAR) AS pcn_vehicle_released_for_auction, --153-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_issued, --154-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_redistributed, --155-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_request_granted, --156-- null string - CAST(NULL AS VARCHAR) AS pcn_ad_hoc_vq4_request, --157-- null string - CAST(NULL AS VARCHAR) AS pcn_paper_vq5_received, --158-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_buslane, --159-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_pre_debt, --160-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_collection, --161-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_drr, --162-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_cc, --163-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_nto, --164-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_print, --165-- null string - CAST(NULL AS VARCHAR) AS pcn_warning_notice_extracted_for_print, --166-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_ofr, --167-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_warrant_request, --168-- null string - CAST(NULL AS VARCHAR) AS pcn_pre_debt_new_debtor_details, --169-- null string - CAST(NULL AS VARCHAR) AS pcn_importdattime, --170-- null string - CAST(NULL AS VARCHAR) AS pcn_importdatetime, --171-- null string - CAST('' AS VARCHAR) AS pcn_import_year, --172-- empty string - CAST('' AS VARCHAR) AS pcn_import_month, --173-- empty string - CAST('' AS VARCHAR) AS pcn_import_day, --174-- empty string - CAST('' AS VARCHAR) AS pcn_import_date, --175-- empty string - - /*Teams data*/ - t.t_start_date, - t.t_end_date, - t.t_team, - t.t_team_name, - t.t_role, - t.t_forename, - t.t_surname, - t.t_full_name, - t.t_qa_doc_created_by, - t.t_qa_doc_full_name, - t.t_post_title, - t.t_notes, - t.t_import_date, - - /* Partition columns moved to the end to keep schema alligned */ - d.import_year, - d.import_month, - d.import_day, - CAST(d.import_date AS VARCHAR) AS import_date - - FROM downtime d - LEFT JOIN team t - ON UPPER(t.t_full_name) = UPPER(d.Response_written_by) --downtime officer full name -) -SELECT * FROM liberator_with_team -UNION -SELECT * FROM google_form ---order by downtime_timestamp desc -; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime_gds.py b/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime_gds.py deleted file mode 100644 index 086bfa2e3..000000000 --- a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_downtime_gds.py +++ /dev/null @@ -1,1172 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_correspondence_performance_records_with_pcn_downtime_gds" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ --->> "dataplatform-prod-liberator-refined-zone"."parking_correspondence_performance_records_with_pcn_downtime_gds" ---< "parking-raw-zone"."parking_correspondence_performance_teams" (data from google spreadsheet) ---< "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" ---<< "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ---< "parking-raw-zone"."parking_officer_downtime" (data from google form) -/* -Correspondence Performance records last 13 months with PCN FOI records -16/06/2022 - Created -21/04/2023 - added teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing -02/10/2023 - modified to union data into one output for Downtime data gathered from Google form https://forms.gle/bB53jAayiZ2Ykwjk6 -08/11/2023 - modified to add monthly officer totals from original gds job -22/10/2024 - translated into PrestoSQL to move away from Glue -05/11/2024 + Refactored SQL to separate the inward column conversions from onward business logic using CTE subqueries and allow improved fault handling. - + Downtime's Google Sheets alternate date formats properly handled -28/11/2024 - updated downtime records for t_team details to pull from parking_correspondence_performance_teams instead on leaving blank -12/12/2024 + Integrated the previous downtime update into the refactored version - + Commented out alternative last 13 month filter that caused unexpectedly higher row count -05/03/2025 - Replaced conditional '' empty string outputs with NULL defaults to be consistent with previous Glue/SparkSQL implementations. -05/03/2025 - Removed redundant left joins from subquery -14/04/2025 - Strictly applied column formatting consistent with previous Glue outputs. Fixed faulty arithmetic translation of Glue/SparkSQL to AthenaSQL. -16/04/2025 - Workaround orchestration issue caused by latest "pcnfoidetails_pcn_foi_full" not yet ready -28/04/2055 - Checked over logic of conditionally-shown columns -*/ -/*Teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing*/ -WITH -responses AS ( - SELECT - /* officer_total_rep_dates*/ - l.Response_written_by AS tot_rep_Response_written_by, - SUBSTR(l.response_generated_at, 1, 10) AS tot_rep_response_date, - SUBSTR(l.response_generated_at, 1, 7) || '-01' AS tot_rep_monthYear_response, - -- Empty "response_generated_at" outputs '-01' which would get counted! - -- Makes no difference: SUBSTR(CAST(liberator_pcn_ic.response_generated_at AS VARCHAR(10)), 1, 7) || '-01' AS tot_rep_monthYear_response, - l.Response_written_by || SUBSTR(l.response_generated_at, 1, 10) AS tot_rep_unique_link, - - /* total_response_month*/ - l.Response_written_by AS mth_tot_rep_Response_written_by, - SUBSTR(l.response_generated_at, 1, 10) AS mth_tot_rep_response_date, - SUBSTR(l.response_generated_at, 1, 7) || '-01' AS mth_tot_rep_monthYear_response, - l.Response_written_by || SUBSTR(l.response_generated_at, 1, 7) || '-01' AS mth_tot_rep_unique_link - - -- Q. How do empty "response_generated_at" rows effect subsequent business logic? - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" l - -- The unreferenced p. caused SQL linter error ST11 - -- Then discoverd the row count was not expanded by the left join to p because p.pcn is unique within import_date -/* -- Therefore removed this redundant left join... - LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p - ON l.ticketserialnumber = p.pcn - AND l.import_date = p.import_date -*/ - WHERE l.import_Date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" g - ) - AND l.response_generated_at <> '' -- Q. Should I remove empty "response_generated_at" to prevent side-effects? - AND LENGTH(l.ticketserialnumber) = 10 - AND CAST(SUBSTR(l.date_received, 1, 10) AS DATE) > CURRENT_DATE - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - DATE_DIFF( - 'month', - TRY(CAST(SUBSTR(l.date_received, 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ) <= 13 -*/ -), - -officer_total_rep_dates AS ( - SELECT - tot_rep_Response_written_by, - tot_rep_response_date, - tot_rep_monthYear_response, - tot_rep_unique_link, - - COUNT(DISTINCT tot_rep_response_date) AS tot_rep_total_dates, - -- COUNT(DISTINCT SUBSTR(liberator_pcn_ic.response_generated_at, 1, 10)) AS tot_rep_total_dates, - COUNT(*) AS tot_rep_records - FROM - responses - GROUP BY - tot_rep_Response_written_by, - tot_rep_response_date, - tot_rep_monthYear_response, - tot_rep_unique_link -), - -total_response_month AS ( - SELECT - mth_tot_rep_Response_written_by, - mth_tot_rep_monthYear_response, - mth_tot_rep_unique_link, - - COUNT(DISTINCT mth_tot_rep_response_date) AS mth_tot_rep_total_dates, - -- COUNT(DISTINCT SUBSTR(liberator_pcn_ic.response_generated_at, 1, 10)) AS mth_tot_rep_total_dates, - COUNT(*) AS mth_tot_rep_records - FROM - responses - GROUP BY - mth_tot_rep_Response_written_by, - mth_tot_rep_monthYear_response, - mth_tot_rep_unique_link -), - -/*Teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing*/ -team AS ( - SELECT DISTINCT - start_date AS t_start_date, - end_date AS t_end_date, - team AS t_team, - team_name AS t_team_name, - ROLE AS t_role, - forename AS t_forename, - surname AS t_surname, - full_name AS t_full_name, - qa_doc_created_by AS t_qa_doc_created_by, - qa_doc_full_name AS t_qa_doc_full_name, - post_title AS t_post_title, - notes AS t_notes, - import_date AS t_import_date --* - FROM "parking-raw-zone"."parking_correspondence_performance_teams" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" g - ) -), - -liberator_pcn_icdate_received_last13months AS ( - SELECT - *, - CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS current_utc_timestamp, - TRY(CAST(date_received AS TIMESTAMP)) AS date_received_timestamp, -- pre-filtered by WHERE clause - - IF(whenassigned <> '', -- contains probable valid timestamp - TRY(CAST(whenassigned AS TIMESTAMP)), - CAST(NULL AS TIMESTAMP) -- edge case - ) AS whenassigned_timestamp, - - IF(Response_generated_at <> '', -- contains probable valid timestamp - TRY(CAST(Response_generated_at AS TIMESTAMP)), - CAST(NULL AS TIMESTAMP) -- edge case - ) AS Response_generated_at_timestamp, - - CAST(DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - CAST(CURRENT_TIMESTAMP AS DATE) - ) AS BIGINT) AS days_since_date_received, - - CAST(IF( - whenassigned <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_date_recieved_whenassigned, - - CAST(IF( - whenassigned <> '' AND response_generated_at = '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_whenassigned, - - CAST(IF( - whenassigned <> '' AND response_generated_at <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(whenassigned, 1, 10) AS DATE)), - TRY(CAST(SUBSTR(response_generated_at, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_whenassigned_response_generated_at, - - CAST(IF( - response_generated_at <> '', - DATE_DIFF( - 'day', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), -- under condition of date_received <>'' - TRY(CAST(SUBSTR(response_generated_at, 1, 10) AS DATE)) - ), - NULL -- edge case - ) AS BIGINT) AS days_since_date_received_response_generated_at, - - /*links to officer monthly totals*/ - CAST(Response_written_by || SUBSTR(response_generated_at, 1, 10) AS VARCHAR) AS link_officer_total_rep_dates, - CAST(Response_written_by || IF(response_generated_at <> '', SUBSTR(response_generated_at, 1, 7) || '-01', '') AS VARCHAR) AS link_month_total--, - - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - WHERE import_Date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" g - ) - AND LENGTH(ticketserialnumber) = 10 -- ticket filter - AND date_received <>'' -- is the overriding condition for "13 months from todays date"! - AND CAST(SUBSTR(date_received, 1, 10) AS DATE) > CAST(CURRENT_TIMESTAMP AS DATE) - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - DATE_DIFF( - 'month', - TRY(CAST(SUBSTR(date_received, 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ) <= 13 -*/ -), - -liberator_with_team_gds AS ( - SELECT - /*Fields for union downtime data*/ - CAST('' AS VARCHAR) AS downtime_timestamp, - CAST('' AS VARCHAR) AS downtime_email_address, - CAST('' AS VARCHAR) AS officer_s_first_name, - CAST('' AS VARCHAR) AS officer_s_last_name, - CAST('' AS VARCHAR) AS liberator_system_username, - CAST('' AS VARCHAR) AS liberator_system_id, - CAST('' AS VARCHAR) AS import_datetime, - CAST('' AS VARCHAR) AS multiple_downtime_days_flag, - CAST('' AS VARCHAR) AS response_secs, - CAST('' AS VARCHAR) AS response_mins, - CAST('' AS VARCHAR) AS response_hours, - CAST('' AS VARCHAR) AS response_days_plus_one, - CAST('' AS VARCHAR) AS downtime_total_non_working_mins_with_lunch, - CAST('' AS VARCHAR) AS downtime_total_non_working_mins, - CAST('' AS VARCHAR) AS downtime_total_working_mins_with_lunch, - CAST('' AS VARCHAR) AS downtime_total_working_mins, - CAST('' AS VARCHAR) AS downtime_total_working_mins_with_lunch_net, - CAST('' AS VARCHAR) AS downtime_total_working_mins_net, - CAST('' AS VARCHAR) AS start_date_time, - CAST('' AS VARCHAR) AS startdate, - CAST('' AS VARCHAR) AS start_date_datetime, - CAST('' AS VARCHAR) AS end_date_time, - CAST('' AS VARCHAR) AS enddate, - CAST('' AS VARCHAR) AS end_date_datetime, - - /*Liberator Incoming Correspondence Data*/ - CAST(CASE -- having already asserted date_received <>'' - WHEN l.whenassigned = '' THEN 'Unassigned' - WHEN l.whenassigned <> '' AND l.response_generated_at = '' THEN 'Assigned' - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' THEN 'Responded' - --ELSE NULL - END AS VARCHAR) AS response_status, - - CAST(CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS VARCHAR) AS Current_time_stamp, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS unassigned_time, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.whenassigned_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS to_assigned_time, - - CAST(CASE - WHEN l.whenassigned <> '' - AND l.response_generated_at = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_time, - - CAST(CASE - WHEN l.whenassigned <> '' - AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_response_time, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS response_time, - - /*unassigned days*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN CAST(days_since_date_received AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS unassigned_days, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - THEN CASE - WHEN l.days_since_date_received <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS unassigned_days_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - AND l.days_since_date_received <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS unassigned_days_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned = '' - AND l.days_since_date_received <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS unassigned_days_kpiTotFourteenLess, - - /*Days to assign*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN CAST(l.days_since_date_recieved_whenassigned AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS Days_to_assign, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - THEN CASE - WHEN l.days_since_date_recieved_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_recieved_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_recieved_whenassigned > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS Days_to_assign_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - AND l.days_since_date_recieved_whenassigned <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS Days_to_assign_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.whenassigned <> '' - AND l.days_since_date_recieved_whenassigned <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS Days_to_assign_kpiTotFourteenLess, - - /*assigned in progress days*/ - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN CAST(l.days_since_whenassigned AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN CASE - WHEN l.days_since_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days_group, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assigned_in_progress_days_kpiTotFiftySixLess, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assigned_in_progress_days_kpiTotFourteenLess, - - /*assigned response days*/ - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN CAST(l.days_since_whenassigned_response_generated_at AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN CASE - WHEN l.days_since_whenassigned_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned_response_generated_at > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays_group, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assignedResponseDays_kpiTotFiftySixLess, - - CAST(CASE - WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS assignedResponseDays_kpiTotFourteenLess, - - /*Response days*/ - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN CAST(l.days_since_date_received_response_generated_at AS VARCHAR) - --ELSE NULL - END AS VARCHAR) AS ResponseDays, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - THEN CASE - WHEN l.days_since_date_received_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_response_generated_at > 56 THEN '56 plus days' - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS ResponseDays_group, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 56 - THEN '1' - ELSE '0' - END AS VARCHAR) AS ResponseDays_kpiTotFiftySixLess, - - CAST(CASE -- asserted date_received <>'' - WHEN l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 14 - THEN '1' - ELSE '0' - END AS VARCHAR) AS ResponseDays_kpiTotFourteenLess, - - Response_generated_at, - Date_Received, - - SUBSTR(CAST(l.date_received AS VARCHAR(10)), 1, 7) || '-01' AS MonthYear, -- asserted date_received <>'' - - l.Type AS "Type", - l.Serviceable, - l.Service_category, - l.Response_written_by, - l.Letter_template, - l.Action_taken, - l.Related_to_PCN, - l.Cancellation_group, - l.Cancellation_reason, - l.whenassigned, - l.ticketserialnumber, - l.noderef, - CAST(l.record_created AS VARCHAR) AS record_created, - - l.import_timestamp, - - /*pcn data*/ - /*** pcn columns taken from "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ***/ - -- Data sourced from the refined zone needs to be handled carefully... - -- Do not assume the following output column datatypes will be the same as "pcnfoidetails_pcn_foi_full". - -- Being left-joined to "pcnfoidetails_pcn_foi_full" will cause NULLs to appear in these columns. - -- And similar transforms elsewhere require different translations to the ones below... - /* These are the current automated translations developed for this transform:- - TRY_CAST(p.[source_column] AS DATE) AS [target_column], --[position]-- try date to date - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try int to string - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS DATE), '%Y-%m-%d'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try date to string - TRY_CAST(p.[source_column] AS INTEGER) AS [target_column], --[position]-- try int to int - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try timestamp to string - TRY_CAST(p.[source_column] AS TIMESTAMP) AS [target_column], --[position]-- try timestamp to timestamp - */ - -- These translations try to deal with problems without resorting to throwing errors, tolerating a reasonable degree of schema evolution in the "pcnfoidetails_pcn_foi_full" source. - -- For example, date and timestamp string formatting works with either string, date and timestamp source columns and works like this... - /* If source can be cast to a date then it can be correctly date-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS DATE), '%Y-%m-%d'), - CAST(p.xxxx AS VARCHAR) --...and can be extended to handle other date formats. - ) AS pcn_xxxx, -- from DATE - */ - /* If source can be cast to a timestamp then it can be correctly timestamp-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), - CAST(p.xxxx AS VARCHAR) -- Alternative for transforming a time-zoned timestamp... - --...COALESCE(DATE_FORMAT(TRY_CAST(TRY(From_iso8601_timestamp(p.xxxx)) AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.xxxx AS VARCHAR)) - --...and can be extended to handle other timestamp formats. - ) AS pcn_xxxx, -- from TIMESTAMP - */ - TRY_CAST(p.pcn AS VARCHAR) AS pcn_pcn, --69-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedate AS DATE), '%Y-%m-%d'), CAST(p.pcnissuedate AS VARCHAR)) AS pcn_pcnissuedate, --70-- try date to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcnissuedatetime AS VARCHAR)) AS pcn_pcnissuedatetime, --71-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_canx_date AS DATE), '%Y-%m-%d'), CAST(p.pcn_canx_date AS VARCHAR)) AS pcn_pcn_canx_date, --72-- try date to string - TRY_CAST(p.cancellationgroup AS VARCHAR) AS pcn_cancellationgroup, --73-- try string to string (default) - TRY_CAST(p.cancellationreason AS VARCHAR) AS pcn_cancellationreason, --74-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_casecloseddate AS DATE), '%Y-%m-%d'), CAST(p.pcn_casecloseddate AS VARCHAR)) AS pcn_pcn_casecloseddate, --75-- try date to string - TRY_CAST(p.street_location AS VARCHAR) AS pcn_street_location, --76-- try string to string (default) - TRY_CAST(p.whereonlocation AS VARCHAR) AS pcn_whereonlocation, --77-- try string to string (default) - TRY_CAST(p.zone AS VARCHAR) AS pcn_zone, --78-- try string to string (default) - TRY_CAST(p.usrn AS VARCHAR) AS pcn_usrn, --79-- try string to string (default) - TRY_CAST(p.contraventioncode AS VARCHAR) AS pcn_contraventioncode, --80-- try string to string (default) - TRY_CAST(p.contraventionsuffix AS VARCHAR) AS pcn_contraventionsuffix, --81-- try string to string (default) - TRY_CAST(p.debttype AS VARCHAR) AS pcn_debttype, --82-- try string to string (default) - TRY_CAST(p.vrm AS VARCHAR) AS pcn_vrm, --83-- try string to string (default) - TRY_CAST(p.vehiclemake AS VARCHAR) AS pcn_vehiclemake, --84-- try string to string (default) - TRY_CAST(p.vehiclemodel AS VARCHAR) AS pcn_vehiclemodel, --85-- try string to string (default) - TRY_CAST(p.vehiclecolour AS VARCHAR) AS pcn_vehiclecolour, --86-- try string to string (default) - TRY_CAST(p.ceo AS VARCHAR) AS pcn_ceo, --87-- try string to string (default) - TRY_CAST(p.ceodevice AS VARCHAR) AS pcn_ceodevice, --88-- try string to string (default) - TRY_CAST(p.current_30_day_flag AS VARCHAR) AS pcn_current_30_day_flag, --89-- try int to string - TRY_CAST(p.isvda AS VARCHAR) AS pcn_isvda, --90-- try int to string - TRY_CAST(p.isvoid AS VARCHAR) AS pcn_isvoid, --91-- try int to string - TRY_CAST(p.isremoval AS VARCHAR) AS pcn_isremoval, --92-- try string to string (default) - TRY_CAST(p.driverseen AS VARCHAR) AS pcn_driverseen, --93-- try string to string (default) - TRY_CAST(p.allwindows AS VARCHAR) AS pcn_allwindows, --94-- try string to string (default) - TRY_CAST(p.parkedonfootway AS VARCHAR) AS pcn_parkedonfootway, --95-- try string to string (default) - TRY_CAST(p.doctor AS VARCHAR) AS pcn_doctor, --96-- try string to string (default) - TRY_CAST(p.warningflag AS VARCHAR) AS pcn_warningflag, --97-- try int to string - TRY_CAST(p.progressionstage AS VARCHAR) AS pcn_progressionstage, --98-- try string to string (default) - TRY_CAST(p.nextprogressionstage AS VARCHAR) AS pcn_nextprogressionstage, --99-- try string to string (default) - TRY_CAST(p.nextprogressionstagestarts AS VARCHAR) AS pcn_nextprogressionstagestarts, --100-- try string to string (default) - TRY_CAST(p.holdreason AS VARCHAR) AS pcn_holdreason, --101-- try string to string (default) - TRY_CAST(p.lib_initial_debt_amount AS VARCHAR) AS pcn_lib_initial_debt_amount, --102-- try string to string (default) - TRY_CAST(p.lib_payment_received AS VARCHAR) AS pcn_lib_payment_received, --103-- try string to string (default) - TRY_CAST(p.lib_write_off_amount AS VARCHAR) AS pcn_lib_write_off_amount, --104-- try string to string (default) - TRY_CAST(p.lib_payment_void AS VARCHAR) AS pcn_lib_payment_void, --105-- try string to string (default) - TRY_CAST(p.lib_payment_method AS VARCHAR) AS pcn_lib_payment_method, --106-- try string to string (default) - TRY_CAST(p.lib_payment_ref AS VARCHAR) AS pcn_lib_payment_ref, --107-- try string to string (default) - TRY_CAST(p.baliff_from AS VARCHAR) AS pcn_baliff_from, --108-- try string to string (default) - TRY_CAST(p.bailiff_to AS VARCHAR) AS pcn_bailiff_to, --109-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.bailiff_processedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.bailiff_processedon AS VARCHAR)) AS pcn_bailiff_processedon, --110-- try timestamp to string - TRY_CAST(p.bailiff_redistributionreason AS VARCHAR) AS pcn_bailiff_redistributionreason, --111-- try string to string (default) - TRY_CAST(p.bailiff AS VARCHAR) AS pcn_bailiff, --112-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.warrantissuedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrantissuedate AS VARCHAR)) AS pcn_warrantissuedate, --113-- try timestamp to string - TRY_CAST(p.allocation AS VARCHAR) AS pcn_allocation, --114-- try int to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_datenotified AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_datenotified AS VARCHAR)) AS pcn_eta_datenotified, --115-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_packsubmittedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_packsubmittedon AS VARCHAR)) AS pcn_eta_packsubmittedon, --116-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_evidencedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_evidencedate AS VARCHAR)) AS pcn_eta_evidencedate, --117-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_adjudicationdate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_adjudicationdate AS VARCHAR)) AS pcn_eta_adjudicationdate, --118-- try timestamp to string - TRY_CAST(p.eta_appealgrounds AS VARCHAR) AS pcn_eta_appealgrounds, --119-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_decisionreceived AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_decisionreceived AS VARCHAR)) AS pcn_eta_decisionreceived, --120-- try timestamp to string - TRY_CAST(p.eta_outcome AS VARCHAR) AS pcn_eta_outcome, --121-- try string to string (default) - TRY_CAST(p.eta_packsubmittedby AS VARCHAR) AS pcn_eta_packsubmittedby, --122-- try string to string (default) - TRY_CAST(p.cancelledby AS VARCHAR) AS pcn_cancelledby, --123-- try string to string (default) - TRY_CAST(p.registered_keeper_address AS VARCHAR) AS pcn_registered_keeper_address, --124-- try string to string (default) - TRY_CAST(p.current_ticket_address AS VARCHAR) AS pcn_current_ticket_address, --125-- try string to string (default) - TRY_CAST(p.corresp_dispute_flag AS VARCHAR) AS pcn_corresp_dispute_flag, --126-- try int to string - TRY_CAST(p.keyworker_corresp_dispute_flag AS VARCHAR) AS pcn_keyworker_corresp_dispute_flag, --127-- try int to string - TRY_CAST(p.fin_year_flag AS VARCHAR) AS pcn_fin_year_flag, --128-- try string to string (default) - TRY_CAST(p.fin_year AS VARCHAR) AS pcn_fin_year, --129-- try string to string (default) - TRY_CAST(p.ticket_ref AS VARCHAR) AS pcn_ticket_ref, --130-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.nto_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nto_printed AS VARCHAR)) AS pcn_nto_printed, --131-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.appeal_accepted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.appeal_accepted AS VARCHAR)) AS pcn_appeal_accepted, --132-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.arrived_in_pound AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.arrived_in_pound AS VARCHAR)) AS pcn_arrived_in_pound, --133-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cancellation_reversed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cancellation_reversed AS VARCHAR)) AS pcn_cancellation_reversed, --134-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cc_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cc_printed AS VARCHAR)) AS pcn_cc_printed, --135-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.drr AS VARCHAR)) AS pcn_drr, --136-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.en_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.en_printed AS VARCHAR)) AS pcn_en_printed, --137-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_released AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_released AS VARCHAR)) AS pcn_hold_released, --138-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_response AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_response AS VARCHAR)) AS pcn_dvla_response, --139-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_request AS VARCHAR)) AS pcn_dvla_request, --140-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.full_rate_uplift AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.full_rate_uplift AS VARCHAR)) AS pcn_full_rate_uplift, --141-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_until AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_until AS VARCHAR)) AS pcn_hold_until, --142-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_at AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_at AS VARCHAR)) AS pcn_lifted_at, --143-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_by AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_by AS VARCHAR)) AS pcn_lifted_by, --144-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.loaded AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.loaded AS VARCHAR)) AS pcn_loaded, --145-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.nor_sent AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nor_sent AS VARCHAR)) AS pcn_nor_sent, --146-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.notice_held AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.notice_held AS VARCHAR)) AS pcn_notice_held, --147-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ofr_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ofr_printed AS VARCHAR)) AS pcn_ofr_printed, --148-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_printed AS VARCHAR)) AS pcn_pcn_printed, --149-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_nto_requested AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_nto_requested AS VARCHAR)) AS pcn_reissue_nto_requested, --150-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_pcn AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_pcn AS VARCHAR)) AS pcn_reissue_pcn, --151-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.set_back_to_pre_cc_stage AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.set_back_to_pre_cc_stage AS VARCHAR)) AS pcn_set_back_to_pre_cc_stage, --152-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.vehicle_released_for_auction AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.vehicle_released_for_auction AS VARCHAR)) AS pcn_vehicle_released_for_auction, --153-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_issued AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_issued AS VARCHAR)) AS pcn_warrant_issued, --154-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_redistributed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_redistributed AS VARCHAR)) AS pcn_warrant_redistributed, --155-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_request_granted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_request_granted AS VARCHAR)) AS pcn_warrant_request_granted, --156-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ad_hoc_vq4_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ad_hoc_vq4_request AS VARCHAR)) AS pcn_ad_hoc_vq4_request, --157-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.paper_vq5_received AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.paper_vq5_received AS VARCHAR)) AS pcn_paper_vq5_received, --158-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_buslane AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_buslane AS VARCHAR)) AS pcn_pcn_extracted_for_buslane, --159-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_pre_debt AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_pre_debt AS VARCHAR)) AS pcn_pcn_extracted_for_pre_debt, --160-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_collection AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_collection AS VARCHAR)) AS pcn_pcn_extracted_for_collection, --161-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_drr AS VARCHAR)) AS pcn_pcn_extracted_for_drr, --162-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_cc AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_cc AS VARCHAR)) AS pcn_pcn_extracted_for_cc, --163-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_nto AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_nto AS VARCHAR)) AS pcn_pcn_extracted_for_nto, --164-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_print AS VARCHAR)) AS pcn_pcn_extracted_for_print, --165-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warning_notice_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warning_notice_extracted_for_print AS VARCHAR)) AS pcn_warning_notice_extracted_for_print, --166-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_ofr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_ofr AS VARCHAR)) AS pcn_pcn_extracted_for_ofr, --167-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_warrant_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_warrant_request AS VARCHAR)) AS pcn_pcn_extracted_for_warrant_request, --168-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pre_debt_new_debtor_details AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pre_debt_new_debtor_details AS VARCHAR)) AS pcn_pre_debt_new_debtor_details, --169-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdattime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdattime AS VARCHAR)) AS pcn_importdattime, --170-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdatetime AS VARCHAR)) AS pcn_importdatetime, --171-- try timestamp to string - TRY_CAST(p.import_year AS VARCHAR) AS pcn_import_year, --172-- try string to string (default) - TRY_CAST(p.import_month AS VARCHAR) AS pcn_import_month, --173-- try string to string (default) - TRY_CAST(p.import_day AS VARCHAR) AS pcn_import_day, --174-- try string to string (default) - TRY_CAST(p.import_date AS VARCHAR) AS pcn_import_date, --175-- try string to string (default) - - /*links to officer monthly totals*/ - l.link_officer_total_rep_dates, - l.link_month_total, - - /* officer monthly totals*/ - CAST(m.mth_tot_rep_monthYear_response AS VARCHAR) AS mth_tot_rep_monthYear_response, - CAST(m.mth_tot_rep_total_dates AS VARCHAR) AS mth_tot_rep_total_dates, - CAST(m.mth_tot_rep_records AS VARCHAR) AS mth_tot_rep_records, - CAST(o.tot_rep_response_date AS VARCHAR) AS tot_rep_response_date, - CAST(o.tot_rep_total_dates AS VARCHAR) AS tot_rep_total_date, - CAST(o.tot_rep_records AS VARCHAR) AS tot_rep_records, - - /* teams */ - t.t_start_date, - t.t_end_date, - t.t_team, - t.t_team_name, - t.t_role, - t.t_forename, - t.t_surname, - t.t_full_name, - t.t_qa_doc_created_by, - t.t_qa_doc_full_name, - t.t_post_title, - t.t_notes, - t.t_import_date, - - /* Partition columns moved to the end to keep schema alligned */ - l.import_year, - l.import_month, - l.import_day, - l.import_date - - FROM liberator_pcn_icdate_received_last13months l - - LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p - /* - ON p.import_date = l.import_date -- joined on import partition - Fails when "pcnfoidetails_pcn_foi_full" hasn't produced data for l.import_date yet... - ...resulting in empty pcn_ columns in the output. - Airflow's orchestration should avoid this after PCNFOIDetails_PCN_FOI_FULL.sql has been migrated... - ...but for now we'll just use the following simple workaround... - */ - ON p.import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" g - ) - AND l.ticketserialnumber = p.pcn -- ticketserialnumber is valid - - LEFT JOIN team t - ON UPPER(t.t_full_name) = UPPER(l.Response_written_by) - - LEFT JOIN officer_total_rep_dates o - ON l.link_officer_total_rep_dates = o.tot_rep_unique_link - - LEFT JOIN total_response_month m - ON l.link_month_total = m.mth_tot_rep_unique_link -), - -downtime_start_date_end_date_last13months AS ( - SELECT *, - - IF(start_date LIKE '%/%/%', -- contains possible valid timestamp - TRY(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')), --> TIMESTAMP with time zone - IF(start_date LIKE '%-%-%', -- contains other possible valid timestamp - TRY(PARSE_DATETIME(start_date, 'yyyy-MM-dd HH:mm:ss')), --> TIMESTAMP with time zone - CAST(NULL AS TIMESTAMP) -- edge case - ) - ) AS start_timestampZ, - - IF(end_date LIKE '%/%/%', -- contains possible valid timestamp - TRY(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')), --> TIMESTAMP with time zone - IF(end_date LIKE '%-%-%', -- contains other possible valid timestamp - TRY(PARSE_DATETIME(end_date, 'yyyy-MM-dd HH:mm:ss')), --> TIMESTAMP with time zone - CAST(NULL AS TIMESTAMP) -- edge case - ) - ) AS end_timestampZ--, - - FROM "parking-raw-zone"."parking_officer_downtime" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_officer_downtime" g - ) - AND "timestamp" NOT LIKE '' - AND "timestamp" NOT LIKE '#REF!' - AND CAST(SUBSTR(CAST("timestamp" as VARCHAR(30)), 1, 10) AS DATE) > CAST(CURRENT_TIMESTAMP AS DATE) - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - DATE_DIFF( - 'month', - TRY(CAST(SUBSTR("timestamp", 1, 10) AS DATE)), - CAST(CURRENT_TIMESTAMP AS DATE) - ) <= 13 -*/ -), - -downtime AS ( - SELECT *, - /* - TO_UNIXTIME(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')) AS start_end_unixtime_seconds, - TO_UNIXTIME(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')) AS end_unixtime_seconds, - - TO_UNIXTIME(PARSE_DATETIME(end_date, 'dd/MM/yyyy HH:mm:ss')) - - TO_UNIXTIME(PARSE_DATETIME(start_date, 'dd/MM/yyyy HH:mm:ss')) AS downtime_secs, - - -- downtime_secs / 60 AS downtime_mins - -- downtime_secs / 3600 AS downtime_hours - */ - - DATE_DIFF( - 'second', - start_timestampZ, - end_timestampZ - ) AS downtime_secs, - - DATE_DIFF( - 'minute', - start_timestampZ, - end_timestampZ - ) AS downtime_mins, - - DATE_DIFF( - 'hour', - start_timestampZ, - end_timestampZ - ) AS downtime_hours, - - DATE_DIFF( - 'day', - start_timestampZ, - end_timestampZ - ) AS downtime_days, - - TRIM(officer_s_first_name) || ' ' || TRIM(officer_s_last_name) AS Response_written_by --downtime officer full name - - FROM downtime_start_date_end_date_last13months -), - -google_form AS ( - --Downtime data gathered from Google form https://forms.gle/bB53jAayiZ2Ykwjk6 - SELECT - CAST(d."timestamp" AS VARCHAR) AS downtime_timestamp, - d.email_address AS downtime_email_address, - TRIM(d.officer_s_first_name) AS officer_s_first_name, - TRIM(d.officer_s_last_name) AS officer_s_last_name, - d.liberator_system_username, - d.liberator_system_id, - CAST(d.import_datetime AS VARCHAR) AS import_datetime, - - /* Throughout asserts... - "timestamp" NOT LIKE '' - AND "timestamp" NOT LIKE '#REF!' - */ - CAST(CASE - WHEN d.downtime_mins > 1440 /* downtime mins (response_mins) */ - THEN '1' - ELSE '0' - END AS VARCHAR) AS multiple_downtime_days_flag, - - -- Because the Glue job's response_secs string output was cast from a bigint returned from unix_timestamp... - -- ...there was no need to cast it to a DOUBLE here... - CAST(d.downtime_secs AS VARCHAR) AS response_secs, - -- ...unlike the products below where SparkSQL and AthenaSQL differed in their arithmetic outcomes... - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input value was cast beforehand to DOUBLE. - CAST(CAST(d.downtime_secs AS DOUBLE)/60 AS VARCHAR) AS REJECTED_response_mins, - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(d.downtime_mins AS DOUBLE) AS VARCHAR) AS response_mins, - /* But might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(d.downtime_mins AS VARCHAR) AS ALTERNATIVE_response_mins, - */ - - -- We are keeping this version because it produces closest to the original Glue output... - -- Though beware, several decimal places may be produced in the VARCHAR output... - CAST(CAST(d.downtime_secs AS DOUBLE)/3600 AS VARCHAR) AS response_hours, - /* But might the following version be the better solution? - -- This does not produce the fractional mantissa because it is an integer calculation from start to finish... - CAST(d.downtime_hours AS VARCHAR) AS ALTERNATIVE_response_hours, - */ - - /*Downtime calendar days*/ - -- Warning: A small number outlier cases, producing negative values in the original Glue output are also produced here... - CAST(d.downtime_days + 1 AS VARCHAR) AS response_days_plus_one, -- downtime days plus one calendar day - - /*Downtime non working mins*/ - CAST(CASE - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1 ) * 948 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 948 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_non_working_mins_with_lunch, --if greater than 1440 mins then (days + 1) * 948 " - - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 1008 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 1008 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_non_working_mins, --if greater than 1440 mins then (days + 1) * 1008 " - - /*Downtime working mins*/ - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 492 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_working_mins_with_lunch, -- 492 mins = 8hrs 12 mins working day including lunch" - - CAST(CASE /* downtime mins (response_mins)*/ - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 /*response_days_plus_one*/ - WHEN d.downtime_mins <= 1440 THEN 432 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS downtime_total_working_mins, -- 432 mins = 7hrs 12mins working hours" - - /* Downtime working mins net (less downtime mins)*/ - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input values were cast beforehand to DOUBLE... - CAST(CASE -- downtime mins (response_mins) - WHEN CAST(d.downtime_secs AS DOUBLE)/60 > 1440 THEN (CAST(d.downtime_secs AS DOUBLE)/86400 + 1) * 492 -- response_days_plus_one - WHEN CAST(d.downtime_secs AS DOUBLE)/60 <= 1440 THEN 492 - CAST(d.downtime_secs AS DOUBLE)/60 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS REJECTED_downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 - WHEN d.downtime_mins <= 1440 THEN 492 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS DOUBLE) AS VARCHAR) AS downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - /* So might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 492 - WHEN d.downtime_mins <= 1440 THEN 492 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS ALTERNATIVE_downtime_total_working_mins_with_lunch_net, -- 492 mins = 8hrs 12 mins "working day including lunch less downtime" - */ - - /* This version was rejected because the output differs wildly from the original Glue output... - -- Here all input values were cast beforehand to DOUBLE... - CAST(CASE -- downtime mins (response_mins) - WHEN CAST(d.downtime_secs AS DOUBLE)/60 > 1440 THEN (CAST(d.downtime_secs AS DOUBLE)/86400 + 1) * 432 -- response_days_plus_one - WHEN CAST(d.downtime_secs AS DOUBLE)/60 <= 1440 THEN 432 - CAST(d.downtime_secs AS DOUBLE)/60 - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS REJECTED_downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - */ - -- So we are keeping this version because it produces closest to the original Glue output... - -- Though beware, the final cast to DOUBLE causes a misleading .0 to be formatted in the VARCHAR output... - CAST(CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 -- response_days_plus_one - WHEN d.downtime_mins <= 1440 THEN 432 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS DOUBLE) AS VARCHAR) AS downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - /* So might the following version be the better solution? - -- This does not add the misleading .0 output in the previous version because it is an integer calculation from start to finish... - CAST(CASE -- downtime mins (response_mins) - WHEN d.downtime_mins > 1440 THEN (d.downtime_days + 1) * 432 -- response_days_plus_one - WHEN d.downtime_mins <= 1440 THEN 432 - d.downtime_mins - --ELSE NULL --edge case NULL VARCHAR will be materialized in denormalized target table - END AS VARCHAR) AS ALTERNATIVE_downtime_total_working_mins_net, -- 432 mins = 7hrs 12mins "working hours" - */ - - CAST(SUBSTR(CAST(d.start_date AS VARCHAR), 12, 5) AS VARCHAR) AS start_date_time, - CAST(SUBSTR(CAST(d.start_date AS VARCHAR), 1, 10) AS VARCHAR) AS startdate, - - CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS VARCHAR) AS start_date_datetime, - - CAST(SUBSTR(CAST(d.end_date AS VARCHAR), 12, 5) AS VARCHAR) AS end_date_time, - CAST(SUBSTR(CAST(d.end_date AS VARCHAR), 1, 10) AS VARCHAR) AS enddate, - - CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS VARCHAR) AS end_date_datetime, - - CAST('Downtime' AS VARCHAR) AS response_status, - CAST(CAST(CURRENT_TIMESTAMP AS TIMESTAMP) AS VARCHAR) AS Current_time_stamp, - - /*Liberator Incoming Correspondence Data*/ - CAST('' AS VARCHAR) AS unassigned_time, - CAST('' AS VARCHAR) AS to_assigned_time, - CAST('' AS VARCHAR) AS assigned_in_progress_time, - CAST('' AS VARCHAR) AS assigned_response_time, - - -- Is there a better way to do this? Substring captures only the minutes... - 'INTERVAL ''' - || REGEXP_REPLACE(CAST( - TRY(CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS TIMESTAMP)) - - TRY(CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 16 - ) AS TIMESTAMP)) - AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - AS response_time, --as downtime_duration --line 30 - - CAST('' AS VARCHAR) AS unassigned_days, - CAST('' AS VARCHAR) AS unassigned_days_group, - CAST('' AS VARCHAR) AS unassigned_days_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS unassigned_days_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS Days_to_assign, - CAST('' AS VARCHAR) AS Days_to_assign_group, - CAST('' AS VARCHAR) AS Days_to_assign_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS Days_to_assign_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS assigned_in_progress_days, - CAST('' AS VARCHAR) AS assigned_in_progress_days_group, - CAST('' AS VARCHAR) AS assigned_in_progress_days_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS assigned_in_progress_days_kpiTotFourteenLess, - CAST('' AS VARCHAR) AS assignedResponseDays, - CAST('' AS VARCHAR) AS assignedResponseDays_group, - CAST('' AS VARCHAR) AS assignedResponseDays_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS assignedResponseDays_kpiTotFourteenLess, - - CAST(DATE_DIFF( - 'day', - CAST(SUBSTR( - CAST(d.start_timestampZ AS VARCHAR(30)), - 1, - 10 - ) AS DATE), - CAST(SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 10 - ) AS DATE) - ) AS VARCHAR) AS ResponseDays, --as downtime_duration in days - - CAST('' AS VARCHAR) AS ResponseDays_group, - CAST('' AS VARCHAR) AS ResponseDays_kpiTotFiftySixLess, - CAST('' AS VARCHAR) AS ResponseDays_kpiTotFourteenLess, - - d.end_date AS Response_generated_at, - d.start_date AS Date_Received, - - SUBSTR( - CAST(d.end_timestampZ AS VARCHAR(30)), - 1, - 7 - ) || '-01' AS MonthYear, - - 'Downtime' AS "Type", - - d.downtime AS Serviceable, -- downtime --,'Downtime' as Serviceable -- downtime - - CAST('' AS VARCHAR) AS Service_category, - - d.Response_written_by, --downtime officer full name - - CAST('' AS VARCHAR) AS Letter_template, - CAST('' AS VARCHAR) AS Action_taken, - CAST('' AS VARCHAR) AS Related_to_PCN, - CAST('' AS VARCHAR) AS Cancellation_group, - CAST('' AS VARCHAR) AS Cancellation_reason, - CAST('' AS VARCHAR) AS whenassigned, - CAST('' AS VARCHAR) AS ticketserialnumber, - CAST('' AS VARCHAR) AS noderef, - CAST('' AS VARCHAR) AS record_created, - d.import_timestamp, - - /*pcn data*/ - -- The original Glue SparkSQL had all of these pcn columns output as '' empty strings which either... - -- ...conflicted with the output column datatypes configured by Glue - -- ...and/or conflicted with the column translations done earlier in the SQL script. - /* Instead here, the null or empty translations are deliberately dependent upon the target datatype... - CAST(NULL AS DATE) AS [target_column], --[position]-- null date - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS INTEGER) AS [target_column], --[position]-- null int - CAST('' AS VARCHAR) AS [target_column], --[position]-- empty string - CAST(NULL AS VARCHAR) AS [target_column], --[position]-- null string - CAST(NULL AS TIMESTAMP) AS [target_column], --[position]-- null timestamp - */ - CAST('' AS VARCHAR) AS pcn_pcn, --69-- empty string - CAST(NULL AS VARCHAR) AS pcn_pcnissuedate, --70-- null string - CAST(NULL AS VARCHAR) AS pcn_pcnissuedatetime, --71-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_canx_date, --72-- null string - CAST('' AS VARCHAR) AS pcn_cancellationgroup, --73-- empty string - CAST('' AS VARCHAR) AS pcn_cancellationreason, --74-- empty string - CAST(NULL AS VARCHAR) AS pcn_pcn_casecloseddate, --75-- null string - CAST('' AS VARCHAR) AS pcn_street_location, --76-- empty string - CAST('' AS VARCHAR) AS pcn_whereonlocation, --77-- empty string - CAST('' AS VARCHAR) AS pcn_zone, --78-- empty string - CAST('' AS VARCHAR) AS pcn_usrn, --79-- empty string - CAST('' AS VARCHAR) AS pcn_contraventioncode, --80-- empty string - CAST('' AS VARCHAR) AS pcn_contraventionsuffix, --81-- empty string - CAST('' AS VARCHAR) AS pcn_debttype, --82-- empty string - CAST('' AS VARCHAR) AS pcn_vrm, --83-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclemake, --84-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclemodel, --85-- empty string - CAST('' AS VARCHAR) AS pcn_vehiclecolour, --86-- empty string - CAST('' AS VARCHAR) AS pcn_ceo, --87-- empty string - CAST('' AS VARCHAR) AS pcn_ceodevice, --88-- empty string - CAST(NULL AS VARCHAR) AS pcn_current_30_day_flag, --89-- null string - CAST(NULL AS VARCHAR) AS pcn_isvda, --90-- null string - CAST(NULL AS VARCHAR) AS pcn_isvoid, --91-- null string - CAST('' AS VARCHAR) AS pcn_isremoval, --92-- empty string - CAST('' AS VARCHAR) AS pcn_driverseen, --93-- empty string - CAST('' AS VARCHAR) AS pcn_allwindows, --94-- empty string - CAST('' AS VARCHAR) AS pcn_parkedonfootway, --95-- empty string - CAST('' AS VARCHAR) AS pcn_doctor, --96-- empty string - CAST(NULL AS VARCHAR) AS pcn_warningflag, --97-- null string - CAST('' AS VARCHAR) AS pcn_progressionstage, --98-- empty string - CAST('' AS VARCHAR) AS pcn_nextprogressionstage, --99-- empty string - CAST('' AS VARCHAR) AS pcn_nextprogressionstagestarts, --100-- empty string - CAST('' AS VARCHAR) AS pcn_holdreason, --101-- empty string - CAST('' AS VARCHAR) AS pcn_lib_initial_debt_amount, --102-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_received, --103-- empty string - CAST('' AS VARCHAR) AS pcn_lib_write_off_amount, --104-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_void, --105-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_method, --106-- empty string - CAST('' AS VARCHAR) AS pcn_lib_payment_ref, --107-- empty string - CAST('' AS VARCHAR) AS pcn_baliff_from, --108-- empty string - CAST('' AS VARCHAR) AS pcn_bailiff_to, --109-- empty string - CAST(NULL AS VARCHAR) AS pcn_bailiff_processedon, --110-- null string - CAST('' AS VARCHAR) AS pcn_bailiff_redistributionreason, --111-- empty string - CAST('' AS VARCHAR) AS pcn_bailiff, --112-- empty string - CAST(NULL AS VARCHAR) AS pcn_warrantissuedate, --113-- null string - CAST(NULL AS VARCHAR) AS pcn_allocation, --114-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_datenotified, --115-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_packsubmittedon, --116-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_evidencedate, --117-- null string - CAST(NULL AS VARCHAR) AS pcn_eta_adjudicationdate, --118-- null string - CAST('' AS VARCHAR) AS pcn_eta_appealgrounds, --119-- empty string - CAST(NULL AS VARCHAR) AS pcn_eta_decisionreceived, --120-- null string - CAST('' AS VARCHAR) AS pcn_eta_outcome, --121-- empty string - CAST('' AS VARCHAR) AS pcn_eta_packsubmittedby, --122-- empty string - CAST('' AS VARCHAR) AS pcn_cancelledby, --123-- empty string - CAST('' AS VARCHAR) AS pcn_registered_keeper_address, --124-- empty string - CAST('' AS VARCHAR) AS pcn_current_ticket_address, --125-- empty string - CAST(NULL AS VARCHAR) AS pcn_corresp_dispute_flag, --126-- null string - CAST(NULL AS VARCHAR) AS pcn_keyworker_corresp_dispute_flag, --127-- null string - CAST('' AS VARCHAR) AS pcn_fin_year_flag, --128-- empty string - CAST('' AS VARCHAR) AS pcn_fin_year, --129-- empty string - CAST('' AS VARCHAR) AS pcn_ticket_ref, --130-- empty string - CAST(NULL AS VARCHAR) AS pcn_nto_printed, --131-- null string - CAST(NULL AS VARCHAR) AS pcn_appeal_accepted, --132-- null string - CAST(NULL AS VARCHAR) AS pcn_arrived_in_pound, --133-- null string - CAST(NULL AS VARCHAR) AS pcn_cancellation_reversed, --134-- null string - CAST(NULL AS VARCHAR) AS pcn_cc_printed, --135-- null string - CAST(NULL AS VARCHAR) AS pcn_drr, --136-- null string - CAST(NULL AS VARCHAR) AS pcn_en_printed, --137-- null string - CAST(NULL AS VARCHAR) AS pcn_hold_released, --138-- null string - CAST(NULL AS VARCHAR) AS pcn_dvla_response, --139-- null string - CAST(NULL AS VARCHAR) AS pcn_dvla_request, --140-- null string - CAST(NULL AS VARCHAR) AS pcn_full_rate_uplift, --141-- null string - CAST(NULL AS VARCHAR) AS pcn_hold_until, --142-- null string - CAST(NULL AS VARCHAR) AS pcn_lifted_at, --143-- null string - CAST(NULL AS VARCHAR) AS pcn_lifted_by, --144-- null string - CAST(NULL AS VARCHAR) AS pcn_loaded, --145-- null string - CAST(NULL AS VARCHAR) AS pcn_nor_sent, --146-- null string - CAST(NULL AS VARCHAR) AS pcn_notice_held, --147-- null string - CAST(NULL AS VARCHAR) AS pcn_ofr_printed, --148-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_printed, --149-- null string - CAST(NULL AS VARCHAR) AS pcn_reissue_nto_requested, --150-- null string - CAST(NULL AS VARCHAR) AS pcn_reissue_pcn, --151-- null string - CAST(NULL AS VARCHAR) AS pcn_set_back_to_pre_cc_stage, --152-- null string - CAST(NULL AS VARCHAR) AS pcn_vehicle_released_for_auction, --153-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_issued, --154-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_redistributed, --155-- null string - CAST(NULL AS VARCHAR) AS pcn_warrant_request_granted, --156-- null string - CAST(NULL AS VARCHAR) AS pcn_ad_hoc_vq4_request, --157-- null string - CAST(NULL AS VARCHAR) AS pcn_paper_vq5_received, --158-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_buslane, --159-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_pre_debt, --160-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_collection, --161-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_drr, --162-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_cc, --163-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_nto, --164-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_print, --165-- null string - CAST(NULL AS VARCHAR) AS pcn_warning_notice_extracted_for_print, --166-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_ofr, --167-- null string - CAST(NULL AS VARCHAR) AS pcn_pcn_extracted_for_warrant_request, --168-- null string - CAST(NULL AS VARCHAR) AS pcn_pre_debt_new_debtor_details, --169-- null string - CAST(NULL AS VARCHAR) AS pcn_importdattime, --170-- null string - CAST(NULL AS VARCHAR) AS pcn_importdatetime, --171-- null string - CAST('' AS VARCHAR) AS pcn_import_year, --172-- empty string - CAST('' AS VARCHAR) AS pcn_import_month, --173-- empty string - CAST('' AS VARCHAR) AS pcn_import_day, --174-- empty string - CAST('' AS VARCHAR) AS pcn_import_date, --175-- empty string - - /*officer monthly totals*/ - CAST('' AS VARCHAR) AS link_officer_total_rep_dates, - CAST('' AS VARCHAR) AS link_month_total, - CAST('' AS VARCHAR) AS mth_tot_rep_monthYear_response, - CAST('' AS VARCHAR) AS mth_tot_rep_total_dates, - CAST('' AS VARCHAR) AS mth_tot_rep_records, - CAST('' AS VARCHAR) AS tot_rep_response_date, - CAST('' AS VARCHAR) AS tot_rep_total_date, - CAST('' AS VARCHAR) AS tot_rep_records, - - /*Teams data*/ - t.t_start_date, - t.t_end_date, - t.t_team, - t.t_team_name, - t.t_role, - t.t_forename, - t.t_surname, - t.t_full_name, - t.t_qa_doc_created_by, - t.t_qa_doc_full_name, - t.t_post_title, - t.t_notes, - t.t_import_date, - - /* Partition columns moved to the end to keep schema alligned */ - d.import_year, - d.import_month, - d.import_day, - CAST(d.import_date AS VARCHAR) AS import_date - - FROM downtime d - LEFT JOIN team t - ON UPPER(t.t_full_name) = UPPER(d.Response_written_by) --downtime officer full name -) -SELECT * FROM liberator_with_team_gds -UNION -SELECT * FROM google_form ---order by downtime_timestamp desc -; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_gds.py b/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_gds.py deleted file mode 100644 index 8af59efef..000000000 --- a/scripts/jobs/parking/parking_correspondence_performance_records_with_pcn_gds.py +++ /dev/null @@ -1,601 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_correspondence_performance_records_with_pcn_gds" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/* -Correspondence Performance records last 13 months with PCN FOI records - --->> "dataplatform-prod-liberator-refined-zone"."parking_correspondence_performance_records_with_pcn_gds" ---<< "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" --- < "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" --- < "parking-raw-zone"."parking_correspondence_performance_teams" - -16/06/2022 - Created -30/11/2022 - with totals for Month and response dates for each officer -21/04/2023 - added teams data from google spreadsheet load - https://docs.google.com/spreadsheets/d/1zxZXX1_qU9NW93Ug1JUy7aXsnTz45qIj7Zftmi9trbI/edit?usp=sharing -04/03/2025 - Refactored SQL for migration back to AthenaSQL for dap-airflow -05/03/2025 - Removed redundant left joins from subqueries -14/04/2025 - Strictly applied column formatting consistent with previous Glue outputs. -16/04/2025 - Workaround to orchestration issue caused by latest "pcnfoidetails_pcn_foi_full" not yet ready -*/ -WITH - -liberator AS ( - - SELECT *, - - CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3)) AS current_utc_timestamp, - TRY(CAST(date_received AS TIMESTAMP(3))) AS date_received_timestamp, -- pre-filtered by WHERE clause - - IF(whenassigned <> '', -- contains probable valid timestamp - TRY(CAST(whenassigned AS TIMESTAMP(3))), - CAST(NULL AS TIMESTAMP(3)) -- edge case - ) AS whenassigned_timestamp, - - IF(Response_generated_at <> '', -- contains probable valid timestamp - TRY(CAST(Response_generated_at AS TIMESTAMP(3))), - CAST(NULL AS TIMESTAMP(3)) -- edge case - ) AS Response_generated_at_timestamp, - - CAST(CASE WHEN date_received <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), -- under condition of date_received <>'' - CURRENT_DATE - ) - --ELSE NULL - END AS INTEGER) AS days_since_date_received, - - CAST(CASE WHEN date_received <> '' AND whenassigned <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), -- under condition of date_received <>'' - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE) - ) - --ELSE NULL - END AS INTEGER) AS days_since_date_received_whenassigned, - - CAST(CASE WHEN whenassigned <> '' AND response_generated_at = '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE), - CURRENT_DATE - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_whenassigned, - - CAST(CASE WHEN whenassigned <> '' AND response_generated_at <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(whenassigned, 1, 10) AS DATE), - TRY_CAST(SUBSTR(response_generated_at, 1, 10) AS DATE) - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_whenassigned_response_generated_at, - - CAST(CASE WHEN date_received <> '' AND response_generated_at <> '' - THEN DATE_DIFF( - 'day', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), - TRY_CAST(SUBSTR(response_generated_at, 1, 10) AS DATE) - ) - --ELSE NULL -- edge case - END AS INTEGER) AS days_since_date_received_response_generated_at, - - /* officer_total_rep_dates */ - Response_written_by AS tot_rep_Response_written_by, - SUBSTR(response_generated_at, 1, 10) AS tot_rep_response_date, - CONCAT(SUBSTR(CAST(response_generated_at AS VARCHAR(10)), 1, 7), '-01') AS tot_rep_monthYear_response, - CONCAT( - Response_written_by, - SUBSTR(response_generated_at, 1, 10) - ) AS tot_rep_unique_link, - - /* total_response_month */ - Response_written_by AS mth_tot_rep_Response_written_by, - SUBSTR(response_generated_at, 1, 10) AS mth_tot_rep_response_date, - CONCAT(SUBSTR(CAST(response_generated_at AS VARCHAR(10)), 1, 7), '-01') AS mth_tot_rep_monthYear_response, - CONCAT( - Response_written_by, - CONCAT(SUBSTR(CAST(response_generated_at AS VARCHAR(10)), 1, 7), '-01') - ) AS mth_tot_rep_unique_link - - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - WHERE import_Date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" g - ) - AND LENGTH(ticketserialnumber) = 10 -- ticket filter - AND date_received <> '' -- is the overriding condition for "13 months from todays date"! - AND TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE) > CURRENT_DATE - INTERVAL '13' MONTH - -- Last 13 months from todays date -/* -- This alternative method captures slightly more records... - AND DATE_DIFF( - 'month', - TRY_CAST(SUBSTR(date_received, 1, 10) AS DATE), - CURRENT_DATE - ) <= 13 -*/ -), - -officer_total_rep_dates AS ( - SELECT - l.tot_rep_Response_written_by, - l.tot_rep_response_date, - l.tot_rep_monthYear_response, - l.tot_rep_unique_link, - COUNT(DISTINCT l.tot_rep_response_date) AS tot_rep_total_dates, - COUNT(*) AS tot_rep_records - -- The unreferenced p. caused SQL linter error ST11 - -- Then discoverd COUNT(*) was not expanded by the left join to p because p.pcn is unique within import_date - FROM liberator l -/* -- Therefore removed this redundant left join... - LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p - ON l.ticketserialnumber = p.pcn - AND l.import_date = p.import_date -*/ - GROUP BY - l.tot_rep_Response_written_by, - l.tot_rep_response_date, - l.tot_rep_monthYear_response, - l.tot_rep_unique_link -), - -total_response_month AS ( - SELECT - l.mth_tot_rep_Response_written_by, - l.mth_tot_rep_monthYear_response, - l.mth_tot_rep_unique_link, - COUNT(DISTINCT l.mth_tot_rep_response_date) AS mth_tot_rep_total_dates, - COUNT(*) AS mth_tot_rep_records - -- The unreferenced p. caused SQL linter error ST11 - -- Then discoverd COUNT(*) was not expanded by the left join to p because p.pcn is unique within import_date - FROM liberator l -/* -- Therefore removed this redundant left join... - LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p - ON l.ticketserialnumber = p.pcn - AND l.import_date = p.import_date -*/ - GROUP BY - l.mth_tot_rep_Response_written_by, - l.mth_tot_rep_monthYear_response, - l.mth_tot_rep_unique_link - --ORDER BY - -- l.mth_rep_monthYear_response DESC -), - -team AS ( - SELECT DISTINCT - "start_date" AS t_start_date, - end_date AS t_end_date, - team AS t_team, - team_name AS t_team_name, - "role" AS t_role, - forename AS t_forename, - surname AS t_surname, - full_name AS t_full_name, - qa_doc_created_by AS t_qa_doc_created_by, - qa_doc_full_name AS t_qa_doc_full_name, - post_title AS t_post_title, - notes AS t_notes, - import_date AS t_import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" - WHERE import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "parking-raw-zone"."parking_correspondence_performance_teams" g - ) -) - -SELECT - CAST(CASE - WHEN l.date_received <> '' AND l.whenassigned = '' - THEN 'Unassigned' - WHEN l.date_received <> '' AND l.whenassigned <> '' AND l.response_generated_at = '' - THEN 'Assigned' - WHEN l.date_received <> '' AND l.whenassigned <> '' AND l.response_generated_at <> '' - THEN 'Responded' - --ELSE NULL - END AS VARCHAR) AS response_status, - - CAST(l.current_utc_timestamp AS VARCHAR) AS current_time_stamp, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS unassigned_time, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.whenassigned_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS to_assigned_time, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.current_utc_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_time, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.whenassigned_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS assigned_response_time, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - THEN 'INTERVAL ''' - || REGEXP_REPLACE(CAST(l.Response_generated_at_timestamp - l.date_received_timestamp AS VARCHAR), '(\.\d+)\s*$', '') --removes mantissa - || ''' DAY TO SECOND' - --ELSE NULL - END AS VARCHAR) AS response_time, - - /*unassigned days*/ - CAST(CASE WHEN l.whenassigned = '' - THEN l.days_since_date_received - --ELSE NULL - END AS VARCHAR) AS unassigned_days, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - THEN - CASE - WHEN l.days_since_date_received <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received > 56 THEN '56 plus days' - --WHEN l.days_since_date_received IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS unassigned_days_group, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - AND l.days_since_date_received <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS unassigned_days_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned = '' - AND l.days_since_date_received <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS unassigned_days_kpiTotFourteenLess, - - /*Days to assign*/ - CAST(l.days_since_date_received_whenassigned AS VARCHAR) AS days_to_assign, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - THEN - CASE - WHEN l.days_since_date_received_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_whenassigned > 56 THEN '56 plus days' - --WHEN l.days_since_date_received_whenassigned IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS days_to_assign_group, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - AND l.days_since_date_received_whenassigned <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS Days_to_assign_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.whenassigned <> '' - AND l.days_since_date_received_whenassigned <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS Days_to_assign_kpiTotFourteenLess, - - /*assigned in progress days*/ - CAST(l.days_since_whenassigned AS VARCHAR) AS assigned_in_progress_days, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - THEN - CASE - WHEN l.days_since_whenassigned <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned > 56 THEN '56 plus days' - --WHEN l.days_since_whenassigned IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assigned_in_progress_days_group, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS assigned_in_progress_days_kpiTotFiftySixLess, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at = '' - AND l.days_since_whenassigned <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS assigned_in_progress_days_kpiTotFourteenLess, - - /*assigned response days*/ - CAST(l.days_since_whenassigned_response_generated_at AS VARCHAR) AS assignedResponseDays, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - THEN - CASE - WHEN l.days_since_whenassigned_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_whenassigned_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_whenassigned_response_generated_at > 56 THEN '56 plus days' - --WHEN l.days_since_whenassigned_response_generated_at IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS assignedResponseDays_group, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS assignedResponseDays_kpiTotFiftySixLess, - - CAST(CASE WHEN l.whenassigned <> '' AND l.response_generated_at <> '' - AND l.days_since_whenassigned_response_generated_at <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS assignedResponseDays_kpiTotFourteenLess, - - /*Response days*/ - CAST(l.days_since_date_received_response_generated_at AS VARCHAR) AS ResponseDays, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - THEN - CASE - WHEN l.days_since_date_received_response_generated_at <= 5 THEN '5 or Less days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 6 AND 14 THEN '6 to 14 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 15 AND 47 THEN '15 to 47 days' - WHEN l.days_since_date_received_response_generated_at BETWEEN 48 AND 56 THEN '48 to 56 days' - WHEN l.days_since_date_received_response_generated_at > 56 THEN '56 plus days' - --WHEN l.days_since_date_received_response_generated_at IS NULL THEN NULL -- edge case examined - --ELSE NULL - END - --ELSE NULL - END AS VARCHAR) AS ResponseDays_group, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 56 - THEN 1 - ELSE 0 - END AS INTEGER) AS ResponseDays_kpiTotFiftySixLess, - - CAST(CASE WHEN l.date_received <> '' AND l.response_generated_at <> '' - AND l.days_since_date_received_response_generated_at <= 14 - THEN 1 - ELSE 0 - END AS INTEGER) AS ResponseDays_kpiTotFourteenLess, - - l.Response_generated_at, --AS VARCHAR - l.Date_Received, --AS VARCHAR - - CAST(SUBSTR(TRY_CAST(l.date_received AS VARCHAR(10)), 1, 7) || '-01' AS VARCHAR) AS MonthYear, - - l.Type AS "type", --AS VARCHAR - l.Serviceable, --AS VARCHAR - l.Service_category, --AS VARCHAR - l.Response_written_by, --AS VARCHAR - l.Letter_template, --AS VARCHAR - l.Action_taken, --AS VARCHAR - l.Related_to_PCN, --AS VARCHAR - l.Cancellation_group, --AS VARCHAR - l.Cancellation_reason, --AS VARCHAR - l.whenassigned, --AS VARCHAR - l.ticketserialnumber, --AS VARCHAR - l.noderef, --AS VARCHAR - - CAST(l.record_created AS VARCHAR) AS record_created, -- cast from VARCHAR see **NOTE** below - l.import_timestamp, --AS VARCHAR - - /*** pcn columns taken from "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ***/ - -- Data sourced from the refined zone needs to be handled carefully... - -- Do not assume the following output column datatypes will be the same as "pcnfoidetails_pcn_foi_full". - -- Being left-joined to "pcnfoidetails_pcn_foi_full" will cause NULLs to appear in these columns. - -- And similar transforms elsewhere require different translations to the ones below... - /* These are the current automated translations developed for this transform:- - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS DATE), '%Y-%m-%d'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try date to string - TRY_CAST(p.[source_column] AS INTEGER) AS [target_column], --[position]-- try int to int - TRY_CAST(p.[source_column] AS VARCHAR) AS [target_column], --[position]-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.[source_column] AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.[source_column] AS VARCHAR)) AS [target_column], --[position]-- try timestamp to string - TRY_CAST(p.[source_column] AS TIMESTAMP) AS [target_column], --[position]-- try timestamp to timestamp - */ - -- These translations try to deal with problems without resorting to throwing errors, tolerating a reasonable degree of schema evolution in the "pcnfoidetails_pcn_foi_full" source. - -- For example, date and timestamp string formatting works with either string, date and timestamp source columns and works like this... - /* If source can be cast to a date then it can be correctly date-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS DATE), '%Y-%m-%d'), - CAST(p.xxxx AS VARCHAR) --...and can be extended to handle other date formats. - ) AS pcn_xxxx, -- from DATE - */ - /* If source can be cast to a timestamp then it can be correctly timestamp-formatted into a string, otherwise just cast whatever it was to a string. - COALESCE( - DATE_FORMAT(TRY_CAST(p.xxxx AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), - CAST(p.xxxx AS VARCHAR) -- Alternative for transforming a time-zoned timestamp... - --...COALESCE(DATE_FORMAT(TRY_CAST(TRY(From_iso8601_timestamp(p.xxxx)) AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.xxxx AS VARCHAR)) - --...and can be extended to handle other timestamp formats. - ) AS pcn_xxxx, -- from TIMESTAMP - */ - TRY_CAST(p.pcn AS VARCHAR) AS pcn_pcn, --45-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedate AS DATE), '%Y-%m-%d'), CAST(p.pcnissuedate AS VARCHAR)) AS pcn_pcnissuedate, --46-- try date to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcnissuedatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcnissuedatetime AS VARCHAR)) AS pcn_pcnissuedatetime, --47-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_canx_date AS DATE), '%Y-%m-%d'), CAST(p.pcn_canx_date AS VARCHAR)) AS pcn_pcn_canx_date, --48-- try date to string - TRY_CAST(p.cancellationgroup AS VARCHAR) AS pcn_cancellationgroup, --49-- try string to string (default) - TRY_CAST(p.cancellationreason AS VARCHAR) AS pcn_cancellationreason, --50-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_casecloseddate AS DATE), '%Y-%m-%d'), CAST(p.pcn_casecloseddate AS VARCHAR)) AS pcn_pcn_casecloseddate, --51-- try date to string - TRY_CAST(p.street_location AS VARCHAR) AS pcn_street_location, --52-- try string to string (default) - TRY_CAST(p.whereonlocation AS VARCHAR) AS pcn_whereonlocation, --53-- try string to string (default) - TRY_CAST(p.zone AS VARCHAR) AS pcn_zone, --54-- try string to string (default) - TRY_CAST(p.usrn AS VARCHAR) AS pcn_usrn, --55-- try string to string (default) - TRY_CAST(p.contraventioncode AS VARCHAR) AS pcn_contraventioncode, --56-- try string to string (default) - TRY_CAST(p.contraventionsuffix AS VARCHAR) AS pcn_contraventionsuffix, --57-- try string to string (default) - TRY_CAST(p.debttype AS VARCHAR) AS pcn_debttype, --58-- try string to string (default) - TRY_CAST(p.vrm AS VARCHAR) AS pcn_vrm, --59-- try string to string (default) - TRY_CAST(p.vehiclemake AS VARCHAR) AS pcn_vehiclemake, --60-- try string to string (default) - TRY_CAST(p.vehiclemodel AS VARCHAR) AS pcn_vehiclemodel, --61-- try string to string (default) - TRY_CAST(p.vehiclecolour AS VARCHAR) AS pcn_vehiclecolour, --62-- try string to string (default) - TRY_CAST(p.ceo AS VARCHAR) AS pcn_ceo, --63-- try string to string (default) - TRY_CAST(p.ceodevice AS VARCHAR) AS pcn_ceodevice, --64-- try string to string (default) - TRY_CAST(p.current_30_day_flag AS INTEGER) AS pcn_current_30_day_flag, --65-- try int to int - TRY_CAST(p.isvda AS INTEGER) AS pcn_isvda, --66-- try int to int - TRY_CAST(p.isvoid AS INTEGER) AS pcn_isvoid, --67-- try int to int - TRY_CAST(p.isremoval AS VARCHAR) AS pcn_isremoval, --68-- try string to string (default) - TRY_CAST(p.driverseen AS VARCHAR) AS pcn_driverseen, --69-- try string to string (default) - TRY_CAST(p.allwindows AS VARCHAR) AS pcn_allwindows, --70-- try string to string (default) - TRY_CAST(p.parkedonfootway AS VARCHAR) AS pcn_parkedonfootway, --71-- try string to string (default) - TRY_CAST(p.doctor AS VARCHAR) AS pcn_doctor, --72-- try string to string (default) - TRY_CAST(p.warningflag AS INTEGER) AS pcn_warningflag, --73-- try int to int - TRY_CAST(p.progressionstage AS VARCHAR) AS pcn_progressionstage, --74-- try string to string (default) - TRY_CAST(p.nextprogressionstage AS VARCHAR) AS pcn_nextprogressionstage, --75-- try string to string (default) - TRY_CAST(p.nextprogressionstagestarts AS VARCHAR) AS pcn_nextprogressionstagestarts, --76-- try string to string (default) - TRY_CAST(p.holdreason AS VARCHAR) AS pcn_holdreason, --77-- try string to string (default) - TRY_CAST(p.lib_initial_debt_amount AS VARCHAR) AS pcn_lib_initial_debt_amount, --78-- try string to string (default) - TRY_CAST(p.lib_payment_received AS VARCHAR) AS pcn_lib_payment_received, --79-- try string to string (default) - TRY_CAST(p.lib_write_off_amount AS VARCHAR) AS pcn_lib_write_off_amount, --80-- try string to string (default) - TRY_CAST(p.lib_payment_void AS VARCHAR) AS pcn_lib_payment_void, --81-- try string to string (default) - TRY_CAST(p.lib_payment_method AS VARCHAR) AS pcn_lib_payment_method, --82-- try string to string (default) - TRY_CAST(p.lib_payment_ref AS VARCHAR) AS pcn_lib_payment_ref, --83-- try string to string (default) - TRY_CAST(p.baliff_from AS VARCHAR) AS pcn_baliff_from, --84-- try string to string (default) - TRY_CAST(p.bailiff_to AS VARCHAR) AS pcn_bailiff_to, --85-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.bailiff_processedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.bailiff_processedon AS VARCHAR)) AS pcn_bailiff_processedon, --86-- try timestamp to string - TRY_CAST(p.bailiff_redistributionreason AS VARCHAR) AS pcn_bailiff_redistributionreason, --87-- try string to string (default) - TRY_CAST(p.bailiff AS VARCHAR) AS pcn_bailiff, --88-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.warrantissuedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrantissuedate AS VARCHAR)) AS pcn_warrantissuedate, --89-- try timestamp to string - TRY_CAST(p.allocation AS INTEGER) AS pcn_allocation, --90-- try int to int - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_datenotified AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_datenotified AS VARCHAR)) AS pcn_eta_datenotified, --91-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_packsubmittedon AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_packsubmittedon AS VARCHAR)) AS pcn_eta_packsubmittedon, --92-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_evidencedate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_evidencedate AS VARCHAR)) AS pcn_eta_evidencedate, --93-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.eta_adjudicationdate AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.eta_adjudicationdate AS VARCHAR)) AS pcn_eta_adjudicationdate, --94-- try timestamp to string - TRY_CAST(p.eta_appealgrounds AS VARCHAR) AS pcn_eta_appealgrounds, --95-- try string to string (default) - TRY_CAST(p.eta_decisionreceived AS TIMESTAMP) AS pcn_eta_decisionreceived, --96-- try timestamp to timestamp - TRY_CAST(p.eta_outcome AS VARCHAR) AS pcn_eta_outcome, --97-- try string to string (default) - TRY_CAST(p.eta_packsubmittedby AS VARCHAR) AS pcn_eta_packsubmittedby, --98-- try string to string (default) - TRY_CAST(p.cancelledby AS VARCHAR) AS pcn_cancelledby, --99-- try string to string (default) - TRY_CAST(p.registered_keeper_address AS VARCHAR) AS pcn_registered_keeper_address, --100-- try string to string (default) - TRY_CAST(p.current_ticket_address AS VARCHAR) AS pcn_current_ticket_address, --101-- try string to string (default) - TRY_CAST(p.corresp_dispute_flag AS INTEGER) AS pcn_corresp_dispute_flag, --102-- try int to int - TRY_CAST(p.keyworker_corresp_dispute_flag AS INTEGER) AS pcn_keyworker_corresp_dispute_flag, --103-- try int to int - TRY_CAST(p.fin_year_flag AS VARCHAR) AS pcn_fin_year_flag, --104-- try string to string (default) - TRY_CAST(p.fin_year AS VARCHAR) AS pcn_fin_year, --105-- try string to string (default) - TRY_CAST(p.ticket_ref AS VARCHAR) AS pcn_ticket_ref, --106-- try string to string (default) - COALESCE(DATE_FORMAT(TRY_CAST(p.nto_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nto_printed AS VARCHAR)) AS pcn_nto_printed, --107-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.appeal_accepted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.appeal_accepted AS VARCHAR)) AS pcn_appeal_accepted, --108-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.arrived_in_pound AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.arrived_in_pound AS VARCHAR)) AS pcn_arrived_in_pound, --109-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cancellation_reversed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cancellation_reversed AS VARCHAR)) AS pcn_cancellation_reversed, --110-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.cc_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.cc_printed AS VARCHAR)) AS pcn_cc_printed, --111-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.drr AS VARCHAR)) AS pcn_drr, --112-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.en_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.en_printed AS VARCHAR)) AS pcn_en_printed, --113-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_released AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_released AS VARCHAR)) AS pcn_hold_released, --114-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_response AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_response AS VARCHAR)) AS pcn_dvla_response, --115-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.dvla_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.dvla_request AS VARCHAR)) AS pcn_dvla_request, --116-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.full_rate_uplift AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.full_rate_uplift AS VARCHAR)) AS pcn_full_rate_uplift, --117-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.hold_until AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.hold_until AS VARCHAR)) AS pcn_hold_until, --118-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_at AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_at AS VARCHAR)) AS pcn_lifted_at, --119-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.lifted_by AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.lifted_by AS VARCHAR)) AS pcn_lifted_by, --120-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.loaded AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.loaded AS VARCHAR)) AS pcn_loaded, --121-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.nor_sent AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.nor_sent AS VARCHAR)) AS pcn_nor_sent, --122-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.notice_held AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.notice_held AS VARCHAR)) AS pcn_notice_held, --123-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ofr_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ofr_printed AS VARCHAR)) AS pcn_ofr_printed, --124-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_printed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_printed AS VARCHAR)) AS pcn_pcn_printed, --125-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_nto_requested AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_nto_requested AS VARCHAR)) AS pcn_reissue_nto_requested, --126-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.reissue_pcn AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.reissue_pcn AS VARCHAR)) AS pcn_reissue_pcn, --127-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.set_back_to_pre_cc_stage AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.set_back_to_pre_cc_stage AS VARCHAR)) AS pcn_set_back_to_pre_cc_stage, --128-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.vehicle_released_for_auction AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.vehicle_released_for_auction AS VARCHAR)) AS pcn_vehicle_released_for_auction, --129-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_issued AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_issued AS VARCHAR)) AS pcn_warrant_issued, --130-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_redistributed AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_redistributed AS VARCHAR)) AS pcn_warrant_redistributed, --131-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warrant_request_granted AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warrant_request_granted AS VARCHAR)) AS pcn_warrant_request_granted, --132-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.ad_hoc_vq4_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.ad_hoc_vq4_request AS VARCHAR)) AS pcn_ad_hoc_vq4_request, --133-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.paper_vq5_received AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.paper_vq5_received AS VARCHAR)) AS pcn_paper_vq5_received, --134-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_buslane AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_buslane AS VARCHAR)) AS pcn_pcn_extracted_for_buslane, --135-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_pre_debt AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_pre_debt AS VARCHAR)) AS pcn_pcn_extracted_for_pre_debt, --136-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_collection AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_collection AS VARCHAR)) AS pcn_pcn_extracted_for_collection, --137-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_drr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_drr AS VARCHAR)) AS pcn_pcn_extracted_for_drr, --138-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_cc AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_cc AS VARCHAR)) AS pcn_pcn_extracted_for_cc, --139-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_nto AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_nto AS VARCHAR)) AS pcn_pcn_extracted_for_nto, --140-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_print AS VARCHAR)) AS pcn_pcn_extracted_for_print, --141-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.warning_notice_extracted_for_print AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.warning_notice_extracted_for_print AS VARCHAR)) AS pcn_warning_notice_extracted_for_print, --142-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_ofr AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_ofr AS VARCHAR)) AS pcn_pcn_extracted_for_ofr, --143-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pcn_extracted_for_warrant_request AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pcn_extracted_for_warrant_request AS VARCHAR)) AS pcn_pcn_extracted_for_warrant_request, --144-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.pre_debt_new_debtor_details AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.pre_debt_new_debtor_details AS VARCHAR)) AS pcn_pre_debt_new_debtor_details, --145-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdattime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdattime AS VARCHAR)) AS pcn_importdattime, --146-- try timestamp to string - COALESCE(DATE_FORMAT(TRY_CAST(p.importdatetime AS TIMESTAMP), '%Y-%m-%d %H:%i:%s'), CAST(p.importdatetime AS VARCHAR)) AS pcn_importdatetime, --147-- try timestamp to string - TRY_CAST(p.import_year AS VARCHAR) AS pcn_import_year, --148-- try string to string (default) - TRY_CAST(p.import_month AS VARCHAR) AS pcn_import_month, --149-- try string to string (default) - TRY_CAST(p.import_day AS VARCHAR) AS pcn_import_day, --150-- try string to string (default) - TRY_CAST(p.import_date AS VARCHAR) AS pcn_import_date, --151-- try string to string (default) - - /* Links to aggregations */ - CAST(l.tot_rep_unique_link AS VARCHAR) AS link_officer_total_rep_dates, - CAST(l.mth_tot_rep_unique_link AS VARCHAR) AS link_month_total, - - /* Monthly aggregations */ - CAST(m.mth_tot_rep_monthYear_response AS VARCHAR) AS mth_tot_rep_monthYear_response, - TRY_CAST(m.mth_tot_rep_total_dates AS BIGINT) AS mth_tot_rep_total_dates, - TRY_CAST(m.mth_tot_rep_records AS BIGINT) AS mth_tot_rep_records, - - /* Daily Aggregations */ - CAST(d.tot_rep_response_date AS VARCHAR) AS tot_rep_response_date, - TRY_CAST(d.tot_rep_total_dates AS BIGINT) AS tot_rep_total_dates, - TRY_CAST(d.tot_rep_records AS BIGINT) AS tot_rep_records, - - /* teams columns expanded by above subquery */ - team.*, - - /* Partition columns moved to the end to keep schema alligned */ - l.import_year, - l.import_month, - l.import_day, - l.import_date - -FROM liberator l -- < "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" -LEFT JOIN "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" p -/* - ON p.import_date = l.import_date -- joined on import partition - Fails when "pcnfoidetails_pcn_foi_full" hasn't produced data for l.import_date yet... - ...resulting in empty pcn_ columns in the output. - Airflow's orchestration should avoid this after PCNFOIDetails_PCN_FOI_FULL.sql has been migrated... - ...but for now we'll just use the following simple workaround... -*/ - ON p.import_date IN ( - SELECT MAX(g.import_date) AS import_date - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" g - ) - AND l.ticketserialnumber = p.pcn -- ticketserialnumber is valid -LEFT JOIN officer_total_rep_dates d -- < Daily Aggregations of (l LEFT JOIN p) - ON l.tot_rep_unique_link = d.tot_rep_unique_link -LEFT JOIN total_response_month m -- < Monthly Aggregations of (l LEFT JOIN p) - ON l.mth_tot_rep_unique_link = m.mth_tot_rep_unique_link - LEFT JOIN team -- < "parking-raw-zone"."parking_correspondence_performance_teams" - ON UPPER(team.t_full_name) = UPPER(l.Response_written_by) -- Can this name match be improved? -; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_eta_decision_records_pcn_dispute_gds.py b/scripts/jobs/parking/parking_eta_decision_records_pcn_dispute_gds.py deleted file mode 100644 index 2d3a6a7fd..000000000 --- a/scripts/jobs/parking/parking_eta_decision_records_pcn_dispute_gds.py +++ /dev/null @@ -1,1859 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -Note: python file name should be the same as the table name -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_eta_decision_records_pcn_dispute_gds" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ ---> "dataplatform-prod-liberator-refined-zone"."parking_eta_decision_records_pcn_dispute_gds" ---< Orchestration depends upon "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" ---< Note "import_date" is unsynchronized across all source tables so may be misaligned if unchecked by orchestration. -/*23/03/2022 - created feed - 07/04/2022 - added field for pcn stacking/grouping if pcn disputed/eta or not - added flags for HQ records as 'HQ_SiDEM_PCN' - updated join to match pcns with end special chars - */ -WITH er AS ( - SELECT case_reference, - name, - pcn AS er_pcn, - hearing_type, - case_status, - decision_i_e_dnc_appeal_allowed_appeal_rejected, - review_flag, - reporting_period, - month_year, - import_datetime AS er_import_datetime, - import_timestamp AS er_import_timestamp, - import_year AS er_import_year, - import_month AS er_import_month, - import_day AS er_import_day, - import_date AS er_import_date - FROM "parking-raw-zone"."parking_eta_decision_records" -- in place of "eta_decision_records" - WHERE import_date = ( - SELECT MAX(import_date) - FROM "parking-raw-zone"."parking_eta_decision_records" -- in place of "eta_decision_records" - ) -), -eta_recs AS ( - SELECT pcn AS etar_pcn, - SUM( - CASE - WHEN decision_i_e_dnc_appeal_allowed_appeal_rejected IN ('Appeal Rejected', 'Appeal Refused') THEN 1 - ELSE 0 - END - ) AS appeal_rejected, - SUM( - CASE - WHEN decision_i_e_dnc_appeal_allowed_appeal_rejected IN ('Appeal Allowed', 'appeal Allowed') THEN 1 - ELSE 0 - END - ) AS appeal_allowed, - SUM( - CASE - WHEN decision_i_e_dnc_appeal_allowed_appeal_rejected IN ('DNC', 'dnc') THEN 1 - ELSE 0 - END - ) AS appeal_dnc, - SUM( - CASE - WHEN decision_i_e_dnc_appeal_allowed_appeal_rejected IN ('Appeal with Direction', 'Direction') THEN 1 - ELSE 0 - END - ) AS appeal_with_direction - FROM "parking-raw-zone"."parking_eta_decision_records" -- in place of "eta_decision_records" - WHERE import_date = ( - SELECT MAX(import_date) - FROM "parking-raw-zone"."parking_eta_decision_records" -- in place of "eta_decision_records" - ) - GROUP BY pcn -), -Disputes AS ( - SELECT ticketserialnumber, - COUNT(DISTINCT ticketserialnumber) AS TotalpcnDisputed, - COUNT(ticketserialnumber) AS TotalDisputed, - /*-- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - date_diff('day', cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays - */ - import_date AS dispute_import_date - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - WHERE import_date = ( - SELECT MAX(import_date) - FROM "dataplatform-prod-liberator-raw-zone"."liberator_pcn_ic" - ) - AND date_received != '' - AND response_generated_at != '' - AND LENGTH(ticketserialnumber) = 10 - AND Serviceable IN ( - 'Challenges', - 'Key worker', - 'Removals', - 'TOL', - 'Charge certificate', - 'Representations' - ) - GROUP BY ticketserialnumber, - import_date -), -pcn AS ( - SELECT * - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" - WHERE import_date = ( - SELECT MAX(import_date) - FROM "dataplatform-prod-liberator-refined-zone"."pcnfoidetails_pcn_foi_full" - ) -), -transform AS ( - SELECT DISTINCT CASE - WHEN ZONE LIKE 'Estates' THEN usrn - WHEN UPPER(SUBSTR(er.er_pcn, 1, 2)) = 'HQ' THEN 'HQ_SiDEM_PCN' - ELSE ZONE - END AS r_zone, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' THEN 'onstreet' - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) THEN 'Estates' - WHEN debttype LIKE 'CCTV%' THEN 'CCTV' - WHEN ZONE LIKE 'Car Parks' THEN 'Car Parks' - WHEN UPPER(SUBSTR(er.er_pcn, 1, 2)) = 'HQ' THEN 'HQ_SiDEM_PCN' - ELSE debttype - END AS pcn_type, - /* LTN Camera/location list as of 15/03/2022 - https://docs.google.com/spreadsheets/d/12_QqvToXPAMMLRMUQpQtwJg9gRZR0vMs_LQghhdwiYw*/ - CASE - WHEN street_location = 'Allen Road' THEN 'YB1052 - Allen Road' - WHEN street_location = 'Ashenden Road junction of Glyn Road' THEN 'LW2205 - Ashenden Road' - WHEN street_location = 'Barnabas Road JCT Berger Road' THEN 'LW2204 - Barnabas Road' - WHEN street_location = 'Barnabas Road JCT Oriel Road' THEN 'LW2204 - Barnabas Road' - WHEN street_location = 'Benthal Road' THEN 'LW2206 - Benthal Road' - WHEN street_location = 'Bouverie Road junction of Stoke Newington Church Street' THEN 'LW2593 - Bouverie Road' - WHEN street_location = 'Clissold Crescent' THEN 'LW2397 - Clissold Crescent' - WHEN street_location = 'Cremer Street' THEN 'LW2041 - Cremer Street' - WHEN street_location = 'Downs Road' THEN 'LW2389 - Downs Road' - WHEN street_location = 'Elsdale Street' THEN 'LW2393 - Elsdale Street' - WHEN street_location = 'Gore Road junction of Lauriston Road.' THEN 'LW2078 - Gore Road' - WHEN street_location = 'Hyde Road' THEN 'LW2208 - Hyde Road' - WHEN street_location = 'Hyde Road JCT Northport Street' THEN 'LW2208 - Hyde Road' - WHEN street_location = 'Lee Street junction of Stean Street' THEN 'LW1535 - Lee Street' - WHEN street_location = 'Loddiges Road jct Frampton Park Road' THEN 'LW1051 - Loddiges Road' - WHEN street_location = 'Lordship Road junction of Lordship Terrace' THEN 'LW2590 - Lordship Road' - WHEN street_location = 'Maury Road junction of Evering Road' THEN 'LW2390 - Maury Road' - WHEN street_location = 'Mead Place' THEN 'LW2395 - Mead Place' - WHEN street_location = 'Meeson Street junction of Kingsmead Way' THEN 'LW2079 - Meeson Street' - WHEN street_location = 'Mount Pleasant Lane' THEN 'LW2391 - Mount Pleasant Lane' - WHEN street_location = 'Nevill Road junction of Barbauld Road' THEN 'LW2595 - Nevill Road/ Barbauld Road' - WHEN street_location = 'Nevill Road junction of Osterley Road' THEN 'LW0633 - Nevill Road (Osterley Road)' - WHEN street_location = 'Neville Road junction of Osterley Road' THEN 'LW0633 - Nevill Road (Osterley Road)' - WHEN street_location = 'Oldfield Road (E)' THEN 'LW2596 - Oldfield Road' - WHEN street_location = 'Oldfield Road junction of Kynaston Road' THEN 'LW2596 - Oldfield Road' - WHEN street_location = 'Pitfield Street (F)' THEN 'LW2207 - Pitfield Street' - WHEN street_location = 'Pitfield Street JCT Hemsworth Street' THEN 'LW2207 - Pitfield Street' - WHEN street_location = 'Powell Road junction of Kenninghall Road' THEN 'LW1691 - Powell Road (Kenninghall Road)' - WHEN street_location = 'Shepherdess Walk' THEN 'LW2076 - Shepherdess Walk' - WHEN street_location = 'Shore Place' THEN 'Mobile camera car - Shore Place' - WHEN street_location = 'Stoke Newington Church Street junction of Lordship Road - Eastbound' THEN 'LW2591 - Stoke Newington Church Street eastbound' - WHEN street_location = 'Stoke Newington Church Street junction of Marton Road - Westbound' THEN 'LW2592 - Stoke Newington Church Street westbound' - WHEN street_location = 'Ufton Road junction of Downham Road' THEN 'LW2077 - Ufton Road' - WHEN street_location = 'Wayland Avenue' THEN 'LW2392 - Wayland Avenue' - WHEN street_location = 'Weymouth Terrace junction of Dunloe Street' THEN 'Mobile camera car - Weymouth Terrace' - WHEN street_location = 'Wilton Way junction of Greenwood Road' THEN 'LW1457 - Wilton Way' - WHEN street_location = 'Woodberry Grove junction of Rowley Gardens' THEN 'LW1457 - Woodberry Grove' - WHEN street_location = 'Woodberry Grove junction of Seven Sisters Road' THEN 'LW1457 - Woodberry Grove' - WHEN street_location = 'Yoakley Road junction of Stoke Newington Church Street' THEN 'LW2594 - Yoakley Road' - ELSE 'NOT current LTN Camera Location' - END AS ltn_camera_location, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS kpi_pcns, - /*PCNs by Type with VDA's excluded before and included after 1st June 2021*/ - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS Flg_kpi_onstreet_carparks, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS Flg_kpi_Estates, - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS Flg_kpi_CCTV, - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS Flg_kpi_Car_Parks, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 THEN 1 - ELSE 0 - END AS Flg_kpi_onstreet, - -- Disputed pcns and by pcn type - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND (isvda = 0) - AND (isvoid = 0) - AND (warningflag = 0) - AND Disputes.ticketserialnumber IS NOT NULL THEN 1 - ELSE 0 - END Flg_kpi_onstreet_carparks_disputes, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND (isvda = 0) - AND (isvoid = 0) - AND (warningflag = 0) - AND Disputes.ticketserialnumber IS NOT NULL THEN 1 - ELSE 0 - END Flag_kpi_Estates_disputes, - CASE - WHEN (debttype LIKE 'CCTV%') - AND (isvda = 0) - AND (isvoid = 0) - AND (warningflag = 0) - AND Disputes.ticketserialnumber IS NOT NULL THEN 1 - ELSE 0 - END AS Flag_kpi_CCTV_disputes, - CASE - WHEN (ZONE LIKE 'Car Parks') - AND (isvda = 0) - AND (isvoid = 0) - AND (warningflag = 0) - AND Disputes.ticketserialnumber IS NOT NULL THEN 1 - ELSE 0 - END AS Flag_kpi_Car_Parks_disputes, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND (isvda = 0) - AND (isvoid = 0) - AND (warningflag = 0) - AND Disputes.ticketserialnumber IS NOT NULL THEN 1 - ELSE 0 - END AS Flg_kpi_onstreet_disputes, - /*onstreet_carparks ETA Decisions*/ - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_onstreet_carparks, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_onstreet_carparks, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_onstreet_carparks, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_onstreet_carparks, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_onstreet_carparks, - /*Estates ETA Decisions*/ - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_Estates, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_Estates, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_Estates, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_Estates, - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_Estates, - /*CCTV ETA Decisions*/ - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_CCTV, - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_CCTV, - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_CCTV, - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_CCTV, - CASE - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_CCTV, - /*Car_Parks ETA Decisions*/ - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_Car_Parks, - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_Car_Parks, - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_Car_Parks, - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_Car_Parks, - CASE - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_Car_Parks, - /*onstreet ETA Decisions*/ - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_onstreet, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_onstreet, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_onstreet, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_onstreet, - CASE - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_onstreet, - /* All kpi ETA Decisions*/ - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 THEN eta_recs.appeal_rejected - ELSE 0 - END AS Flg_decision_appeal_rejected_all_kpi, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 THEN eta_recs.appeal_allowed - ELSE 0 - END AS Flg_decision_appeal_allowed_all_kpi, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 THEN eta_recs.appeal_dnc - ELSE 0 - END AS Flg_decision_appeal_dnc_all_kpi, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 THEN eta_recs.appeal_with_direction - ELSE 0 - END AS Flg_decision_appeal_with_direction_all_kpi, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) THEN ( - eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction - ) - ELSE 0 - END AS Flg_eta_decision_all_kpi, - /*PCNs catergorised not eta or disputed for stacking*/ - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'Not dispute or eta' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NOT NULL THEN 'disputed' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'eta with direction' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_dnc > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'eta dnc' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_allowed > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'eta allowed' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_rejected > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'eta rejected' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'disputed and eta with direction' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'disputed and eta dnc' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'disputed and eta allowed' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'disputed and eta rejected' - ELSE 'NOT KPI' - END AS kpi_pcn_dispute_eta_group, - CASE - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NOT NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_dnc > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_allowed > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - AND eta_recs.appeal_rejected > 0 - AND Disputes.ticketserialnumber IS NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_with_direction > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_dnc > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_allowed > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'Y' - WHEN ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND eta_recs.appeal_rejected > 0 - AND Disputes.ticketserialnumber IS NOT NULL THEN 'Y' - ELSE 'N' - END AS kpi_pcn_dispute_eta_flag, - /*PCNs not eta or disputed*/ - CASE - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'Estates' - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'CCTV' - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'Car_Parks' - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected = 0 - AND eta_recs.appeal_allowed = 0 - AND eta_recs.appeal_dnc = 0 - AND eta_recs.appeal_with_direction = 0 - ) - OR ( - eta_recs.appeal_rejected IS NULL - AND eta_recs.appeal_allowed IS NULL - AND eta_recs.appeal_dnc IS NULL - AND eta_recs.appeal_with_direction IS NULL - ) - ) - AND Disputes.ticketserialnumber IS NULL THEN 'onstreet' - /*disputed by kpi pcn type*/ - WHEN ( - (ZONE LIKE 'Estates') - OR (street_location LIKE '%Estate%') - OR (usrn LIKE 'Z%') - ) - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) - OR (Disputes.ticketserialnumber IS NOT NULL) - ) THEN 'Estates - disputed_eta' - WHEN debttype LIKE 'CCTV%' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) - OR (Disputes.ticketserialnumber IS NOT NULL) - ) THEN 'CCTV - disputed_eta' - WHEN ZONE LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) - OR (Disputes.ticketserialnumber IS NOT NULL) - ) THEN 'Car_Parks - disputed_eta' - WHEN debttype NOT LIKE 'CCTV%' - AND ZONE NOT LIKE 'Estates' - AND street_location NOT LIKE '%Estate%' - AND usrn NOT LIKE 'Z%' - AND ZONE NOT LIKE 'Car Parks' - AND ( - ( - isvda = 0 - AND CAST(pcnissuedate AS DATE) < CAST('2021-06-01' AS DATE) - ) - OR ( - CAST(pcnissuedate AS DATE) > CAST('2021-05-31' AS DATE) - ) - ) - AND isvoid = 0 - AND warningflag = 0 - AND ( - ( - eta_recs.appeal_rejected > 0 - OR eta_recs.appeal_allowed > 0 - OR eta_recs.appeal_dnc > 0 - OR eta_recs.appeal_with_direction > 0 - ) - OR (Disputes.ticketserialnumber IS NOT NULL) - ) THEN 'onstreet - disputed_eta' - ELSE 'NOT KPI' - END AS kpi_pcn_not_dispute_eta_name, - * - /*** Collect all columns from the source tables including columns we don't yet know about. ***/ - FROM er - LEFT JOIN eta_recs ON SUBSTR(eta_recs.etar_pcn, 1, 10) = SUBSTR(er.er_pcn, 1, 10) - LEFT JOIN Disputes ON Disputes.ticketserialnumber = SUBSTR(er.er_pcn, 1, 10) - LEFT JOIN pcn ON SUBSTR(er.er_pcn, 1, 10) = pcn.pcn -) -SELECT -- To mitigate future side effects from * wildcard columns in the legacy queries above, the following query - -- explicitly define the output columns with data types cast to exactly match the existing target table... - --> "dataplatform-prod-liberator-refined-zone"."parking_eta_decision_records_pcn_dispute_gds" - CAST(r_zone AS VARCHAR) AS r_zone, - -->string - CAST(pcn_type AS VARCHAR) AS pcn_type, - -->string - CAST(ltn_camera_location AS VARCHAR) AS ltn_camera_location, - -->string - CAST(flg_kpi_onstreet_carparks AS INTEGER) AS flg_kpi_onstreet_carparks, - -->int - CAST(flg_kpi_estates AS INTEGER) AS flg_kpi_estates, - -->int - CAST(flg_kpi_cctv AS INTEGER) AS flg_kpi_cctv, - -->int - CAST(flg_kpi_car_parks AS INTEGER) AS flg_kpi_car_parks, - -->int - CAST(flg_kpi_onstreet AS INTEGER) AS flg_kpi_onstreet, - -->int - CAST(flg_kpi_onstreet_carparks_disputes AS INTEGER) AS flg_kpi_onstreet_carparks_disputes, - -->int - CAST(flag_kpi_estates_disputes AS INTEGER) AS flag_kpi_estates_disputes, - -->int - CAST(flag_kpi_cctv_disputes AS INTEGER) AS flag_kpi_cctv_disputes, - -->int - CAST(flag_kpi_car_parks_disputes AS INTEGER) AS flag_kpi_car_parks_disputes, - -->int - CAST(flg_kpi_onstreet_disputes AS INTEGER) AS flg_kpi_onstreet_disputes, - -->int - CAST( - flg_decision_appeal_rejected_onstreet_carparks AS BIGINT - ) AS flg_decision_appeal_rejected_onstreet_carparks, - -->bigint - CAST( - flg_decision_appeal_allowed_onstreet_carparks AS BIGINT - ) AS flg_decision_appeal_allowed_onstreet_carparks, - -->bigint - CAST( - flg_decision_appeal_dnc_onstreet_carparks AS BIGINT - ) AS flg_decision_appeal_dnc_onstreet_carparks, - -->bigint - CAST( - flg_decision_appeal_with_direction_onstreet_carparks AS BIGINT - ) AS flg_decision_appeal_with_direction_onstreet_carparks, - -->bigint - CAST(flg_eta_decision_onstreet_carparks AS BIGINT) AS flg_eta_decision_onstreet_carparks, - -->bigint - CAST(flg_decision_appeal_rejected_estates AS BIGINT) AS flg_decision_appeal_rejected_estates, - -->bigint - CAST(flg_decision_appeal_allowed_estates AS BIGINT) AS flg_decision_appeal_allowed_estates, - -->bigint - CAST(flg_decision_appeal_dnc_estates AS BIGINT) AS flg_decision_appeal_dnc_estates, - -->bigint - CAST( - flg_decision_appeal_with_direction_estates AS BIGINT - ) AS flg_decision_appeal_with_direction_estates, - -->bigint - CAST(flg_eta_decision_estates AS BIGINT) AS flg_eta_decision_estates, - -->bigint - CAST(flg_decision_appeal_rejected_cctv AS BIGINT) AS flg_decision_appeal_rejected_cctv, - -->bigint - CAST(flg_decision_appeal_allowed_cctv AS BIGINT) AS flg_decision_appeal_allowed_cctv, - -->bigint - CAST(flg_decision_appeal_dnc_cctv AS BIGINT) AS flg_decision_appeal_dnc_cctv, - -->bigint - CAST( - flg_decision_appeal_with_direction_cctv AS BIGINT - ) AS flg_decision_appeal_with_direction_cctv, - -->bigint - CAST(flg_eta_decision_cctv AS BIGINT) AS flg_eta_decision_cctv, - -->bigint - CAST(flg_decision_appeal_rejected_car_parks AS BIGINT) AS flg_decision_appeal_rejected_car_parks, - -->bigint - CAST(flg_decision_appeal_allowed_car_parks AS BIGINT) AS flg_decision_appeal_allowed_car_parks, - -->bigint - CAST(flg_decision_appeal_dnc_car_parks AS BIGINT) AS flg_decision_appeal_dnc_car_parks, - -->bigint - CAST( - flg_decision_appeal_with_direction_car_parks AS BIGINT - ) AS flg_decision_appeal_with_direction_car_parks, - -->bigint - CAST(flg_eta_decision_car_parks AS BIGINT) AS flg_eta_decision_car_parks, - -->bigint - CAST(flg_decision_appeal_rejected_onstreet AS BIGINT) AS flg_decision_appeal_rejected_onstreet, - -->bigint - CAST(flg_decision_appeal_allowed_onstreet AS BIGINT) AS flg_decision_appeal_allowed_onstreet, - -->bigint - CAST(flg_decision_appeal_dnc_onstreet AS BIGINT) AS flg_decision_appeal_dnc_onstreet, - -->bigint - CAST( - flg_decision_appeal_with_direction_onstreet AS BIGINT - ) AS flg_decision_appeal_with_direction_onstreet, - -->bigint - CAST(flg_eta_decision_onstreet AS BIGINT) AS flg_eta_decision_onstreet, - -->bigint - CAST(flg_decision_appeal_rejected_all_kpi AS BIGINT) AS flg_decision_appeal_rejected_all_kpi, - -->bigint - CAST(flg_decision_appeal_allowed_all_kpi AS BIGINT) AS flg_decision_appeal_allowed_all_kpi, - -->bigint - CAST(flg_decision_appeal_dnc_all_kpi AS BIGINT) AS flg_decision_appeal_dnc_all_kpi, - -->bigint - CAST( - flg_decision_appeal_with_direction_all_kpi AS BIGINT - ) AS flg_decision_appeal_with_direction_all_kpi, - -->bigint - CAST(flg_eta_decision_all_kpi AS BIGINT) AS flg_eta_decision_all_kpi, - -->bigint - CAST(case_reference AS VARCHAR) AS case_reference, - -->string - CAST("name" AS VARCHAR) AS "name", - -->string - CAST(er_pcn AS VARCHAR) AS er_pcn, - -->string - CAST(hearing_type AS VARCHAR) AS hearing_type, - -->string - CAST(case_status AS VARCHAR) AS case_status, - -->string - CAST( - decision_i_e_dnc_appeal_allowed_appeal_rejected AS VARCHAR - ) AS decision_i_e_dnc_appeal_allowed_appeal_rejected, - -->string - CAST(review_flag AS VARCHAR) AS review_flag, - -->string - CAST(reporting_period AS VARCHAR) AS reporting_period, - -->string - CAST(month_year AS VARCHAR) AS month_year, - -->string - CAST(er_import_datetime AS TIMESTAMP) AS er_import_datetime, - -->timestamp - CAST(er_import_timestamp AS VARCHAR) AS er_import_timestamp, - -->string - CAST(er_import_year AS VARCHAR) AS er_import_year, - -->string - CAST(er_import_month AS VARCHAR) AS er_import_month, - -->string - CAST(er_import_day AS VARCHAR) AS er_import_day, - -->string - CAST(er_import_date AS VARCHAR) AS er_import_date, - -->string - CAST(etar_pcn AS VARCHAR) AS etar_pcn, - -->string - CAST(appeal_rejected AS BIGINT) AS appeal_rejected, - -->bigint - CAST(appeal_allowed AS BIGINT) AS appeal_allowed, - -->bigint - CAST(appeal_dnc AS BIGINT) AS appeal_dnc, - -->bigint - CAST(appeal_with_direction AS BIGINT) AS appeal_with_direction, - -->bigint - CAST(ticketserialnumber AS VARCHAR) AS ticketserialnumber, - -->string - CAST(totalpcndisputed AS BIGINT) AS totalpcndisputed, - -->bigint - CAST(totaldisputed AS BIGINT) AS totaldisputed, - -->bigint - CAST(dispute_import_date AS VARCHAR) AS dispute_import_date, - -->string - CAST(pcn AS VARCHAR) AS pcn, - -->string - CAST(pcnissuedate AS DATE) AS pcnissuedate, - -->date - CAST(pcnissuedatetime AS TIMESTAMP) AS pcnissuedatetime, - -->timestamp - CAST(pcn_canx_date AS DATE) AS pcn_canx_date, - -->date - CAST(cancellationgroup AS VARCHAR) AS cancellationgroup, - -->string - CAST(cancellationreason AS VARCHAR) AS cancellationreason, - -->string - CAST(pcn_casecloseddate AS DATE) AS pcn_casecloseddate, - -->date - CAST(street_location AS VARCHAR) AS street_location, - -->string - CAST(whereonlocation AS VARCHAR) AS whereonlocation, - -->string - CAST("zone" AS VARCHAR) AS "zone", - -->string - CAST(usrn AS VARCHAR) AS usrn, - -->string - CAST(contraventioncode AS VARCHAR) AS contraventioncode, - -->string - CAST(contraventionsuffix AS VARCHAR) AS contraventionsuffix, - -->string - CAST(debttype AS VARCHAR) AS debttype, - -->string - CAST(vrm AS VARCHAR) AS vrm, - -->string - CAST(vehiclemake AS VARCHAR) AS vehiclemake, - -->string - CAST(vehiclemodel AS VARCHAR) AS vehiclemodel, - -->string - CAST(vehiclecolour AS VARCHAR) AS vehiclecolour, - -->string - CAST(ceo AS VARCHAR) AS ceo, - -->string - CAST(ceodevice AS VARCHAR) AS ceodevice, - -->string - CAST(current_30_day_flag AS INTEGER) AS current_30_day_flag, - -->int - CAST(isvda AS INTEGER) AS isvda, - -->int - CAST(isvoid AS INTEGER) AS isvoid, - -->int - CAST(isremoval AS VARCHAR) AS isremoval, - -->string - CAST(driverseen AS VARCHAR) AS driverseen, - -->string - CAST(allwindows AS VARCHAR) AS allwindows, - -->string - CAST(parkedonfootway AS VARCHAR) AS parkedonfootway, - -->string - CAST(doctor AS VARCHAR) AS doctor, - -->string - CAST(warningflag AS INTEGER) AS warningflag, - -->int - CAST(progressionstage AS VARCHAR) AS progressionstage, - -->string - CAST(nextprogressionstage AS VARCHAR) AS nextprogressionstage, - -->string - CAST(nextprogressionstagestarts AS VARCHAR) AS nextprogressionstagestarts, - -->string - CAST(holdreason AS VARCHAR) AS holdreason, - -->string - CAST(lib_initial_debt_amount AS VARCHAR) AS lib_initial_debt_amount, - -->string - CAST(lib_payment_received AS VARCHAR) AS lib_payment_received, - -->string - CAST(lib_write_off_amount AS VARCHAR) AS lib_write_off_amount, - -->string - CAST(lib_payment_void AS VARCHAR) AS lib_payment_void, - -->string - CAST(lib_payment_method AS VARCHAR) AS lib_payment_method, - -->string - CAST(lib_payment_ref AS VARCHAR) AS lib_payment_ref, - -->string - CAST(baliff_from AS VARCHAR) AS baliff_from, - -->string - CAST(bailiff_to AS VARCHAR) AS bailiff_to, - -->string - CAST(bailiff_processedon AS TIMESTAMP) AS bailiff_processedon, - -->timestamp - CAST(bailiff_redistributionreason AS VARCHAR) AS bailiff_redistributionreason, - -->string - CAST(bailiff AS VARCHAR) AS bailiff, - -->string - CAST(warrantissuedate AS TIMESTAMP) AS warrantissuedate, - -->timestamp - CAST(allocation AS INTEGER) AS allocation, - -->int - CAST(eta_datenotified AS TIMESTAMP) AS eta_datenotified, - -->timestamp - CAST(eta_packsubmittedon AS TIMESTAMP) AS eta_packsubmittedon, - -->timestamp - CAST(eta_evidencedate AS TIMESTAMP) AS eta_evidencedate, - -->timestamp - CAST(eta_adjudicationdate AS TIMESTAMP) AS eta_adjudicationdate, - -->timestamp - CAST(eta_appealgrounds AS VARCHAR) AS eta_appealgrounds, - -->string - CAST(eta_decisionreceived AS TIMESTAMP) AS eta_decisionreceived, - -->timestamp - CAST(eta_outcome AS VARCHAR) AS eta_outcome, - -->string - CAST(eta_packsubmittedby AS VARCHAR) AS eta_packsubmittedby, - -->string - CAST(cancelledby AS VARCHAR) AS cancelledby, - -->string - CAST(registered_keeper_address AS VARCHAR) AS registered_keeper_address, - -->string - CAST(current_ticket_address AS VARCHAR) AS current_ticket_address, - -->string - CAST(corresp_dispute_flag AS INTEGER) AS corresp_dispute_flag, - -->int - CAST(keyworker_corresp_dispute_flag AS INTEGER) AS keyworker_corresp_dispute_flag, - -->int - CAST(fin_year_flag AS VARCHAR) AS fin_year_flag, - -->string - CAST(fin_year AS VARCHAR) AS fin_year, - -->string - CAST(ticket_ref AS VARCHAR) AS ticket_ref, - -->string - CAST(nto_printed AS TIMESTAMP) AS nto_printed, - -->timestamp - CAST(appeal_accepted AS TIMESTAMP) AS appeal_accepted, - -->timestamp - CAST(arrived_in_pound AS TIMESTAMP) AS arrived_in_pound, - -->timestamp - CAST(cancellation_reversed AS TIMESTAMP) AS cancellation_reversed, - -->timestamp - CAST(cc_printed AS TIMESTAMP) AS cc_printed, - -->timestamp - CAST(drr AS TIMESTAMP) AS drr, - -->timestamp - CAST(en_printed AS TIMESTAMP) AS en_printed, - -->timestamp - CAST(hold_released AS TIMESTAMP) AS hold_released, - -->timestamp - CAST(dvla_response AS TIMESTAMP) AS dvla_response, - -->timestamp - CAST(dvla_request AS TIMESTAMP) AS dvla_request, - -->timestamp - CAST(full_rate_uplift AS TIMESTAMP) AS full_rate_uplift, - -->timestamp - CAST(hold_until AS TIMESTAMP) AS hold_until, - -->timestamp - CAST(lifted_at AS TIMESTAMP) AS lifted_at, - -->timestamp - CAST(lifted_by AS TIMESTAMP) AS lifted_by, - -->timestamp - CAST(loaded AS TIMESTAMP) AS loaded, - -->timestamp - CAST(nor_sent AS TIMESTAMP) AS nor_sent, - -->timestamp - CAST(notice_held AS TIMESTAMP) AS notice_held, - -->timestamp - CAST(ofr_printed AS TIMESTAMP) AS ofr_printed, - -->timestamp - CAST(pcn_printed AS TIMESTAMP) AS pcn_printed, - -->timestamp - CAST(reissue_nto_requested AS TIMESTAMP) AS reissue_nto_requested, - -->timestamp - CAST(reissue_pcn AS TIMESTAMP) AS reissue_pcn, - -->timestamp - CAST(set_back_to_pre_cc_stage AS TIMESTAMP) AS set_back_to_pre_cc_stage, - -->timestamp - CAST(vehicle_released_for_auction AS TIMESTAMP) AS vehicle_released_for_auction, - -->timestamp - CAST(warrant_issued AS TIMESTAMP) AS warrant_issued, - -->timestamp - CAST(warrant_redistributed AS TIMESTAMP) AS warrant_redistributed, - -->timestamp - CAST(warrant_request_granted AS TIMESTAMP) AS warrant_request_granted, - -->timestamp - CAST(ad_hoc_vq4_request AS TIMESTAMP) AS ad_hoc_vq4_request, - -->timestamp - CAST(paper_vq5_received AS TIMESTAMP) AS paper_vq5_received, - -->timestamp - CAST(pcn_extracted_for_buslane AS TIMESTAMP) AS pcn_extracted_for_buslane, - -->timestamp - CAST(pcn_extracted_for_pre_debt AS TIMESTAMP) AS pcn_extracted_for_pre_debt, - -->timestamp - CAST(pcn_extracted_for_collection AS TIMESTAMP) AS pcn_extracted_for_collection, - -->timestamp - CAST(pcn_extracted_for_drr AS TIMESTAMP) AS pcn_extracted_for_drr, - -->timestamp - CAST(pcn_extracted_for_cc AS TIMESTAMP) AS pcn_extracted_for_cc, - -->timestamp - CAST(pcn_extracted_for_nto AS TIMESTAMP) AS pcn_extracted_for_nto, - -->timestamp - CAST(pcn_extracted_for_print AS TIMESTAMP) AS pcn_extracted_for_print, - -->timestamp - CAST(warning_notice_extracted_for_print AS TIMESTAMP) AS warning_notice_extracted_for_print, - -->timestamp - CAST(pcn_extracted_for_ofr AS TIMESTAMP) AS pcn_extracted_for_ofr, - -->timestamp - CAST(pcn_extracted_for_warrant_request AS TIMESTAMP) AS pcn_extracted_for_warrant_request, - -->timestamp - CAST(pre_debt_new_debtor_details AS TIMESTAMP) AS pre_debt_new_debtor_details, - -->timestamp - CAST(importdattime AS TIMESTAMP) AS importdattime, - -->timestamp - CAST(importdatetime AS TIMESTAMP) AS importdatetime, - -->timestamp - CAST(kpi_pcns AS INTEGER) AS kpi_pcns, - -->int - CAST(kpi_pcn_dispute_eta_group AS VARCHAR) AS kpi_pcn_dispute_eta_group, - -->string - CAST(kpi_pcn_dispute_eta_flag AS VARCHAR) AS kpi_pcn_dispute_eta_flag, - -->string - CAST(kpi_pcn_not_dispute_eta_name AS VARCHAR) AS kpi_pcn_not_dispute_eta_name, - -->string - CAST(import_year AS VARCHAR) AS import_year, - -->string - CAST(import_month AS VARCHAR) AS import_month, - -->string - CAST(import_day AS VARCHAR) AS import_day, - -->string - CAST(import_date AS VARCHAR) AS import_date --, -->string -FROM transform; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary.py b/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary.py deleted file mode 100644 index 3709dcf0a..000000000 --- a/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary.py +++ /dev/null @@ -1,1106 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame -from scripts.helpers.helpers import get_glue_env_var, get_latest_partitions, PARTITION_KEYS, create_pushdown_predicate - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) -environment = get_glue_env_var("environment") - -# Script generated for node Amazon S3 - Liberator_pcn_ic -AmazonS3Liberator_pcn_ic_node1631812698045 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_ic", - transformation_ctx="AmazonS3Liberator_pcn_ic_node1631812698045", - push_down_predicate=create_pushdown_predicate("import_date",1) - - ) -) - -# Script generated for node S3 bucket - pcnfoidetails_pcn_foi_full -S3bucketpcnfoidetails_pcn_foi_full_node1 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="S3bucketpcnfoidetails_pcn_foi_full_node1", - push_down_predicate=create_pushdown_predicate("import_date",1) - - ) -) - -# Script generated for node Amazon S3 - liberator_pcn_tickets -AmazonS3liberator_pcn_tickets_node1637153316033 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="AmazonS3liberator_pcn_tickets_node1637153316033", - push_down_predicate=create_pushdown_predicate("import_date",1) - - ) -) - -# Script generated for node Amazon S3 - liberator-raw-zone - liberator_pcn_audit -AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_audit", - transformation_ctx="AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 - parking-raw-zone - parking_eta_decision_records -AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578 = glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - table_name="parking_eta_decision_records", - transformation_ctx="AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578", -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ --- PCN GDS daily summary for FOI Dashboard datediff -/* -17/11/2021 Added With regkep for field registered_keeper_address to pick up from original table liberator_pcn_tickets as from pcnfoidetails_pcn_foi_full cannot be resolved -30/11/2021 Updated/added PCN recovery by PCN type -20/01/2022 added additional field street_location comment out fields (whereonlocation, holdreason, bailiff, eta_appealgrounds, eta_outcome) increased to 51 mths from 34 mths about 98055 records -17/03/2022 updated to daily last 500 days about 97657 records, added fields eta_outcome, debt_type -07/04/2022 Convert to 400 days - added pcn totals for last 400 days - added field for pcn stacking/grouping if pcn disputed/eta or not -*/ - -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, cast(max(to_date(month_year,'MMM-yy')) as date) as etar_max_date --cast(max(to_date(month_year,'%b-%y')) as date) as etar_max_date -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction','Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) - -,regkep as(select ticketserialnumber,registered_keeper_address,current_ticket_address,import_timestamp,import_year,import_month,import_day,import_date from liberator_pcn_tickets -WHERE liberator_pcn_tickets.import_date = (SELECT max(liberator_pcn_tickets.import_date) from liberator_pcn_tickets) -) -,pcn_audit as( -SELECT distinct(audit_message),ticket_ref, liberator_pcn_audit.import_date, 1 as flag_write_off FROM liberator_pcn_audit -where liberator_pcn_audit.import_date = (SELECT max(liberator_pcn_audit.import_date) from liberator_pcn_audit) -and audit_message in ('Debt write-off request raised: 3 or more VQ5 without keeper','Debt write-off request raised: 455+ day old warrant','Debt write-off request raised: DVLA address','Debt write-off request raised: Foreign vehicle','Debt write-off request raised: No seemingly valid postcode','Debt write-off request raised: Scottish address') -order by ticket_ref desc -) -, pcn_recovery_reason as (select distinct pcn as cancel_pcn, cancellationgroup as cancellation_group, cancellationreason as cancellation_reason, pcnfoidetails_pcn_foi_full.import_date, 1 as flag_cancel_reason FROM pcnfoidetails_pcn_foi_full -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and cancellationgroup in ('Bailiff','CANCELLED - VULNERABLE DEBTOR','Debt Recovery','Pre-debt','System','Write-off')) - -, tot_pcn_loc as ( -/*Total PCNs by location last 400 days*/ -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, cast(max(to_date(month_year,'MMM-yy')) as date) as etar_max_date --cast(max(to_date(month_year,'%b-%y')) as date) as etar_max_date -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction','Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) - -select - street_location as tpl_street_location - -,min(cast(pcnissuedate as date)) as tpl_earliest_pcn -,max(cast(pcnissuedate as date)) as tpl_latest_pcn ---,datediff(min(cast(pcnissuedate as date)), max(cast(pcnissuedate as date)) ) as tpl_location_period_days -,datediff(max(cast(pcnissuedate as date)), min(cast(pcnissuedate as date)) ) as tpl_location_period_days -,count(distinct pcnissuedate) as tpl_location_days -,count(*) as tpl_PCNs_Records -,Count (pcn_canx_date) as tpl_Num_Cancelled -,Count (pcn_casecloseddate) as tpl_Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as tpl_Flag_PCN_CANCELLED - -,sum(case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is not null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 1 -else 0 end) as tpl_kpi_pcn_dispute_eta_flag - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date ---left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date ---left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date ---left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = pcnfoidetails_pcn_foi_full.pcn - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '400' day --Last 400 days from todays date -group by -street_location - -order by sum(case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is not null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 1 -else 0 end) desc -) - -select -tpl_kpi_pcn_dispute_eta_flag -,tpl_location_days -,tpl_location_period_days -,case when tpl_location_days >0 then (tpl_kpi_pcn_dispute_eta_flag / tpl_location_days) else 0 end as tot_avg_per_day -,min(tpl_earliest_pcn) as tpl_earliest_pcn -,max(tpl_latest_pcn) as tpl_latest_pcn - -,pcnissuedate --concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') AS IssueMonthYear -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) AS Dispute_kpi_MonthYear -,min(etar_max_date) as earliest_eta_date -,max(etar_max_date) as latest_eta_date -, debttype - -/* pcn kpi type name*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'onstreet' -else debttype end as kpi_pcn_type_name -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) as r_zone -, usrn -, street_location -/* LTN Camera/location list as of 15/03/2022 - https://docs.google.com/spreadsheets/d/12_QqvToXPAMMLRMUQpQtwJg9gRZR0vMs_LQghhdwiYw*/ -, (case When street_location = 'Allen Road' then 'YB1052 - Allen Road' -When street_location = 'Ashenden Road junction of Glyn Road' then 'LW2205 - Ashenden Road' -When street_location = 'Barnabas Road JCT Berger Road' then 'LW2204 - Barnabas Road' -When street_location = 'Barnabas Road JCT Oriel Road' then 'LW2204 - Barnabas Road' -When street_location = 'Benthal Road' then 'LW2206 - Benthal Road' -When street_location = 'Bouverie Road junction of Stoke Newington Church Street' then 'LW2593 - Bouverie Road' -When street_location = 'Clissold Crescent' then 'LW2397 - Clissold Crescent' -When street_location = 'Cremer Street' then 'LW2041 - Cremer Street' -When street_location = 'Downs Road' then 'LW2389 - Downs Road' -When street_location = 'Elsdale Street' then 'LW2393 - Elsdale Street' -When street_location = 'Gore Road junction of Lauriston Road.' then 'LW2078 - Gore Road' -When street_location = 'Hyde Road' then 'LW2208 - Hyde Road' -When street_location = 'Hyde Road JCT Northport Street' then 'LW2208 - Hyde Road' -When street_location = 'Lee Street junction of Stean Street' then 'LW1535 - Lee Street' -When street_location = 'Loddiges Road jct Frampton Park Road' then 'LW1051 - Loddiges Road' -When street_location = 'Lordship Road junction of Lordship Terrace' then 'LW2590 - Lordship Road' -When street_location = 'Maury Road junction of Evering Road' then 'LW2390 - Maury Road' -When street_location = 'Mead Place' then 'LW2395 - Mead Place' -When street_location = 'Meeson Street junction of Kingsmead Way' then 'LW2079 - Meeson Street' -When street_location = 'Mount Pleasant Lane' then 'LW2391 - Mount Pleasant Lane' -When street_location = 'Nevill Road junction of Barbauld Road' then 'LW2595 - Nevill Road/ Barbauld Road' -When street_location = 'Nevill Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Neville Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Oldfield Road (E)' then 'LW2596 - Oldfield Road' -When street_location = 'Oldfield Road junction of Kynaston Road' then 'LW2596 - Oldfield Road' -When street_location = 'Pitfield Street (F)' then 'LW2207 - Pitfield Street' -When street_location = 'Pitfield Street JCT Hemsworth Street' then 'LW2207 - Pitfield Street' -When street_location = 'Powell Road junction of Kenninghall Road' then 'LW1691 - Powell Road (Kenninghall Road)' -When street_location = 'Shepherdess Walk' then 'LW2076 - Shepherdess Walk' -When street_location = 'Shore Place' then 'Mobile camera car - Shore Place' -When street_location = 'Stoke Newington Church Street junction of Lordship Road - Eastbound' then 'LW2591 - Stoke Newington Church Street eastbound' -When street_location = 'Stoke Newington Church Street junction of Marton Road - Westbound' then 'LW2592 - Stoke Newington Church Street westbound' -When street_location = 'Ufton Road junction of Downham Road' then 'LW2077 - Ufton Road' -When street_location = 'Wayland Avenue' then 'LW2392 - Wayland Avenue' -When street_location = 'Weymouth Terrace junction of Dunloe Street' then 'Mobile camera car - Weymouth Terrace' -When street_location = 'Wilton Way junction of Greenwood Road' then 'LW1457 - Wilton Way' -When street_location = 'Woodberry Grove junction of Rowley Gardens' then 'LW1457 - Woodberry Grove' -When street_location = 'Woodberry Grove junction of Seven Sisters Road' then 'LW1457 - Woodberry Grove' -When street_location = 'Yoakley Road junction of Stoke Newington Church Street' then 'LW2594 - Yoakley Road' else 'NOT current LTN Camera Location' end) as ltn_camera_location --- , whereonlocation -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds -, eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date as importdate -- import_date -, concat(pcnfoidetails_pcn_foi_full.import_year,pcnfoidetails_pcn_foi_full.import_month,pcnfoidetails_pcn_foi_full.import_day) as import_date ---, current_timestamp() as ImportDateTime - -,sum(case when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 then 1 else 0 end) as kpi_pcns -,count(*) as PCNs_Records -,Count (pcn_canx_date) as Num_Cancelled -,Count (pcn_casecloseddate) as Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as Flag_PCN_CANCELLED -,count(regkep.registered_keeper_address) as num_reg_keep -,count(regkep.current_ticket_address) as num_curr_add - --- Flag registered_keeper_address or current address registered_keeper_address -,Sum(Case When regkep.registered_keeper_address != '' or regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_address -,Sum(Case When regkep.registered_keeper_address = '' or regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_address_null -,Sum(Case When regkep.registered_keeper_address != '' Then 1 Else 0 End) as Flag_reg_keeper_address -,Sum(Case When regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_current_address -,Sum(Case When regkep.registered_keeper_address = '' Then 1 Else 0 End) as Flag_reg_keeper_address_null -,Sum(Case When regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_current_address_null - --- CEO error All -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error_pcn - - --- ceo error flag by pcn type -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error - -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error - - --- CEO error flag Without VOID VDA or warningflag -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_errorWO -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_errorWO -,Sum(Case When ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_errorWO -,Sum(Case When ((zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_errorWO -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_errorWO - --- ceo error flag by pcn types with VDA -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_vda -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_vda -,Sum(Case When ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_vda -,Sum(Case When ((zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_vda -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_vda - --- PCNs kpi by pcn types for CEO Error -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error_pcn - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_pcn -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_pcn -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_pcn -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error_pcn - -,Sum(CASE When Lib_Payment_Received != '0' Then 1 Else 0 END) as Flag_PCN_PAYMENT -,Count(distinct pcn) as PCNs -,CAST(SUM(cast(lib_initial_debt_amount as double)) as decimal(11,2)) as Total_lib_initial_debt_amount -,CAST(SUM(cast(lib_payment_received as double)) as decimal(11,2)) as Total_lib_payment_received -,CAST(SUM(cast(lib_write_off_amount as double)) as decimal(11,2)) as Total_lib_write_off_amount -,CAST(SUM(cast(lib_payment_void as double)) as decimal(11,2)) as Total_lib_payment_void - --- KPI PCNs Paid by PCN type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Estates_paid -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_CCTV_paid - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_paid - --- Paid with address by pcn type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid_address - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Estates_paid_address -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_CCTV_paid_address - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid_address -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_paid_address - --- PCN Recovery -/*PCN Recovered All*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered - -,sum(case - when ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered_can - -/*PCN Recovered by Type - CCTV*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_cctv - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_can_cctv - -/*PCN Recovered by Type - Estates*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_estates - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_can_estates - -/*PCN Recovered by Type - Carparks*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_can_carparks - - -/*PCN Recovered by Type - onstreet*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_onstreet - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_can_onstreet - - -/*PCN Recovered by Type - carparks and onstreet*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_onstreet_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_can_onstreet_carparks - -/*KPI PCNs by Type with VDA's excluded before and included after 1st June 2021*/ - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet_carparks - -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Estates - -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_CCTV - -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Car_Parks - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet - - - --- Disputed pcns and by pcn type -,count(distinct Disputes.ticketserialnumber) as TotalpcnDisputed -,count(Disputes.ticketserialnumber) as TotalDisputed - -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flg_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_Estates_disputes -,COUNT(DISTINCT(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_CCTV_disputes - -,COUNT(DISTINCT(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_Car_Parks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_onstreet_disputes -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flg_kpi_onstreet_disputes - -/*ETA records with by PCN Type*/ -,sum(eta_recs.appeal_rejected) as decision_appeal_rejected -,sum(eta_recs.appeal_allowed) as decision_appeal_allowed -,sum(eta_recs.appeal_dnc) as decision_appeal_dnc -,sum(eta_recs.appeal_with_direction) as decision_appeal_with_direction - - -/*onstreet_carparks ETA Decisions*/ -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_onstreet_carparks - -/*Estates ETA Decisions*/ -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_Estates - - -/*CCTV ETA Decisions*/ -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_CCTV - -/*Car_Parks ETA Decisions*/ -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_Car_Parks - - -/*onstreet ETA Decisions*/ -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_onstreet - -/* All kpi ETA Decisions*/ -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_all_kpi - - - -/*PCNs catergorised not eta or disputed for stacking*/ -,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Not dispute or eta' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'disputed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'eta rejected' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta rejected' -else 'NOT KPI' end as kpi_pcn_dispute_eta_group - -,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'Y' -else 'N' end as kpi_pcn_dispute_eta_flag - -/*PCNs not eta or disputed*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'onstreet' -/*disputed by kpi pcn type*/ -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Estates - disputed_eta' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'CCTV - disputed_eta' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Car_Parks - disputed_eta' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'onstreet - disputed_eta' -else 'NOT KPI' end as kpi_pcn_not_dispute_eta_name - - ---PCN Calculation include Where code warningflag = 0 and isvda = 0 and isvoid = 0. No Warnings NO VDA and No voided PCNs ---) and (cast(isvda as varchar) like '0') and (cast(isvoid as varchar) like '0') and (cast(warningflag as varchar) like '0') ---and (isvda = 0) and (isvoid = 0) and (warningflag = 0) - --- PCNs kpi by pcn types -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Estates -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_CCTV - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Car_Parks - - -,Sum(Case When (isvda = 1) or (isvoid = 1) or (warningflag = 1) Then 1 Else 0 End) as Flag_total_vda_void_warning - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) Then 1 Else 0 End) as Flag_pi_Estates -,Sum(Case When zone like 'Car Parks' Then 1 Else 0 End) as Flag_pi_Car_Parks -,Sum(Case When debttype like 'CCTV%' Then 1 Else 0 End) as Flag_pi_CCTV -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' Then 1 Else 0 End) as Flag_pi_onstreet -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' Then 1 Else 0 End) as Flag_pi_onstreet_carparks - -,Sum(Case When debttype like 'CEO' Then 1 Else 0 End) as Flag_debttype_CEO -,Sum(Case When debttype like 'CCTV Moving traffic' Then 1 Else 0 End) as Flag_debttype_CCTV_Moving_traffic -,Sum(Case When debttype like 'CCTV static' Then 1 Else 0 End) as Flag_debttype_CCTV_static -,Sum(Case When debttype like 'CCTV Bus Lane' Then 1 Else 0 End) as Flag_debttype_CCTV_Bus_Lane -,Sum(Case When debttype like 'Manual_Tickets' Then 1 Else 0 End) as Flag_debttype_Manual_Tickets - --- pcns not in kpi -,sum(case when cast(pcnissuedate as date) > cast('2021-08-09' as date) then 1 else 0 end) as af_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-08-10' as date) then 1 else 0 end) as bf_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) > cast('2021-05-31' as date) then 1 else 0 end) as af_first_jun_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-06-01' as date) then 1 else 0 end) as bf_first_jun_PCN_issue - -,Sum(Case When isvda = 1 Then 1 Else 0 End) as Flag_total_isvda -,Sum(Case When isvoid = 1 Then 1 Else 0 End) as Flag_total_isvoid -,Sum(Case When cast(isremoval as INTEGER) = 1 Then 1 Else 0 End) as Flag_total_isremoval -,Sum(Case When warningflag = 1 Then 1 Else 0 End) as Flag_total_warningflag - -,Sum(Case When progressionstage like 'discount' Then 1 Else 0 End) as Flag_progressionstage_discount -,Sum(Case When progressionstage like 'postalgrace' Then 1 Else 0 End) as Flag_progressionstage_postalgrace -,Sum(Case When progressionstage like 'waitdvla' Then 1 Else 0 End) as Flag_progressionstage_waitdvla -,Sum(Case When progressionstage like 'readytoprint' Then 1 Else 0 End) as Flag_progressionstage_readytoprint -,Sum(Case When progressionstage like 'nto' Then 1 Else 0 End) as Flag_progressionstage_nto -,Sum(Case When progressionstage like 'full' Then 1 Else 0 End) as Flag_progressionstage_full -,Sum(Case When progressionstage like 'cc' Then 1 Else 0 End) as Flag_progressionstage_cc -,Sum(Case When progressionstage like 'nfa' Then 1 Else 0 End) as Flag_progressionstage_nfa -,Sum(Case When progressionstage like 'warningnoticesent' Then 1 Else 0 End) as Flag_progressionstage_warningnoticesent -,Sum(Case When progressionstage like 'en' Then 1 Else 0 End) as Flag_progressionstage_en -,Sum(Case When progressionstage like 'foreigncollection' Then 1 Else 0 End) as Flag_progressionstage_foreigncollection -,Sum(Case When progressionstage like 'nodr' Then 1 Else 0 End) as Flag_progressionstage_nodr -,Sum(Case When progressionstage like 'predebt' Then 1 Else 0 End) as Flag_progressionstage_predebt -,Sum(Case When progressionstage like 'nodrr' Then 1 Else 0 End) as Flag_progressionstage_nodrr -,Sum(Case When progressionstage like 'warrant' Then 1 Else 0 End) as Flag_progressionstage_warrant -,Sum(Case When progressionstage like 'warr' Then 1 Else 0 End) as Flag_progressionstage_warr -,Sum(Case When progressionstage like 'pre-debt' Then 1 Else 0 End) as Flag_progressionstage_pre_debt - - -,Sum(Case When nto_printed is not null Then 1 Else 0 End) as Flag_nto_printed -,Sum(Case When appeal_accepted is not null Then 1 Else 0 End) as Flag_appeal_accepted -,Sum(Case When arrived_in_pound is not null Then 1 Else 0 End) as Flag_arrived_in_pound -,Sum(Case When cancellation_reversed is not null Then 1 Else 0 End) as Flag_cancellation_reversed -,Sum(Case When cc_printed is not null Then 1 Else 0 End) as Flag_cc_printed -,Sum(Case When drr is not null Then 1 Else 0 End) as Flag_drr -,Sum(Case When en_printed is not null Then 1 Else 0 End) as Flag_en_printed -,Sum(Case When hold_released is not null Then 1 Else 0 End) as Flag_hold_released -,Sum(Case When dvla_response is not null Then 1 Else 0 End) as Flag_dvla_response -,Sum(Case When dvla_request is not null Then 1 Else 0 End) as Flag_dvla_request -,Sum(Case When full_rate_uplift is not null Then 1 Else 0 End) as Flag_full_rate_uplift -,Sum(Case When hold_until is not null Then 1 Else 0 End) as Flag_hold_until -,Sum(Case When lifted_at is not null Then 1 Else 0 End) as Flag_lifted_at -,Sum(Case When lifted_by is not null Then 1 Else 0 End) as Flag_lifted_by -,Sum(Case When loaded is not null Then 1 Else 0 End) as Flag_loaded -,Sum(Case When nor_sent is not null Then 1 Else 0 End) as Flag_nor_sent -,Sum(Case When notice_held is not null Then 1 Else 0 End) as Flag_notice_held -,Sum(Case When ofr_printed is not null Then 1 Else 0 End) as Flag_ofr_printed -,Sum(Case When pcn_printed is not null Then 1 Else 0 End) as Flag_pcn_printed -,Sum(Case When reissue_nto_requested is not null Then 1 Else 0 End) as Flag_reissue_nto_requested -,Sum(Case When reissue_pcn is not null Then 1 Else 0 End) as Flag_reissue_pcn -,Sum(Case When set_back_to_pre_cc_stage is not null Then 1 Else 0 End) as Flag_set_back_to_pre_cc_stage -,Sum(Case When vehicle_released_for_auction is not null Then 1 Else 0 End) as Flag_vehicle_released_for_auction -,Sum(Case When warrant_issued is not null Then 1 Else 0 End) as Flag_warrant_issued -,Sum(Case When warrant_redistributed is not null Then 1 Else 0 End) as Flag_warrant_redistributed -,Sum(Case When warrant_request_granted is not null Then 1 Else 0 End) as Flag_warrant_request_granted -,Sum(Case When ad_hoc_vq4_request is not null Then 1 Else 0 End) as Flag_ad_hoc_vq4_request -,Sum(Case When paper_vq5_received is not null Then 1 Else 0 End) as Flag_paper_vq5_received -,Sum(Case When pcn_extracted_for_buslane is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_buslane -,Sum(Case When pcn_extracted_for_pre_debt is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_pre_debt -,Sum(Case When pcn_extracted_for_collection is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_collection -,Sum(Case When pcn_extracted_for_drr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_drr -,Sum(Case When pcn_extracted_for_cc is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_cc -,Sum(Case When pcn_extracted_for_nto is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_nto -,Sum(Case When pcn_extracted_for_print is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_print -,Sum(Case When warning_notice_extracted_for_print is not null Then 1 Else 0 End) as Flag_warning_notice_extracted_for_print -,Sum(Case When pcn_extracted_for_ofr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_ofr -,Sum(Case When pcn_extracted_for_warrant_request is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_warrant_request -,Sum(Case When pre_debt_new_debtor_details is not null Then 1 Else 0 End) as Flag_pre_debt_new_debtor_details - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date -left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date -left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date -left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = pcnfoidetails_pcn_foi_full.pcn -left join tot_pcn_loc on tot_pcn_loc.tpl_street_location = pcnfoidetails_pcn_foi_full.street_location -/*from er -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = substr(er.pcn, 1, 10) -left join Disputes on Disputes.ticketserialnumber = substr(er.pcn, 1, 10) -left join pcn on substr(er.pcn, 1, 10) = pcn.pcn*/ - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '400' day --Last 400 days from todays date -group by pcnissuedate --,concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) -,tpl_kpi_pcn_dispute_eta_flag, tpl_location_period_days ,tpl_location_days ,case when tpl_location_days >0 then (tpl_kpi_pcn_dispute_eta_flag / tpl_location_days) else 0 end ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') ---,cast(Substr(pcnissuedate, 1,7) as date) -, debttype -, zone -/* pcn kpi type name*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'onstreet' -else debttype end -, (Case When zone like 'Estates' Then usrn Else zone End) -, usrn -, street_location --- , whereonlocation -,(case When street_location = 'Allen Road' then 'YB1052 - Allen Road' -When street_location = 'Ashenden Road junction of Glyn Road' then 'LW2205 - Ashenden Road' -When street_location = 'Barnabas Road JCT Berger Road' then 'LW2204 - Barnabas Road' -When street_location = 'Barnabas Road JCT Oriel Road' then 'LW2204 - Barnabas Road' -When street_location = 'Benthal Road' then 'LW2206 - Benthal Road' -When street_location = 'Bouverie Road junction of Stoke Newington Church Street' then 'LW2593 - Bouverie Road' -When street_location = 'Clissold Crescent' then 'LW2397 - Clissold Crescent' -When street_location = 'Cremer Street' then 'LW2041 - Cremer Street' -When street_location = 'Downs Road' then 'LW2389 - Downs Road' -When street_location = 'Elsdale Street' then 'LW2393 - Elsdale Street' -When street_location = 'Gore Road junction of Lauriston Road.' then 'LW2078 - Gore Road' -When street_location = 'Hyde Road' then 'LW2208 - Hyde Road' -When street_location = 'Hyde Road JCT Northport Street' then 'LW2208 - Hyde Road' -When street_location = 'Lee Street junction of Stean Street' then 'LW1535 - Lee Street' -When street_location = 'Loddiges Road jct Frampton Park Road' then 'LW1051 - Loddiges Road' -When street_location = 'Lordship Road junction of Lordship Terrace' then 'LW2590 - Lordship Road' -When street_location = 'Maury Road junction of Evering Road' then 'LW2390 - Maury Road' -When street_location = 'Mead Place' then 'LW2395 - Mead Place' -When street_location = 'Meeson Street junction of Kingsmead Way' then 'LW2079 - Meeson Street' -When street_location = 'Mount Pleasant Lane' then 'LW2391 - Mount Pleasant Lane' -When street_location = 'Nevill Road junction of Barbauld Road' then 'LW2595 - Nevill Road/ Barbauld Road' -When street_location = 'Nevill Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Neville Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Oldfield Road (E)' then 'LW2596 - Oldfield Road' -When street_location = 'Oldfield Road junction of Kynaston Road' then 'LW2596 - Oldfield Road' -When street_location = 'Pitfield Street (F)' then 'LW2207 - Pitfield Street' -When street_location = 'Pitfield Street JCT Hemsworth Street' then 'LW2207 - Pitfield Street' -When street_location = 'Powell Road junction of Kenninghall Road' then 'LW1691 - Powell Road (Kenninghall Road)' -When street_location = 'Shepherdess Walk' then 'LW2076 - Shepherdess Walk' -When street_location = 'Shore Place' then 'Mobile camera car - Shore Place' -When street_location = 'Stoke Newington Church Street junction of Lordship Road - Eastbound' then 'LW2591 - Stoke Newington Church Street eastbound' -When street_location = 'Stoke Newington Church Street junction of Marton Road - Westbound' then 'LW2592 - Stoke Newington Church Street westbound' -When street_location = 'Ufton Road junction of Downham Road' then 'LW2077 - Ufton Road' -When street_location = 'Wayland Avenue' then 'LW2392 - Wayland Avenue' -When street_location = 'Weymouth Terrace junction of Dunloe Street' then 'Mobile camera car - Weymouth Terrace' -When street_location = 'Wilton Way junction of Greenwood Road' then 'LW1457 - Wilton Way' -When street_location = 'Woodberry Grove junction of Rowley Gardens' then 'LW1457 - Woodberry Grove' -When street_location = 'Woodberry Grove junction of Seven Sisters Road' then 'LW1457 - Woodberry Grove' -When street_location = 'Yoakley Road junction of Stoke Newington Church Street' then 'LW2594 - Yoakley Road' else 'NOT current LTN Camera Location' end) -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds -, eta_outcome -/*flag - PCNs catergorised not eta or disputed for stacking*/,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'Y' -else 'N' end -/*PCNs catergorised not eta or disputed for stacking*/,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Not dispute or eta' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'disputed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'eta rejected' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta rejected' -else 'NOT KPI' end -/*PCNs not eta or disputed*/,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'onstreet' -/*disputed by kpi pcn type*/ -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Estates - disputed_eta' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'CCTV - disputed_eta' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Car_Parks - disputed_eta' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'onstreet - disputed_eta' -else 'NOT KPI' end - -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date -- import_date ---, current_timestamp() as ImportDateTime -order by pcnissuedate --concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') desc -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_ic": AmazonS3Liberator_pcn_ic_node1631812698045, - "pcnfoidetails_pcn_foi_full": S3bucketpcnfoidetails_pcn_foi_full_node1, - "liberator_pcn_tickets": AmazonS3liberator_pcn_tickets_node1637153316033, - "liberator_pcn_audit": AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740, - "eta_decision_records": AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-"+environment+"-refined-zone/parking/liberator/parking_foi_pcn_gds_daily_summary/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-"+environment+"-liberator-refined-zone", - catalogTableName="parking_foi_pcn_gds_daily_summary", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary_records.py b/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary_records.py deleted file mode 100644 index f196e4c78..000000000 --- a/scripts/jobs/parking/parking_foi_pcn_gds_daily_summary_records.py +++ /dev/null @@ -1,1116 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame -from scripts.helpers.helpers import get_glue_env_var, get_latest_partitions, PARTITION_KEYS, create_pushdown_predicate - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) -environment = get_glue_env_var("environment") - -# Script generated for node Amazon S3 - Liberator_pcn_ic -AmazonS3Liberator_pcn_ic_node1631812698045 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_ic", - transformation_ctx="AmazonS3Liberator_pcn_ic_node1631812698045", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node S3 bucket - pcnfoidetails_pcn_foi_full -S3bucketpcnfoidetails_pcn_foi_full_node1 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="S3bucketpcnfoidetails_pcn_foi_full_node1", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator_pcn_tickets -AmazonS3liberator_pcn_tickets_node1637153316033 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="AmazonS3liberator_pcn_tickets_node1637153316033", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator-raw-zone - liberator_pcn_audit -AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-"+environment+"-liberator-raw-zone", - table_name="liberator_pcn_audit", - transformation_ctx="AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 - parking-raw-zone - parking_eta_decision_records -AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578 = glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - table_name="parking_eta_decision_records", - transformation_ctx="AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578", -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ --- PCN GDS daily summary for FOI Dashboard datediff -/* -17/11/2021 Added With regkep for field registered_keeper_address to pick up from original table liberator_pcn_tickets as from pcnfoidetails_pcn_foi_full cannot be resolved -30/11/2021 Updated/added PCN recovery by PCN type -20/01/2022 added additional field street_location comment out fields (whereonlocation, holdreason, bailiff, eta_appealgrounds, eta_outcome) increased to 51 mths from 34 mths about 98055 records -17/03/2022 updated to daily last 500 days about 97657 records, added fields eta_outcome, debt_type -07/04/2022 Convert to 400 days - added pcn totals for last 400 days - added field for pcn stacking/grouping if pcn disputed/eta or not -*/ - -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, cast(max(to_date(month_year,'MMM-yy')) as date) as etar_max_date --cast(max(to_date(month_year,'%b-%y')) as date) as etar_max_date -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction','Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) - -,regkep as(select ticketserialnumber,registered_keeper_address,current_ticket_address,import_timestamp,import_year,import_month,import_day,import_date from liberator_pcn_tickets -WHERE liberator_pcn_tickets.import_date = (SELECT max(liberator_pcn_tickets.import_date) from liberator_pcn_tickets) -) -,pcn_audit as( -SELECT distinct(audit_message),ticket_ref, liberator_pcn_audit.import_date, 1 as flag_write_off FROM liberator_pcn_audit -where liberator_pcn_audit.import_date = (SELECT max(liberator_pcn_audit.import_date) from liberator_pcn_audit) -and audit_message in ('Debt write-off request raised: 3 or more VQ5 without keeper','Debt write-off request raised: 455+ day old warrant','Debt write-off request raised: DVLA address','Debt write-off request raised: Foreign vehicle','Debt write-off request raised: No seemingly valid postcode','Debt write-off request raised: Scottish address') -order by ticket_ref desc -) -, pcn_recovery_reason as (select distinct pcn as cancel_pcn, cancellationgroup as cancellation_group, cancellationreason as cancellation_reason, pcnfoidetails_pcn_foi_full.import_date, 1 as flag_cancel_reason FROM pcnfoidetails_pcn_foi_full -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and cancellationgroup in ('Bailiff','CANCELLED - VULNERABLE DEBTOR','Debt Recovery','Pre-debt','System','Write-off')) - -, tot_pcn_loc as ( -/*Total PCNs by location last 400 days*/ -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, cast(max(to_date(month_year,'MMM-yy')) as date) as etar_max_date --cast(max(to_date(month_year,'%b-%y')) as date) as etar_max_date -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction','Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) - -select - street_location as tpl_street_location - -,min(cast(pcnissuedate as date)) as tpl_earliest_pcn -,max(cast(pcnissuedate as date)) as tpl_latest_pcn ---,datediff(min(cast(pcnissuedate as date)), max(cast(pcnissuedate as date)) ) as tpl_location_period_days -,datediff(max(cast(pcnissuedate as date)), min(cast(pcnissuedate as date)) ) as tpl_location_period_days -,count(distinct pcnissuedate) as tpl_location_days -,count(*) as tpl_PCNs_Records -,Count (pcn_canx_date) as tpl_Num_Cancelled -,Count (pcn_casecloseddate) as tpl_Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as tpl_Flag_PCN_CANCELLED - -,sum(case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is not null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 1 -else 0 end) as tpl_kpi_pcn_dispute_eta_flag - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date ---left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date ---left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date ---left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = pcnfoidetails_pcn_foi_full.pcn - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '400' day --Last 400 days from todays date -group by -street_location - -order by sum(case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 or eta_recs.appeal_allowed = 0 or eta_recs.appeal_dnc = 0 or eta_recs.appeal_with_direction = 0) or (Disputes.ticketserialnumber is not null)) then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 1 -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 1 -else 0 end) desc -) - -select -tpl_kpi_pcn_dispute_eta_flag -,tpl_location_days -,tpl_location_period_days -,case when tpl_location_days >0 then (tpl_kpi_pcn_dispute_eta_flag / tpl_location_days) else 0 end as tot_avg_per_day -,min(tpl_earliest_pcn) as tpl_earliest_pcn -,max(tpl_latest_pcn) as tpl_latest_pcn - ---added to get pcn records rather than summary --for use in major cctv dashboards -,pcnfoidetails_pcn_foi_full.pcn -,cancellationgroup -,cancellationreason ---below is as per original summary script - -,pcnissuedate --concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') AS IssueMonthYear -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) AS Dispute_kpi_MonthYear -,min(etar_max_date) as earliest_eta_date -,max(etar_max_date) as latest_eta_date -, debttype - -/* pcn kpi type name*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'onstreet' -else debttype end as kpi_pcn_type_name -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) as r_zone -, usrn -, street_location -/* LTN Camera/location list as of 15/03/2022 - https://docs.google.com/spreadsheets/d/12_QqvToXPAMMLRMUQpQtwJg9gRZR0vMs_LQghhdwiYw*/ -, (case When street_location = 'Allen Road' then 'YB1052 - Allen Road' -When street_location = 'Ashenden Road junction of Glyn Road' then 'LW2205 - Ashenden Road' -When street_location = 'Barnabas Road JCT Berger Road' then 'LW2204 - Barnabas Road' -When street_location = 'Barnabas Road JCT Oriel Road' then 'LW2204 - Barnabas Road' -When street_location = 'Benthal Road' then 'LW2206 - Benthal Road' -When street_location = 'Bouverie Road junction of Stoke Newington Church Street' then 'LW2593 - Bouverie Road' -When street_location = 'Clissold Crescent' then 'LW2397 - Clissold Crescent' -When street_location = 'Cremer Street' then 'LW2041 - Cremer Street' -When street_location = 'Downs Road' then 'LW2389 - Downs Road' -When street_location = 'Elsdale Street' then 'LW2393 - Elsdale Street' -When street_location = 'Gore Road junction of Lauriston Road.' then 'LW2078 - Gore Road' -When street_location = 'Hyde Road' then 'LW2208 - Hyde Road' -When street_location = 'Hyde Road JCT Northport Street' then 'LW2208 - Hyde Road' -When street_location = 'Lee Street junction of Stean Street' then 'LW1535 - Lee Street' -When street_location = 'Loddiges Road jct Frampton Park Road' then 'LW1051 - Loddiges Road' -When street_location = 'Lordship Road junction of Lordship Terrace' then 'LW2590 - Lordship Road' -When street_location = 'Maury Road junction of Evering Road' then 'LW2390 - Maury Road' -When street_location = 'Mead Place' then 'LW2395 - Mead Place' -When street_location = 'Meeson Street junction of Kingsmead Way' then 'LW2079 - Meeson Street' -When street_location = 'Mount Pleasant Lane' then 'LW2391 - Mount Pleasant Lane' -When street_location = 'Nevill Road junction of Barbauld Road' then 'LW2595 - Nevill Road/ Barbauld Road' -When street_location = 'Nevill Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Neville Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Oldfield Road (E)' then 'LW2596 - Oldfield Road' -When street_location = 'Oldfield Road junction of Kynaston Road' then 'LW2596 - Oldfield Road' -When street_location = 'Pitfield Street (F)' then 'LW2207 - Pitfield Street' -When street_location = 'Pitfield Street JCT Hemsworth Street' then 'LW2207 - Pitfield Street' -When street_location = 'Powell Road junction of Kenninghall Road' then 'LW1691 - Powell Road (Kenninghall Road)' -When street_location = 'Shepherdess Walk' then 'LW2076 - Shepherdess Walk' -When street_location = 'Shore Place' then 'Mobile camera car - Shore Place' -When street_location = 'Stoke Newington Church Street junction of Lordship Road - Eastbound' then 'LW2591 - Stoke Newington Church Street eastbound' -When street_location = 'Stoke Newington Church Street junction of Marton Road - Westbound' then 'LW2592 - Stoke Newington Church Street westbound' -When street_location = 'Ufton Road junction of Downham Road' then 'LW2077 - Ufton Road' -When street_location = 'Wayland Avenue' then 'LW2392 - Wayland Avenue' -When street_location = 'Weymouth Terrace junction of Dunloe Street' then 'Mobile camera car - Weymouth Terrace' -When street_location = 'Wilton Way junction of Greenwood Road' then 'LW1457 - Wilton Way' -When street_location = 'Woodberry Grove junction of Rowley Gardens' then 'LW1457 - Woodberry Grove' -When street_location = 'Woodberry Grove junction of Seven Sisters Road' then 'LW1457 - Woodberry Grove' -When street_location = 'Yoakley Road junction of Stoke Newington Church Street' then 'LW2594 - Yoakley Road' else 'NOT current LTN Camera Location' end) as ltn_camera_location --- , whereonlocation -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds -, eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date as importdate -- import_date -, concat(pcnfoidetails_pcn_foi_full.import_year,pcnfoidetails_pcn_foi_full.import_month,pcnfoidetails_pcn_foi_full.import_day) as import_date ---, current_timestamp() as ImportDateTime - -,sum(case when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 then 1 else 0 end) as kpi_pcns -,count(*) as PCNs_Records -,Count (pcn_canx_date) as Num_Cancelled -,Count (pcn_casecloseddate) as Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as Flag_PCN_CANCELLED -,count(regkep.registered_keeper_address) as num_reg_keep -,count(regkep.current_ticket_address) as num_curr_add - --- Flag registered_keeper_address or current address registered_keeper_address -,Sum(Case When regkep.registered_keeper_address != '' or regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_address -,Sum(Case When regkep.registered_keeper_address = '' or regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_address_null -,Sum(Case When regkep.registered_keeper_address != '' Then 1 Else 0 End) as Flag_reg_keeper_address -,Sum(Case When regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_current_address -,Sum(Case When regkep.registered_keeper_address = '' Then 1 Else 0 End) as Flag_reg_keeper_address_null -,Sum(Case When regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_current_address_null - --- CEO error All -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error_pcn - - --- ceo error flag by pcn type -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error - -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error - - --- CEO error flag Without VOID VDA or warningflag -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_errorWO -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_errorWO -,Sum(Case When ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_errorWO -,Sum(Case When ((zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_errorWO -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_errorWO - --- ceo error flag by pcn types with VDA -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_vda -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_vda -,Sum(Case When ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_vda -,Sum(Case When ((zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_vda -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_vda - --- PCNs kpi by pcn types for CEO Error -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error_pcn - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_pcn -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_pcn -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_pcn -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error_pcn - -,Sum(CASE When Lib_Payment_Received != '0' Then 1 Else 0 END) as Flag_PCN_PAYMENT -,Count(distinct pcn) as PCNs -,CAST(SUM(cast(lib_initial_debt_amount as double)) as decimal(11,2)) as Total_lib_initial_debt_amount -,CAST(SUM(cast(lib_payment_received as double)) as decimal(11,2)) as Total_lib_payment_received -,CAST(SUM(cast(lib_write_off_amount as double)) as decimal(11,2)) as Total_lib_write_off_amount -,CAST(SUM(cast(lib_payment_void as double)) as decimal(11,2)) as Total_lib_payment_void - --- KPI PCNs Paid by PCN type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Estates_paid -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_CCTV_paid - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_paid - --- Paid with address by pcn type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid_address - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Estates_paid_address -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_CCTV_paid_address - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid_address -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_paid_address - --- PCN Recovery -/*PCN Recovered All*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered - -,sum(case - when ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered_can - -/*PCN Recovered by Type - CCTV*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_cctv - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_can_cctv - -/*PCN Recovered by Type - Estates*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_estates - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_can_estates - -/*PCN Recovered by Type - Carparks*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_can_carparks - - -/*PCN Recovered by Type - onstreet*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_onstreet - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_can_onstreet - - -/*PCN Recovered by Type - carparks and onstreet*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_onstreet_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_can_onstreet_carparks - -/*KPI PCNs by Type with VDA's excluded before and included after 1st June 2021*/ - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet_carparks - -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Estates - -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_CCTV - -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Car_Parks - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet - - - --- Disputed pcns and by pcn type -,count(distinct Disputes.ticketserialnumber) as TotalpcnDisputed -,count(Disputes.ticketserialnumber) as TotalDisputed - -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flg_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_Estates_disputes -,COUNT(DISTINCT(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_CCTV_disputes - -,COUNT(DISTINCT(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_Car_Parks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_onstreet_disputes -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flg_kpi_onstreet_disputes - -/*ETA records with by PCN Type*/ -,sum(eta_recs.appeal_rejected) as decision_appeal_rejected -,sum(eta_recs.appeal_allowed) as decision_appeal_allowed -,sum(eta_recs.appeal_dnc) as decision_appeal_dnc -,sum(eta_recs.appeal_with_direction) as decision_appeal_with_direction - - -/*onstreet_carparks ETA Decisions*/ -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_onstreet_carparks -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_onstreet_carparks - -/*Estates ETA Decisions*/ -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_Estates -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_Estates - - -/*CCTV ETA Decisions*/ -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_CCTV - -/*Car_Parks ETA Decisions*/ -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_Car_Parks -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_Car_Parks - - -/*onstreet ETA Decisions*/ -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_onstreet -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_onstreet - -/* All kpi ETA Decisions*/ -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_all_kpi -,Sum(Case - When - ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_all_kpi - - - -/*PCNs catergorised not eta or disputed for stacking*/ -,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Not dispute or eta' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'disputed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'eta rejected' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta rejected' -else 'NOT KPI' end as kpi_pcn_dispute_eta_group - -,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'Y' -else 'N' end as kpi_pcn_dispute_eta_flag - -/*PCNs not eta or disputed*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'onstreet' -/*disputed by kpi pcn type*/ -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Estates - disputed_eta' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'CCTV - disputed_eta' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Car_Parks - disputed_eta' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'onstreet - disputed_eta' -else 'NOT KPI' end as kpi_pcn_not_dispute_eta_name - - ---PCN Calculation include Where code warningflag = 0 and isvda = 0 and isvoid = 0. No Warnings NO VDA and No voided PCNs ---) and (cast(isvda as varchar) like '0') and (cast(isvoid as varchar) like '0') and (cast(warningflag as varchar) like '0') ---and (isvda = 0) and (isvoid = 0) and (warningflag = 0) - --- PCNs kpi by pcn types -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Estates -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_CCTV - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Car_Parks - - -,Sum(Case When (isvda = 1) or (isvoid = 1) or (warningflag = 1) Then 1 Else 0 End) as Flag_total_vda_void_warning - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) Then 1 Else 0 End) as Flag_pi_Estates -,Sum(Case When zone like 'Car Parks' Then 1 Else 0 End) as Flag_pi_Car_Parks -,Sum(Case When debttype like 'CCTV%' Then 1 Else 0 End) as Flag_pi_CCTV -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' Then 1 Else 0 End) as Flag_pi_onstreet -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' Then 1 Else 0 End) as Flag_pi_onstreet_carparks - -,Sum(Case When debttype like 'CEO' Then 1 Else 0 End) as Flag_debttype_CEO -,Sum(Case When debttype like 'CCTV Moving traffic' Then 1 Else 0 End) as Flag_debttype_CCTV_Moving_traffic -,Sum(Case When debttype like 'CCTV static' Then 1 Else 0 End) as Flag_debttype_CCTV_static -,Sum(Case When debttype like 'CCTV Bus Lane' Then 1 Else 0 End) as Flag_debttype_CCTV_Bus_Lane -,Sum(Case When debttype like 'Manual_Tickets' Then 1 Else 0 End) as Flag_debttype_Manual_Tickets - --- pcns not in kpi -,sum(case when cast(pcnissuedate as date) > cast('2021-08-09' as date) then 1 else 0 end) as af_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-08-10' as date) then 1 else 0 end) as bf_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) > cast('2021-05-31' as date) then 1 else 0 end) as af_first_jun_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-06-01' as date) then 1 else 0 end) as bf_first_jun_PCN_issue - -,Sum(Case When isvda = 1 Then 1 Else 0 End) as Flag_total_isvda -,Sum(Case When isvoid = 1 Then 1 Else 0 End) as Flag_total_isvoid -,Sum(Case When cast(isremoval as INTEGER) = 1 Then 1 Else 0 End) as Flag_total_isremoval -,Sum(Case When warningflag = 1 Then 1 Else 0 End) as Flag_total_warningflag - -,Sum(Case When progressionstage like 'discount' Then 1 Else 0 End) as Flag_progressionstage_discount -,Sum(Case When progressionstage like 'postalgrace' Then 1 Else 0 End) as Flag_progressionstage_postalgrace -,Sum(Case When progressionstage like 'waitdvla' Then 1 Else 0 End) as Flag_progressionstage_waitdvla -,Sum(Case When progressionstage like 'readytoprint' Then 1 Else 0 End) as Flag_progressionstage_readytoprint -,Sum(Case When progressionstage like 'nto' Then 1 Else 0 End) as Flag_progressionstage_nto -,Sum(Case When progressionstage like 'full' Then 1 Else 0 End) as Flag_progressionstage_full -,Sum(Case When progressionstage like 'cc' Then 1 Else 0 End) as Flag_progressionstage_cc -,Sum(Case When progressionstage like 'nfa' Then 1 Else 0 End) as Flag_progressionstage_nfa -,Sum(Case When progressionstage like 'warningnoticesent' Then 1 Else 0 End) as Flag_progressionstage_warningnoticesent -,Sum(Case When progressionstage like 'en' Then 1 Else 0 End) as Flag_progressionstage_en -,Sum(Case When progressionstage like 'foreigncollection' Then 1 Else 0 End) as Flag_progressionstage_foreigncollection -,Sum(Case When progressionstage like 'nodr' Then 1 Else 0 End) as Flag_progressionstage_nodr -,Sum(Case When progressionstage like 'predebt' Then 1 Else 0 End) as Flag_progressionstage_predebt -,Sum(Case When progressionstage like 'nodrr' Then 1 Else 0 End) as Flag_progressionstage_nodrr -,Sum(Case When progressionstage like 'warrant' Then 1 Else 0 End) as Flag_progressionstage_warrant -,Sum(Case When progressionstage like 'warr' Then 1 Else 0 End) as Flag_progressionstage_warr -,Sum(Case When progressionstage like 'pre-debt' Then 1 Else 0 End) as Flag_progressionstage_pre_debt - - -,Sum(Case When nto_printed is not null Then 1 Else 0 End) as Flag_nto_printed -,Sum(Case When appeal_accepted is not null Then 1 Else 0 End) as Flag_appeal_accepted -,Sum(Case When arrived_in_pound is not null Then 1 Else 0 End) as Flag_arrived_in_pound -,Sum(Case When cancellation_reversed is not null Then 1 Else 0 End) as Flag_cancellation_reversed -,Sum(Case When cc_printed is not null Then 1 Else 0 End) as Flag_cc_printed -,Sum(Case When drr is not null Then 1 Else 0 End) as Flag_drr -,Sum(Case When en_printed is not null Then 1 Else 0 End) as Flag_en_printed -,Sum(Case When hold_released is not null Then 1 Else 0 End) as Flag_hold_released -,Sum(Case When dvla_response is not null Then 1 Else 0 End) as Flag_dvla_response -,Sum(Case When dvla_request is not null Then 1 Else 0 End) as Flag_dvla_request -,Sum(Case When full_rate_uplift is not null Then 1 Else 0 End) as Flag_full_rate_uplift -,Sum(Case When hold_until is not null Then 1 Else 0 End) as Flag_hold_until -,Sum(Case When lifted_at is not null Then 1 Else 0 End) as Flag_lifted_at -,Sum(Case When lifted_by is not null Then 1 Else 0 End) as Flag_lifted_by -,Sum(Case When loaded is not null Then 1 Else 0 End) as Flag_loaded -,Sum(Case When nor_sent is not null Then 1 Else 0 End) as Flag_nor_sent -,Sum(Case When notice_held is not null Then 1 Else 0 End) as Flag_notice_held -,Sum(Case When ofr_printed is not null Then 1 Else 0 End) as Flag_ofr_printed -,Sum(Case When pcn_printed is not null Then 1 Else 0 End) as Flag_pcn_printed -,Sum(Case When reissue_nto_requested is not null Then 1 Else 0 End) as Flag_reissue_nto_requested -,Sum(Case When reissue_pcn is not null Then 1 Else 0 End) as Flag_reissue_pcn -,Sum(Case When set_back_to_pre_cc_stage is not null Then 1 Else 0 End) as Flag_set_back_to_pre_cc_stage -,Sum(Case When vehicle_released_for_auction is not null Then 1 Else 0 End) as Flag_vehicle_released_for_auction -,Sum(Case When warrant_issued is not null Then 1 Else 0 End) as Flag_warrant_issued -,Sum(Case When warrant_redistributed is not null Then 1 Else 0 End) as Flag_warrant_redistributed -,Sum(Case When warrant_request_granted is not null Then 1 Else 0 End) as Flag_warrant_request_granted -,Sum(Case When ad_hoc_vq4_request is not null Then 1 Else 0 End) as Flag_ad_hoc_vq4_request -,Sum(Case When paper_vq5_received is not null Then 1 Else 0 End) as Flag_paper_vq5_received -,Sum(Case When pcn_extracted_for_buslane is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_buslane -,Sum(Case When pcn_extracted_for_pre_debt is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_pre_debt -,Sum(Case When pcn_extracted_for_collection is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_collection -,Sum(Case When pcn_extracted_for_drr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_drr -,Sum(Case When pcn_extracted_for_cc is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_cc -,Sum(Case When pcn_extracted_for_nto is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_nto -,Sum(Case When pcn_extracted_for_print is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_print -,Sum(Case When warning_notice_extracted_for_print is not null Then 1 Else 0 End) as Flag_warning_notice_extracted_for_print -,Sum(Case When pcn_extracted_for_ofr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_ofr -,Sum(Case When pcn_extracted_for_warrant_request is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_warrant_request -,Sum(Case When pre_debt_new_debtor_details is not null Then 1 Else 0 End) as Flag_pre_debt_new_debtor_details - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date -left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date -left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date -left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = pcnfoidetails_pcn_foi_full.pcn -left join tot_pcn_loc on tot_pcn_loc.tpl_street_location = pcnfoidetails_pcn_foi_full.street_location -/*from er -left join eta_recs on substr(eta_recs.etar_pcn, 1, 10) = substr(er.pcn, 1, 10) -left join Disputes on Disputes.ticketserialnumber = substr(er.pcn, 1, 10) -left join pcn on substr(er.pcn, 1, 10) = pcn.pcn*/ - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '400' day --Last 400 days from todays date -group by pcnissuedate --,concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) -,tpl_kpi_pcn_dispute_eta_flag, tpl_location_period_days ,tpl_location_days ,case when tpl_location_days >0 then (tpl_kpi_pcn_dispute_eta_flag / tpl_location_days) else 0 end ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') ---,cast(Substr(pcnissuedate, 1,7) as date) -, debttype -, zone - ---added to get pcn records rather than summary --for use in major cctv dashboards -,pcnfoidetails_pcn_foi_full.pcn -,cancellationgroup -,cancellationreason ---below is as per original summary script - -/* pcn kpi type name*/ -,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 Then 'onstreet' -else debttype end -, (Case When zone like 'Estates' Then usrn Else zone End) -, usrn -, street_location --- , whereonlocation -,(case When street_location = 'Allen Road' then 'YB1052 - Allen Road' -When street_location = 'Ashenden Road junction of Glyn Road' then 'LW2205 - Ashenden Road' -When street_location = 'Barnabas Road JCT Berger Road' then 'LW2204 - Barnabas Road' -When street_location = 'Barnabas Road JCT Oriel Road' then 'LW2204 - Barnabas Road' -When street_location = 'Benthal Road' then 'LW2206 - Benthal Road' -When street_location = 'Bouverie Road junction of Stoke Newington Church Street' then 'LW2593 - Bouverie Road' -When street_location = 'Clissold Crescent' then 'LW2397 - Clissold Crescent' -When street_location = 'Cremer Street' then 'LW2041 - Cremer Street' -When street_location = 'Downs Road' then 'LW2389 - Downs Road' -When street_location = 'Elsdale Street' then 'LW2393 - Elsdale Street' -When street_location = 'Gore Road junction of Lauriston Road.' then 'LW2078 - Gore Road' -When street_location = 'Hyde Road' then 'LW2208 - Hyde Road' -When street_location = 'Hyde Road JCT Northport Street' then 'LW2208 - Hyde Road' -When street_location = 'Lee Street junction of Stean Street' then 'LW1535 - Lee Street' -When street_location = 'Loddiges Road jct Frampton Park Road' then 'LW1051 - Loddiges Road' -When street_location = 'Lordship Road junction of Lordship Terrace' then 'LW2590 - Lordship Road' -When street_location = 'Maury Road junction of Evering Road' then 'LW2390 - Maury Road' -When street_location = 'Mead Place' then 'LW2395 - Mead Place' -When street_location = 'Meeson Street junction of Kingsmead Way' then 'LW2079 - Meeson Street' -When street_location = 'Mount Pleasant Lane' then 'LW2391 - Mount Pleasant Lane' -When street_location = 'Nevill Road junction of Barbauld Road' then 'LW2595 - Nevill Road/ Barbauld Road' -When street_location = 'Nevill Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Neville Road junction of Osterley Road' then 'LW0633 - Nevill Road (Osterley Road)' -When street_location = 'Oldfield Road (E)' then 'LW2596 - Oldfield Road' -When street_location = 'Oldfield Road junction of Kynaston Road' then 'LW2596 - Oldfield Road' -When street_location = 'Pitfield Street (F)' then 'LW2207 - Pitfield Street' -When street_location = 'Pitfield Street JCT Hemsworth Street' then 'LW2207 - Pitfield Street' -When street_location = 'Powell Road junction of Kenninghall Road' then 'LW1691 - Powell Road (Kenninghall Road)' -When street_location = 'Shepherdess Walk' then 'LW2076 - Shepherdess Walk' -When street_location = 'Shore Place' then 'Mobile camera car - Shore Place' -When street_location = 'Stoke Newington Church Street junction of Lordship Road - Eastbound' then 'LW2591 - Stoke Newington Church Street eastbound' -When street_location = 'Stoke Newington Church Street junction of Marton Road - Westbound' then 'LW2592 - Stoke Newington Church Street westbound' -When street_location = 'Ufton Road junction of Downham Road' then 'LW2077 - Ufton Road' -When street_location = 'Wayland Avenue' then 'LW2392 - Wayland Avenue' -When street_location = 'Weymouth Terrace junction of Dunloe Street' then 'Mobile camera car - Weymouth Terrace' -When street_location = 'Wilton Way junction of Greenwood Road' then 'LW1457 - Wilton Way' -When street_location = 'Woodberry Grove junction of Rowley Gardens' then 'LW1457 - Woodberry Grove' -When street_location = 'Woodberry Grove junction of Seven Sisters Road' then 'LW1457 - Woodberry Grove' -When street_location = 'Yoakley Road junction of Stoke Newington Church Street' then 'LW2594 - Yoakley Road' else 'NOT current LTN Camera Location' end) -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds -, eta_outcome -/*flag - PCNs catergorised not eta or disputed for stacking*/,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'Y' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'Y' -else 'N' end -/*PCNs catergorised not eta or disputed for stacking*/,case -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null then 'Not dispute or eta' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is not null then 'disputed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is null then 'eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is null then 'eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is null then 'eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is null then 'eta rejected' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_with_direction > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta with direction' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_dnc > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta dnc' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_allowed > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta allowed' -when ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and eta_recs.appeal_rejected > 0 and Disputes.ticketserialnumber is not null then 'disputed and eta rejected' -else 'NOT KPI' end -/*PCNs not eta or disputed*/,case -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Estates' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'CCTV' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'Car_Parks' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected = 0 and eta_recs.appeal_allowed = 0 and eta_recs.appeal_dnc = 0 and eta_recs.appeal_with_direction = 0) or (eta_recs.appeal_rejected is null and eta_recs.appeal_allowed is null and eta_recs.appeal_dnc is null and eta_recs.appeal_with_direction is null)) and Disputes.ticketserialnumber is null Then 'onstreet' -/*disputed by kpi pcn type*/ -when ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Estates - disputed_eta' -when debttype like 'CCTV%' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'CCTV - disputed_eta' -when zone like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'Car_Parks - disputed_eta' -When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) and isvoid = 0 and warningflag = 0 and ((eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) or (Disputes.ticketserialnumber is not null)) Then 'onstreet - disputed_eta' -else 'NOT KPI' end - -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date -- import_date ---, current_timestamp() as ImportDateTime -order by pcnissuedate --concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') desc -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_ic": AmazonS3Liberator_pcn_ic_node1631812698045, - "pcnfoidetails_pcn_foi_full": S3bucketpcnfoidetails_pcn_foi_full_node1, - "liberator_pcn_tickets": AmazonS3liberator_pcn_tickets_node1637153316033, - "liberator_pcn_audit": AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740, - "eta_decision_records": AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-"+environment+"-refined-zone/parking/liberator/parking_foi_pcn_gds_daily_summary_records/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-"+environment+"-liberator-refined-zone", - catalogTableName="parking_foi_pcn_gds_daily_summary_records", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/scripts/jobs/parking/parking_foi_pcn_gds_summary.py b/scripts/jobs/parking/parking_foi_pcn_gds_summary.py deleted file mode 100644 index f31a30a59..000000000 --- a/scripts/jobs/parking/parking_foi_pcn_gds_summary.py +++ /dev/null @@ -1,630 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame - -from scripts.helpers.helpers import get_glue_env_var, create_pushdown_predicate -environment = get_glue_env_var("environment") - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) - -# Script generated for node Amazon S3 - Liberator_pcn_ic -AmazonS3Liberator_pcn_ic_node1631812698045 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_ic", - transformation_ctx="AmazonS3Liberator_pcn_ic_node1631812698045", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node S3 bucket - pcnfoidetails_pcn_foi_full -S3bucketpcnfoidetails_pcn_foi_full_node1 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="S3bucketpcnfoidetails_pcn_foi_full_node1", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator_pcn_tickets -AmazonS3liberator_pcn_tickets_node1637153316033 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="AmazonS3liberator_pcn_tickets_node1637153316033", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator-raw-zone - liberator_pcn_audit -AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_audit", - transformation_ctx="AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 - parking-raw-zone - parking_eta_decision_records -AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578 = glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - table_name="parking_eta_decision_records", - transformation_ctx="AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578", -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ --- PCN GDS summary with location for FOI Dashboard -/* -17/11/2021 Added With regkep for field registered_keeper_address to pick up from original table liberator_pcn_tickets as from pcnfoidetails_pcn_foi_full cannot be resolved -30/11/2021 Updated/added PCN recovery by PCN type -20/01/2022 added additional field street_location comment out fields (whereonlocation, holdreason, bailiff, eta_appealgrounds, eta_outcome) increased to 51 mths from 34 mths about 98055 records -*/ - -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, -sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction','Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) - -,regkep as(select ticketserialnumber,registered_keeper_address,current_ticket_address,import_timestamp,import_year,import_month,import_day,import_date from liberator_pcn_tickets -WHERE liberator_pcn_tickets.import_date = (SELECT max(liberator_pcn_tickets.import_date) from liberator_pcn_tickets) -) -,pcn_audit as( -SELECT distinct(audit_message),ticket_ref, liberator_pcn_audit.import_date, 1 as flag_write_off FROM liberator_pcn_audit -where liberator_pcn_audit.import_date = (SELECT max(liberator_pcn_audit.import_date) from liberator_pcn_audit) -and audit_message in ('Debt write-off request raised: 3 or more VQ5 without keeper','Debt write-off request raised: 455+ day old warrant','Debt write-off request raised: DVLA address','Debt write-off request raised: Foreign vehicle','Debt write-off request raised: No seemingly valid postcode','Debt write-off request raised: Scottish address') -order by ticket_ref desc -) -, pcn_recovery_reason as (select distinct pcn as cancel_pcn, cancellationgroup as cancellation_group, cancellationreason as cancellation_reason, pcnfoidetails_pcn_foi_full.import_date, 1 as flag_cancel_reason FROM pcnfoidetails_pcn_foi_full -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and cancellationgroup in ('Bailiff','CANCELLED - VULNERABLE DEBTOR','Debt Recovery','Pre-debt','System','Write-off')) - -select -concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') AS IssueMonthYear -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) AS Dispute_kpi_MonthYear ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) as monthyearissue ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') as month_year_pcnissuedate ---,cast(Substr(pcnissuedate, 1,7) as date) as month_year_pcnissue_date -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) as r_zone -, usrn -, street_location --- , whereonlocation -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds --- , eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date as importdate -- import_date -, concat(pcnfoidetails_pcn_foi_full.import_year,pcnfoidetails_pcn_foi_full.import_month,pcnfoidetails_pcn_foi_full.import_day) as import_date ---, current_timestamp() as ImportDateTime - -,count(*) as PCNs_Records -,Count (pcn_canx_date) as Num_Cancelled -,Count (pcn_casecloseddate) as Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as Flag_PCN_CANCELLED -,count(regkep.registered_keeper_address) as num_reg_keep -,count(regkep.current_ticket_address) as num_curr_add - --- Flag registered_keeper_address or current address registered_keeper_address -,Sum(Case When regkep.registered_keeper_address != '' or regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_address -,Sum(Case When regkep.registered_keeper_address = '' or regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_address_null -,Sum(Case When regkep.registered_keeper_address != '' Then 1 Else 0 End) as Flag_reg_keeper_address -,Sum(Case When regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_current_address -,Sum(Case When regkep.registered_keeper_address = '' Then 1 Else 0 End) as Flag_reg_keeper_address_null -,Sum(Case When regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_current_address_null - --- CEO error All -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error_pcn - - --- ceo error flag by pcn type -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error - -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error - - --- CEO error flag Without VOID VDA or warningflag -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_errorWO -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_errorWO -,Sum(Case When ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_errorWO -,Sum(Case When ((zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_errorWO -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_errorWO - --- ceo error flag by pcn types with VDA -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_vda -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_vda -,Sum(Case When ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_vda -,Sum(Case When ((zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_vda -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_vda - --- PCNs kpi by pcn types for CEO Error -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error_pcn - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_pcn -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_pcn -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_pcn -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error_pcn - -,Sum(CASE When Lib_Payment_Received != '0' Then 1 Else 0 END) as Flag_PCN_PAYMENT -,Count(distinct pcn) as PCNs -,CAST(SUM(cast(lib_initial_debt_amount as double)) as decimal(11,2)) as Total_lib_initial_debt_amount -,CAST(SUM(cast(lib_payment_received as double)) as decimal(11,2)) as Total_lib_payment_received -,CAST(SUM(cast(lib_write_off_amount as double)) as decimal(11,2)) as Total_lib_write_off_amount -,CAST(SUM(cast(lib_payment_void as double)) as decimal(11,2)) as Total_lib_payment_void - --- KPI PCNs Paid by PCN type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Estates_paid -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_CCTV_paid - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_paid - --- Paid with address by pcn type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid_address - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Estates_paid_address -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_CCTV_paid_address - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid_address -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_paid_address - --- PCN Recovery -/*PCN Recovered All*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered - -,sum(case - when ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered_can - -/*PCN Recovered by Type - CCTV*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_cctv - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_can_cctv - -/*PCN Recovered by Type - Estates*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_estates - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_can_estates - -/*PCN Recovered by Type - Carparks*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_can_carparks - - -/*PCN Recovered by Type - onstreet*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_onstreet - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_can_onstreet - - -/*PCN Recovered by Type - carparks and onstreet*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_onstreet_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_can_onstreet_carparks - -/*PCNs by Type with VDA's excluded before and included after 1st June 2021*/ - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet_carparks - -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Estates - -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_CCTV - -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Car_Parks - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet - - - --- Disputed pcns and by pcn type -,count(distinct Disputes.ticketserialnumber) as TotalpcnDisputed -,count(Disputes.ticketserialnumber) as TotalDisputed -/*,COUNT (distinct if (isvda = 0 and isvoid = 0 and warningflag = 0, Disputes.ticketserialnumber - )) as Dispute_pcn_Total -,COUNT (if (isvda = 0 and isvoid = 0 and warningflag = 0, Disputes.ticketserialnumber - )) as Dispute_Total - */ - -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flg_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_Estates_disputes -,COUNT(DISTINCT(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_CCTV_disputes - -,COUNT(DISTINCT(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_Car_Parks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_onstreet_disputes -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flg_kpi_onstreet_disputes - -/*ETA records with by PCN Type*/ -,sum(eta_recs.appeal_rejected) as decision_appeal_rejected -,sum(eta_recs.appeal_allowed) as decision_appeal_allowed -,sum(eta_recs.appeal_dnc) as decision_appeal_dnc -,sum(eta_recs.appeal_with_direction) as decision_appeal_with_direction - -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 0 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 0 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 0 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 0 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 0 or eta_recs.appeal_allowed > 0 or eta_recs.appeal_dnc > 0 or eta_recs.appeal_with_direction > 0) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_CCTV - - ---PCN Calculation include Where code warningflag = 0 and isvda = 0 and isvoid = 0. No Warnings NO VDA and No voided PCNs ---) and (cast(isvda as varchar) like '0') and (cast(isvoid as varchar) like '0') and (cast(warningflag as varchar) like '0') ---and (isvda = 0) and (isvoid = 0) and (warningflag = 0) - --- PCNs kpi by pcn types -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Estates -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_CCTV - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Car_Parks - - -,Sum(Case When (isvda = 1) or (isvoid = 1) or (warningflag = 1) Then 1 Else 0 End) as Flag_total_vda_void_warning - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) Then 1 Else 0 End) as Flag_pi_Estates -,Sum(Case When zone like 'Car Parks' Then 1 Else 0 End) as Flag_pi_Car_Parks -,Sum(Case When debttype like 'CCTV%' Then 1 Else 0 End) as Flag_pi_CCTV -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' Then 1 Else 0 End) as Flag_pi_onstreet -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' Then 1 Else 0 End) as Flag_pi_onstreet_carparks - -,Sum(Case When debttype like 'CEO' Then 1 Else 0 End) as Flag_debttype_CEO -,Sum(Case When debttype like 'CCTV Moving traffic' Then 1 Else 0 End) as Flag_debttype_CCTV_Moving_traffic -,Sum(Case When debttype like 'CCTV static' Then 1 Else 0 End) as Flag_debttype_CCTV_static -,Sum(Case When debttype like 'CCTV Bus Lane' Then 1 Else 0 End) as Flag_debttype_CCTV_Bus_Lane -,Sum(Case When debttype like 'Manual_Tickets' Then 1 Else 0 End) as Flag_debttype_Manual_Tickets - --- pcns not in kpi -,sum(case when cast(pcnissuedate as date) > cast('2021-08-09' as date) then 1 else 0 end) as af_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-08-10' as date) then 1 else 0 end) as bf_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) > cast('2021-05-31' as date) then 1 else 0 end) as af_first_jun_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-06-01' as date) then 1 else 0 end) as bf_first_jun_PCN_issue - -,Sum(Case When isvda = 1 Then 1 Else 0 End) as Flag_total_isvda -,Sum(Case When isvoid = 1 Then 1 Else 0 End) as Flag_total_isvoid -,Sum(Case When cast(isremoval as INTEGER) = 1 Then 1 Else 0 End) as Flag_total_isremoval -,Sum(Case When warningflag = 1 Then 1 Else 0 End) as Flag_total_warningflag - -,Sum(Case When progressionstage like 'discount' Then 1 Else 0 End) as Flag_progressionstage_discount -,Sum(Case When progressionstage like 'postalgrace' Then 1 Else 0 End) as Flag_progressionstage_postalgrace -,Sum(Case When progressionstage like 'waitdvla' Then 1 Else 0 End) as Flag_progressionstage_waitdvla -,Sum(Case When progressionstage like 'readytoprint' Then 1 Else 0 End) as Flag_progressionstage_readytoprint -,Sum(Case When progressionstage like 'nto' Then 1 Else 0 End) as Flag_progressionstage_nto -,Sum(Case When progressionstage like 'full' Then 1 Else 0 End) as Flag_progressionstage_full -,Sum(Case When progressionstage like 'cc' Then 1 Else 0 End) as Flag_progressionstage_cc -,Sum(Case When progressionstage like 'nfa' Then 1 Else 0 End) as Flag_progressionstage_nfa -,Sum(Case When progressionstage like 'warningnoticesent' Then 1 Else 0 End) as Flag_progressionstage_warningnoticesent -,Sum(Case When progressionstage like 'en' Then 1 Else 0 End) as Flag_progressionstage_en -,Sum(Case When progressionstage like 'foreigncollection' Then 1 Else 0 End) as Flag_progressionstage_foreigncollection -,Sum(Case When progressionstage like 'nodr' Then 1 Else 0 End) as Flag_progressionstage_nodr -,Sum(Case When progressionstage like 'predebt' Then 1 Else 0 End) as Flag_progressionstage_predebt -,Sum(Case When progressionstage like 'nodrr' Then 1 Else 0 End) as Flag_progressionstage_nodrr -,Sum(Case When progressionstage like 'warrant' Then 1 Else 0 End) as Flag_progressionstage_warrant -,Sum(Case When progressionstage like 'warr' Then 1 Else 0 End) as Flag_progressionstage_warr -,Sum(Case When progressionstage like 'pre-debt' Then 1 Else 0 End) as Flag_progressionstage_pre_debt - - -,Sum(Case When nto_printed is not null Then 1 Else 0 End) as Flag_nto_printed -,Sum(Case When appeal_accepted is not null Then 1 Else 0 End) as Flag_appeal_accepted -,Sum(Case When arrived_in_pound is not null Then 1 Else 0 End) as Flag_arrived_in_pound -,Sum(Case When cancellation_reversed is not null Then 1 Else 0 End) as Flag_cancellation_reversed -,Sum(Case When cc_printed is not null Then 1 Else 0 End) as Flag_cc_printed -,Sum(Case When drr is not null Then 1 Else 0 End) as Flag_drr -,Sum(Case When en_printed is not null Then 1 Else 0 End) as Flag_en_printed -,Sum(Case When hold_released is not null Then 1 Else 0 End) as Flag_hold_released -,Sum(Case When dvla_response is not null Then 1 Else 0 End) as Flag_dvla_response -,Sum(Case When dvla_request is not null Then 1 Else 0 End) as Flag_dvla_request -,Sum(Case When full_rate_uplift is not null Then 1 Else 0 End) as Flag_full_rate_uplift -,Sum(Case When hold_until is not null Then 1 Else 0 End) as Flag_hold_until -,Sum(Case When lifted_at is not null Then 1 Else 0 End) as Flag_lifted_at -,Sum(Case When lifted_by is not null Then 1 Else 0 End) as Flag_lifted_by -,Sum(Case When loaded is not null Then 1 Else 0 End) as Flag_loaded -,Sum(Case When nor_sent is not null Then 1 Else 0 End) as Flag_nor_sent -,Sum(Case When notice_held is not null Then 1 Else 0 End) as Flag_notice_held -,Sum(Case When ofr_printed is not null Then 1 Else 0 End) as Flag_ofr_printed -,Sum(Case When pcn_printed is not null Then 1 Else 0 End) as Flag_pcn_printed -,Sum(Case When reissue_nto_requested is not null Then 1 Else 0 End) as Flag_reissue_nto_requested -,Sum(Case When reissue_pcn is not null Then 1 Else 0 End) as Flag_reissue_pcn -,Sum(Case When set_back_to_pre_cc_stage is not null Then 1 Else 0 End) as Flag_set_back_to_pre_cc_stage -,Sum(Case When vehicle_released_for_auction is not null Then 1 Else 0 End) as Flag_vehicle_released_for_auction -,Sum(Case When warrant_issued is not null Then 1 Else 0 End) as Flag_warrant_issued -,Sum(Case When warrant_redistributed is not null Then 1 Else 0 End) as Flag_warrant_redistributed -,Sum(Case When warrant_request_granted is not null Then 1 Else 0 End) as Flag_warrant_request_granted -,Sum(Case When ad_hoc_vq4_request is not null Then 1 Else 0 End) as Flag_ad_hoc_vq4_request -,Sum(Case When paper_vq5_received is not null Then 1 Else 0 End) as Flag_paper_vq5_received -,Sum(Case When pcn_extracted_for_buslane is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_buslane -,Sum(Case When pcn_extracted_for_pre_debt is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_pre_debt -,Sum(Case When pcn_extracted_for_collection is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_collection -,Sum(Case When pcn_extracted_for_drr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_drr -,Sum(Case When pcn_extracted_for_cc is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_cc -,Sum(Case When pcn_extracted_for_nto is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_nto -,Sum(Case When pcn_extracted_for_print is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_print -,Sum(Case When warning_notice_extracted_for_print is not null Then 1 Else 0 End) as Flag_warning_notice_extracted_for_print -,Sum(Case When pcn_extracted_for_ofr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_ofr -,Sum(Case When pcn_extracted_for_warrant_request is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_warrant_request -,Sum(Case When pre_debt_new_debtor_details is not null Then 1 Else 0 End) as Flag_pre_debt_new_debtor_details - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date -left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date -left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date -left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on eta_recs.etar_pcn = pcnfoidetails_pcn_foi_full.pcn - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '51' month --Last 36 months from todays date -group by concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') ---,cast(Substr(pcnissuedate, 1,7) as date) -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) -, usrn -, street_location --- , whereonlocation -, contraventioncode -, contraventionsuffix --- , holdreason --- , bailiff --- , eta_appealgrounds --- , eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date -- import_date ---, current_timestamp() as ImportDateTime -order by concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') desc -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_ic": AmazonS3Liberator_pcn_ic_node1631812698045, - "pcnfoidetails_pcn_foi_full": S3bucketpcnfoidetails_pcn_foi_full_node1, - "liberator_pcn_tickets": AmazonS3liberator_pcn_tickets_node1637153316033, - "liberator_pcn_audit": AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740, - "eta_decision_records": AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-" + environment + "-refined-zone/parking/liberator/parking_foi_pcn_gds_summary/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="parking_foi_pcn_gds_summary", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/scripts/jobs/parking/parking_foreign_vrm_pcns.py b/scripts/jobs/parking/parking_foreign_vrm_pcns.py deleted file mode 100644 index 2af83b08f..000000000 --- a/scripts/jobs/parking/parking_foreign_vrm_pcns.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -Note: python file name should be the same as the table name -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "Parking_Foreign_VRM_PCNs" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/************************************************************************************************************************* - Parking_Foreign_VRM_PCNs - - This SQL creates the list of VRMs against the current valid Permits - - 02/03/2022 - Create Query - *************************************************************************************************************************/ -With PCN_Raw as ( - select * - from "dataplatform-prod-liberator-raw-zone".liberator_pcn_tickets - Where Import_Date = ( - Select MAX(Import_Date) - from "dataplatform-prod-liberator-raw-zone".liberator_pcn_tickets - ) - and lower(foreignvehiclecountry) = 'y' -) -SELECT PCN, - cast(pcnissuedatetime as timestamp) as pcnissuedatetime, - pcn_canx_date, - A.cancellationgroup, - A.cancellationreason, - street_location, - whereonlocation, - zone, - usrn, - A.contraventioncode, - debttype, - A.vrm, - vehiclemake, - vehiclemodel, - vehiclecolour, - ceo, - isremoval, - A.progressionstage, - lib_payment_received as payment_received, - whenpaid as PaymentDate, - noderef, - replace(replace(ticketnotes, '\r', ''), '\n', '') as ticketnotes, - cast(current_timestamp as timestamp) as ImportDateTime, - format_datetime(current_date, 'yyyy') AS import_year, - format_datetime(current_date, 'MM') AS import_month, - format_datetime(current_date, 'dd') AS import_day, - format_datetime(current_date, 'yyyyMMdd') AS import_date -FROM "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full as A - INNER JOIN PCN_Raw as B ON A.pcn = B.ticketserialnumber -Where ImportDateTime = ( - Select MAX(ImportDateTime) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and warningflag = 0 -Order By pcnissuedatetime; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_kpi_gds_summary.py b/scripts/jobs/parking/parking_kpi_gds_summary.py deleted file mode 100644 index ebddd7d5c..000000000 --- a/scripts/jobs/parking/parking_kpi_gds_summary.py +++ /dev/null @@ -1,625 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame - -from scripts.helpers.helpers import get_glue_env_var, create_pushdown_predicate -environment = get_glue_env_var("environment") - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) - -# Script generated for node Amazon S3 - Liberator_pcn_ic -AmazonS3Liberator_pcn_ic_node1631812698045 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_ic", - transformation_ctx="AmazonS3Liberator_pcn_ic_node1631812698045", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node S3 bucket - pcnfoidetails_pcn_foi_full -S3bucketpcnfoidetails_pcn_foi_full_node1 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="S3bucketpcnfoidetails_pcn_foi_full_node1", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator_pcn_tickets -AmazonS3liberator_pcn_tickets_node1637153316033 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="AmazonS3liberator_pcn_tickets_node1637153316033", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node Amazon S3 - liberator-raw-zone - liberator_pcn_audit -AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_audit", - transformation_ctx="AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 - parking raw - parking_eta_decision_records -AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578 = ( - glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - table_name="parking_eta_decision_records", - transformation_ctx="AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578", - ) -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ --- PCN GDS summary with disputes -/* -17/11/2021 Added With regkep for field registered_keeper_address to pick up from original table liberator_pcn_tickets as from pcnfoidetails_pcn_foi_full cannot be resolved -30/11/2021 Updated/added PCN recovery by PCN type -*/ - -With Disputes as ( -SELECT distinct -- *, - liberator_pcn_ic.ticketserialnumber, --- liberator_pcn_ic.Serviceable, - cast(substr(liberator_pcn_ic.date_received, 1, 10) as date) as date_received, - cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date) as response_generated_at, - concat(substr(Cast(liberator_pcn_ic.date_received as varchar(10)),1, 7), '-01') as MonthYear, - concat(substr(Cast(liberator_pcn_ic.response_generated_at as varchar(10)),1, 7), '-01') as response_MonthYear, - datediff( cast(substr(liberator_pcn_ic.date_received, 1, 10) as date), cast(substr(liberator_pcn_ic.response_generated_at, 1, 10) as date)) as ResponseDays, - import_date - -from liberator_pcn_ic - -where liberator_pcn_ic.import_Date = (Select MAX(liberator_pcn_ic.import_date) from liberator_pcn_ic) -AND liberator_pcn_ic.date_received != '' AND liberator_pcn_ic.response_generated_at != '' -AND length(liberator_pcn_ic.ticketserialnumber) = 10 -AND liberator_pcn_ic.Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations') -) -,eta_recs as ( -select pcn as etar_pcn, -sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Rejected','Appeal Refused') then 1 else 0 end) as appeal_rejected -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal Allowed','appeal Allowed') then 1 else 0 end) as appeal_allowed -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('DNC','dnc') then 1 else 0 end) as appeal_dnc -,sum(Case when decision_i_e_dnc_appeal_allowed_appeal_rejected in ('Appeal with Direction') then 1 else 0 end) as appeal_with_direction - -FROM eta_decision_records where eta_decision_records.import_date =(select max(eta_decision_records.import_date) FROM eta_decision_records) -group by pcn order by pcn -) -,regkep as(select ticketserialnumber,registered_keeper_address,current_ticket_address,import_timestamp,import_year,import_month,import_day,import_date from liberator_pcn_tickets -WHERE liberator_pcn_tickets.import_date = (SELECT max(liberator_pcn_tickets.import_date) from liberator_pcn_tickets) -) -,pcn_audit as( -SELECT distinct(audit_message),ticket_ref, liberator_pcn_audit.import_date, 1 as flag_write_off FROM liberator_pcn_audit -where liberator_pcn_audit.import_date = (SELECT max(liberator_pcn_audit.import_date) from liberator_pcn_audit) -and audit_message in ('Debt write-off request raised: 3 or more VQ5 without keeper','Debt write-off request raised: 455+ day old warrant','Debt write-off request raised: DVLA address','Debt write-off request raised: Foreign vehicle','Debt write-off request raised: No seemingly valid postcode','Debt write-off request raised: Scottish address') -order by ticket_ref desc -) -, pcn_recovery_reason as (select distinct pcn as cancel_pcn, cancellationgroup as cancellation_group, cancellationreason as cancellation_reason, pcnfoidetails_pcn_foi_full.import_date, 1 as flag_cancel_reason FROM pcnfoidetails_pcn_foi_full -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and cancellationgroup in ('Bailiff','CANCELLED - VULNERABLE DEBTOR','Debt Recovery','Pre-debt','System','Write-off')) - -select -concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') AS IssueMonthYear -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) AS Dispute_kpi_MonthYear ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) as monthyearissue ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') as month_year_pcnissuedate ---,cast(Substr(pcnissuedate, 1,7) as date) as month_year_pcnissue_date -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) as r_zone -, usrn -, contraventioncode -, contraventionsuffix -, holdreason -, bailiff -, eta_appealgrounds -, eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date as importdate -- import_date -, concat(pcnfoidetails_pcn_foi_full.import_year,pcnfoidetails_pcn_foi_full.import_month,pcnfoidetails_pcn_foi_full.import_day) as import_date ---, current_timestamp() as ImportDateTime - -,count(*) as PCNs_Records -,Count (pcn_canx_date) as Num_Cancelled -,Count (pcn_casecloseddate) as Num_closed -,Sum(Case When pcn_canx_date is not null Then 1 Else 0 End) as Flag_PCN_CANCELLED -,count(regkep.registered_keeper_address) as num_reg_keep -,count(regkep.current_ticket_address) as num_curr_add - --- Flag registered_keeper_address or current address registered_keeper_address -,Sum(Case When regkep.registered_keeper_address != '' or regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_address -,Sum(Case When regkep.registered_keeper_address = '' or regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_address_null -,Sum(Case When regkep.registered_keeper_address != '' Then 1 Else 0 End) as Flag_reg_keeper_address -,Sum(Case When regkep.current_ticket_address != '' Then 1 Else 0 End) as Flag_current_address -,Sum(Case When regkep.registered_keeper_address = '' Then 1 Else 0 End) as Flag_reg_keeper_address_null -,Sum(Case When regkep.current_ticket_address = '' Then 1 Else 0 End) as Flag_current_address_null - --- CEO error All -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_ceo_error_pcn - - --- ceo error flag by pcn type -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error - -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error - - --- CEO error flag Without VOID VDA or warningflag -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_errorWO -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_errorWO -,Sum(Case When ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_errorWO -,Sum(Case When ((zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_errorWO -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_errorWO - --- ceo error flag by pcn types with VDA -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_vda -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_vda -,Sum(Case When ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_vda -,Sum(Case When ((zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_vda -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and (cancellationreason like '%CEO failed%' or cancellationreason like '%CEO issued%' or cancellationreason like '%CEO recorded%' or cancellationreason like '%Info recorded in Electronic notes wrong/Inconsistent%' or cancellationreason like '%No Electronic notes made%' or cancellationreason like '%No proof served/Issued correctly%' or cancellationreason like '%Photos if taken do not support PCN if challenged%' or cancellationreason like '%PCN Issued to exempted vehicle') and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_vda - --- PCNs kpi by pcn types for CEO Error -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks_ceo_error_pcn - -,Sum(Case When (((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ((zone like 'Estates' or street_location like '%Estate%' OR usrn like 'Z%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date)) Then 1 Else 0 End) as Flag_kpi_Estates_ceo_error_pcn -,Sum(Case When ((debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ((debttype like 'CCTV%') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_CCTV_ceo_error_pcn -,Sum(Case When ((zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date) ) or ( (zone like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_ceo_error_pcn -,Sum(Case When ((debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvoid = 0) and (warningflag = 0) and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flag_kpi_onstreet_ceo_error_pcn -,Sum(Case When (debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvda = 0 and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or ( debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and isvoid = 0 and warningflag = 0 and cast(pcnissuedate as date) > cast('2021-05-31' as date) ) Then 1 Else 0 End) as Flg_kpi_onstreet_ceo_error_pcn - -,Sum(CASE When Lib_Payment_Received != '0' Then 1 Else 0 END) as Flag_PCN_PAYMENT -,Count(distinct pcn) as PCNs -,CAST(SUM(cast(lib_initial_debt_amount as double)) as decimal(11,2)) as Total_lib_initial_debt_amount -,CAST(SUM(cast(lib_payment_received as double)) as decimal(11,2)) as Total_lib_payment_received -,CAST(SUM(cast(lib_write_off_amount as double)) as decimal(11,2)) as Total_lib_write_off_amount -,CAST(SUM(cast(lib_payment_void as double)) as decimal(11,2)) as Total_lib_payment_void - --- KPI PCNs Paid by PCN type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Estates_paid -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_CCTV_paid - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' Then 1 Else 0 End) as Flag_kpi_onstreet_paid - --- Paid with address by pcn type -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks_paid_address - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Estates_paid_address -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_CCTV_paid_address - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_Car_Parks_paid_address -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Lib_Payment_Received != '0' and (regkep.registered_keeper_address is null or regkep.registered_keeper_address = '' or regkep.registered_keeper_address = ' ' or regkep.current_ticket_address is null or regkep.current_ticket_address = '' or regkep.current_ticket_address = ' ' ) Then 1 Else 0 End) as Flag_kpi_onstreet_paid_address - --- PCN Recovery -/*PCN Recovered All*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered - -,sum(case - when ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') -then 1 - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' - then 1 else 0 end) as pcn_recovered_can - -/*PCN Recovered by Type - CCTV*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_cctv - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype like 'CCTV%' - and (regkep.registered_keeper_address != '' or regkep.current_ticket_address != '') - then 1 else 0 end) as pcn_recovered_can_cctv - -/*PCN Recovered by Type - Estates*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_estates - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - then 1 else 0 end) as pcn_recovered_can_estates - -/*PCN Recovered by Type - Carparks*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and (zone like 'Car Parks') - then 1 else 0 end) as pcn_recovered_can_carparks - - -/*PCN Recovered by Type - onstreet*/ - -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_onstreet - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - then 1 else 0 end) as pcn_recovered_can_onstreet - - -/*PCN Recovered by Type - carparks and onstreet*/ -,sum(case - when Lib_Payment_Received != '0' - and ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_onstreet_carparks - -,sum(case - when - ((pcn_canx_date is null and pcn_recovery_reason.cancellation_group is null and pcn_recovery_reason.cancellation_reason is null) or (pcn_canx_date is not null and pcn_recovery_reason.cancellation_group is not null and pcn_recovery_reason.cancellation_reason is not null)) - and isvoid = 0 - and warningflag = 0 - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - then 1 else 0 end) as pcn_recovered_can_onstreet_carparks - -/*PCNs by Type with VDA's excluded before and included after 1st June 2021*/ - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet_carparks - -,Sum(Case - When - ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Estates - -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_CCTV - -,Sum(Case - When - zone like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_Car_Parks - -,Sum(Case - When - debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - Then 1 Else 0 End) as Flg_kpi_onstreet - - - --- Disputed pcns and by pcn type -,count(distinct Disputes.ticketserialnumber) as TotalpcnDisputed -,count(Disputes.ticketserialnumber) as TotalDisputed -/*,COUNT (distinct if (isvda = 0 and isvoid = 0 and warningflag = 0, Disputes.ticketserialnumber - )) as Dispute_pcn_Total -,COUNT (if (isvda = 0 and isvoid = 0 and warningflag = 0, Disputes.ticketserialnumber - )) as Dispute_Total - */ - -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flg_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_onstreet_carparks_disputes -,COUNT(DISTINCT(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) Flag_kpi_Estates_disputes -,COUNT(DISTINCT(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_CCTV_disputes - -,COUNT(DISTINCT(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_Car_Parks_disputes -,COUNT(DISTINCT(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') or zone not like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flag_kpi_onstreet_disputes -,COUNT(DISTINCT(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) and Disputes.ticketserialnumber is not null Then Disputes.ticketserialnumber End) ) as Flg_kpi_onstreet_disputes - -/*ETA Records with by PCN types*/ -,sum(eta_recs.appeal_rejected) as decision_appeal_rejected -,sum(eta_recs.appeal_allowed) as decision_appeal_allowed -,sum(eta_recs.appeal_dnc) as decision_appeal_dnc -,sum(eta_recs.appeal_with_direction) as decision_appeal_with_direction -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_rejected > 1 - Then eta_recs.appeal_rejected Else 0 End) as Flg_decision_appeal_rejected_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_allowed > 1 - Then eta_recs.appeal_allowed Else 0 End) as Flg_decision_appeal_allowed_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_dnc > 1 - Then eta_recs.appeal_dnc Else 0 End) as Flg_decision_appeal_dnc_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and eta_recs.appeal_with_direction > 1 - Then eta_recs.appeal_with_direction Else 0 End) as Flg_decision_appeal_with_direction_CCTV -,Sum(Case - When - debttype like 'CCTV%' - and ((isvda = 0 and cast(pcnissuedate as date) < cast('2021-06-01' as date)) or (cast(pcnissuedate as date) > cast('2021-05-31' as date)) ) - and isvoid = 0 - and warningflag = 0 - and (eta_recs.appeal_rejected > 1 or eta_recs.appeal_allowed > 1 or eta_recs.appeal_dnc > 1 or eta_recs.appeal_with_direction > 1) - Then (eta_recs.appeal_rejected + eta_recs.appeal_allowed + eta_recs.appeal_dnc + eta_recs.appeal_with_direction ) Else 0 End) as Flg_eta_decision_CCTV - - ---PCN Calculation include Where code warningflag = 0 and isvda = 0 and isvoid = 0. No Warnings NO VDA and No voided PCNs ---) and (cast(isvda as varchar) like '0') and (cast(isvoid as varchar) like '0') and (cast(warningflag as varchar) like '0') ---and (isvda = 0) and (isvoid = 0) and (warningflag = 0) - --- PCNs kpi by pcn types -,Sum(Case When (debttype not like 'CCTV%' or (zone not like 'Estates' or street_location not like '%Estate%' OR usrn not like 'Z%') ) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_onstreet_carparks -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flg_kpi_onstreet_carparks -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Estates -,Sum(Case When (debttype like 'CCTV%') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_CCTV - -,Sum(Case When (zone like 'Car Parks') and (isvda = 0) and (isvoid = 0) and (warningflag = 0) Then 1 Else 0 End) as Flag_kpi_Car_Parks - - -,Sum(Case When (isvda = 1) or (isvoid = 1) or (warningflag = 1) Then 1 Else 0 End) as Flag_total_vda_void_warning - -,Sum(Case When ((zone like 'Estates') or (street_location like '%Estate%') OR (usrn like 'Z%')) Then 1 Else 0 End) as Flag_pi_Estates -,Sum(Case When zone like 'Car Parks' Then 1 Else 0 End) as Flag_pi_Car_Parks -,Sum(Case When debttype like 'CCTV%' Then 1 Else 0 End) as Flag_pi_CCTV -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' and zone not like 'Car Parks' Then 1 Else 0 End) as Flag_pi_onstreet -,Sum(Case When debttype not like 'CCTV%' and zone not like 'Estates' and street_location not like '%Estate%' and usrn not like 'Z%' Then 1 Else 0 End) as Flag_pi_onstreet_carparks - -,Sum(Case When debttype like 'CEO' Then 1 Else 0 End) as Flag_debttype_CEO -,Sum(Case When debttype like 'CCTV Moving traffic' Then 1 Else 0 End) as Flag_debttype_CCTV_Moving_traffic -,Sum(Case When debttype like 'CCTV static' Then 1 Else 0 End) as Flag_debttype_CCTV_static -,Sum(Case When debttype like 'CCTV Bus Lane' Then 1 Else 0 End) as Flag_debttype_CCTV_Bus_Lane -,Sum(Case When debttype like 'Manual_Tickets' Then 1 Else 0 End) as Flag_debttype_Manual_Tickets - --- pcns not in kpi -,sum(case when cast(pcnissuedate as date) > cast('2021-08-09' as date) then 1 else 0 end) as af_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-08-10' as date) then 1 else 0 end) as bf_ten_aug_PCN_issue -,sum(case when cast(pcnissuedate as date) > cast('2021-05-31' as date) then 1 else 0 end) as af_first_jun_PCN_issue -,sum(case when cast(pcnissuedate as date) < cast('2021-06-01' as date) then 1 else 0 end) as bf_first_jun_PCN_issue - -,Sum(Case When isvda = 1 Then 1 Else 0 End) as Flag_total_isvda -,Sum(Case When isvoid = 1 Then 1 Else 0 End) as Flag_total_isvoid -,Sum(Case When cast(isremoval as INTEGER) = 1 Then 1 Else 0 End) as Flag_total_isremoval -,Sum(Case When warningflag = 1 Then 1 Else 0 End) as Flag_total_warningflag - -,Sum(Case When progressionstage like 'discount' Then 1 Else 0 End) as Flag_progressionstage_discount -,Sum(Case When progressionstage like 'postalgrace' Then 1 Else 0 End) as Flag_progressionstage_postalgrace -,Sum(Case When progressionstage like 'waitdvla' Then 1 Else 0 End) as Flag_progressionstage_waitdvla -,Sum(Case When progressionstage like 'readytoprint' Then 1 Else 0 End) as Flag_progressionstage_readytoprint -,Sum(Case When progressionstage like 'nto' Then 1 Else 0 End) as Flag_progressionstage_nto -,Sum(Case When progressionstage like 'full' Then 1 Else 0 End) as Flag_progressionstage_full -,Sum(Case When progressionstage like 'cc' Then 1 Else 0 End) as Flag_progressionstage_cc -,Sum(Case When progressionstage like 'nfa' Then 1 Else 0 End) as Flag_progressionstage_nfa -,Sum(Case When progressionstage like 'warningnoticesent' Then 1 Else 0 End) as Flag_progressionstage_warningnoticesent -,Sum(Case When progressionstage like 'en' Then 1 Else 0 End) as Flag_progressionstage_en -,Sum(Case When progressionstage like 'foreigncollection' Then 1 Else 0 End) as Flag_progressionstage_foreigncollection -,Sum(Case When progressionstage like 'nodr' Then 1 Else 0 End) as Flag_progressionstage_nodr -,Sum(Case When progressionstage like 'predebt' Then 1 Else 0 End) as Flag_progressionstage_predebt -,Sum(Case When progressionstage like 'nodrr' Then 1 Else 0 End) as Flag_progressionstage_nodrr -,Sum(Case When progressionstage like 'warrant' Then 1 Else 0 End) as Flag_progressionstage_warrant -,Sum(Case When progressionstage like 'warr' Then 1 Else 0 End) as Flag_progressionstage_warr -,Sum(Case When progressionstage like 'pre-debt' Then 1 Else 0 End) as Flag_progressionstage_pre_debt - - -,Sum(Case When nto_printed is not null Then 1 Else 0 End) as Flag_nto_printed -,Sum(Case When appeal_accepted is not null Then 1 Else 0 End) as Flag_appeal_accepted -,Sum(Case When arrived_in_pound is not null Then 1 Else 0 End) as Flag_arrived_in_pound -,Sum(Case When cancellation_reversed is not null Then 1 Else 0 End) as Flag_cancellation_reversed -,Sum(Case When cc_printed is not null Then 1 Else 0 End) as Flag_cc_printed -,Sum(Case When drr is not null Then 1 Else 0 End) as Flag_drr -,Sum(Case When en_printed is not null Then 1 Else 0 End) as Flag_en_printed -,Sum(Case When hold_released is not null Then 1 Else 0 End) as Flag_hold_released -,Sum(Case When dvla_response is not null Then 1 Else 0 End) as Flag_dvla_response -,Sum(Case When dvla_request is not null Then 1 Else 0 End) as Flag_dvla_request -,Sum(Case When full_rate_uplift is not null Then 1 Else 0 End) as Flag_full_rate_uplift -,Sum(Case When hold_until is not null Then 1 Else 0 End) as Flag_hold_until -,Sum(Case When lifted_at is not null Then 1 Else 0 End) as Flag_lifted_at -,Sum(Case When lifted_by is not null Then 1 Else 0 End) as Flag_lifted_by -,Sum(Case When loaded is not null Then 1 Else 0 End) as Flag_loaded -,Sum(Case When nor_sent is not null Then 1 Else 0 End) as Flag_nor_sent -,Sum(Case When notice_held is not null Then 1 Else 0 End) as Flag_notice_held -,Sum(Case When ofr_printed is not null Then 1 Else 0 End) as Flag_ofr_printed -,Sum(Case When pcn_printed is not null Then 1 Else 0 End) as Flag_pcn_printed -,Sum(Case When reissue_nto_requested is not null Then 1 Else 0 End) as Flag_reissue_nto_requested -,Sum(Case When reissue_pcn is not null Then 1 Else 0 End) as Flag_reissue_pcn -,Sum(Case When set_back_to_pre_cc_stage is not null Then 1 Else 0 End) as Flag_set_back_to_pre_cc_stage -,Sum(Case When vehicle_released_for_auction is not null Then 1 Else 0 End) as Flag_vehicle_released_for_auction -,Sum(Case When warrant_issued is not null Then 1 Else 0 End) as Flag_warrant_issued -,Sum(Case When warrant_redistributed is not null Then 1 Else 0 End) as Flag_warrant_redistributed -,Sum(Case When warrant_request_granted is not null Then 1 Else 0 End) as Flag_warrant_request_granted -,Sum(Case When ad_hoc_vq4_request is not null Then 1 Else 0 End) as Flag_ad_hoc_vq4_request -,Sum(Case When paper_vq5_received is not null Then 1 Else 0 End) as Flag_paper_vq5_received -,Sum(Case When pcn_extracted_for_buslane is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_buslane -,Sum(Case When pcn_extracted_for_pre_debt is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_pre_debt -,Sum(Case When pcn_extracted_for_collection is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_collection -,Sum(Case When pcn_extracted_for_drr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_drr -,Sum(Case When pcn_extracted_for_cc is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_cc -,Sum(Case When pcn_extracted_for_nto is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_nto -,Sum(Case When pcn_extracted_for_print is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_print -,Sum(Case When warning_notice_extracted_for_print is not null Then 1 Else 0 End) as Flag_warning_notice_extracted_for_print -,Sum(Case When pcn_extracted_for_ofr is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_ofr -,Sum(Case When pcn_extracted_for_warrant_request is not null Then 1 Else 0 End) as Flag_pcn_extracted_for_warrant_request -,Sum(Case When pre_debt_new_debtor_details is not null Then 1 Else 0 End) as Flag_pre_debt_new_debtor_details - -FROM pcnfoidetails_pcn_foi_full -left join Disputes on Disputes.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = Disputes.import_date -left join regkep on regkep.ticketserialnumber = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = regkep.import_date -left join pcn_audit on pcn_audit.ticket_ref = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_audit.import_date -left join pcn_recovery_reason on pcn_recovery_reason.cancel_pcn = pcnfoidetails_pcn_foi_full.pcn and pcnfoidetails_pcn_foi_full.import_date = pcn_recovery_reason.import_date -left join eta_recs on eta_recs.etar_pcn = pcnfoidetails_pcn_foi_full.pcn - -WHERE pcnfoidetails_pcn_foi_full.import_date = (SELECT max(pcnfoidetails_pcn_foi_full.import_date) from pcnfoidetails_pcn_foi_full) and pcnissuedate > current_date - interval '34' month --Last 36 months from todays date -group by concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') -,cast(concat(Cast(extract(year from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month) as varchar(4)),'-',cast(extract(month from pcnfoidetails_pcn_foi_full.pcnissuedate + interval '3' month)as varchar(2)), '-01') as Date) ---,cast(concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as date) ---,to_date(Substr(pcnissuedate, 1,7), 'Y-m') ---,cast(Substr(pcnissuedate, 1,7) as date) -, zone -, (Case When zone like 'Estates' Then usrn Else zone End) -, usrn -, contraventioncode -, contraventionsuffix -, holdreason -, bailiff -, eta_appealgrounds -, eta_outcome -, pcnfoidetails_pcn_foi_full.import_year -, pcnfoidetails_pcn_foi_full.import_month -, pcnfoidetails_pcn_foi_full.import_day -, pcnfoidetails_pcn_foi_full.import_date -- import_date ---, current_timestamp() as ImportDateTime -order by concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') desc -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_ic": AmazonS3Liberator_pcn_ic_node1631812698045, - "pcnfoidetails_pcn_foi_full": S3bucketpcnfoidetails_pcn_foi_full_node1, - "liberator_pcn_tickets": AmazonS3liberator_pcn_tickets_node1637153316033, - "liberator_pcn_audit": AmazonS3liberatorrawzoneliberator_pcn_audit_node1638297295740, - "eta_decision_records": AmazonS3parkingrawzoneparking_eta_decision_records_node1645806323578, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-" + environment + "-refined-zone/parking/liberator/parking_kpi_gds_summary/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="parking_kpi_gds_summary", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/scripts/jobs/parking/parking_open_pcns_vrms_linked_cancelled_ringer.py b/scripts/jobs/parking/parking_open_pcns_vrms_linked_cancelled_ringer.py deleted file mode 100644 index 980e43f47..000000000 --- a/scripts/jobs/parking/parking_open_pcns_vrms_linked_cancelled_ringer.py +++ /dev/null @@ -1,138 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -Note: python file name should be the same as the table name -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_open_pcns_vrms_linked_cancelled_ringer" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/*Open PCNs linked to VRMs cancelled due to being a Ringer or Clone -Created: 15/02/2024 -11/03/2024 - added extracted post codes and bailiff fields to output - -*/ -With cancelled_vrm as ( - Select distinct vrm as canx_vrm - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - where import_date = ( - select max(import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and ( - upper(cancellationgroup) like '%RINGER%' - or upper(cancellationreason) like '%RINGER%' - ) -) -Select cancelled_vrm.*, - concat( - substr(Cast(pcnissuedate as varchar(10)), 1, 7), - '-01' - ) as MonthYear, - progressionstage, - debttype, - pcn, - pcnissuedate, - pcnissuedatetime, - street_location, - whereonlocation, - zone, - usrn, - contraventioncode, - contraventionsuffix, - vrm, - vehiclemake, - vehiclemodel, - vehiclecolour, - corresp_dispute_flag, - registered_keeper_address, - current_ticket_address - /*Registered extracted post codes*/ -, - case - when length( - regexp_extract( - registered_keeper_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) - ) = 0 - or regexp_extract( - registered_keeper_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) is null - or regexp_extract( - registered_keeper_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) like '' - or regexp_extract( - registered_keeper_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) like ' ' then 'No Address' - else regexp_extract( - registered_keeper_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) - end as reg_add_extracted_post_code - /*Current extracted post codes*/ -, - case - when length( - regexp_extract( - current_ticket_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) - ) = 0 - or regexp_extract( - current_ticket_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) is null - or regexp_extract( - current_ticket_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) like '' - or regexp_extract( - current_ticket_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) like ' ' then 'No Address' - else regexp_extract( - current_ticket_address, - '([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})' - ) - end as curr_add_extracted_post_code, - bailiff - /*for partiion*/ -, - format_datetime(current_date, 'yyyy') AS import_year, - format_datetime(current_date, 'MM') AS import_month, - format_datetime(current_date, 'dd') AS import_day, - format_datetime(current_date, 'yyyyMMdd') AS import_date -from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - left join cancelled_vrm on cancelled_vrm.canx_vrm = pcnfoidetails_pcn_foi_full.vrm -where pcnfoidetails_pcn_foi_full.import_date = ( - select max(pcnfoidetails_pcn_foi_full.import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and ( - pcnfoidetails_pcn_foi_full.pcn_canx_date is null - and pcnfoidetails_pcn_foi_full.pcn_casecloseddate is null - ) - and cancelled_vrm.canx_vrm is not null - and pcnfoidetails_pcn_foi_full.warningflag = 0 -order by cancelled_vrm.canx_vrm, - progressionstage, - debttype, - pcnissuedatetime desc, - pcn, - street_location -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_pcn_create_event_log.py b/scripts/jobs/parking/parking_pcn_create_event_log.py deleted file mode 100644 index 916489218..000000000 --- a/scripts/jobs/parking/parking_pcn_create_event_log.py +++ /dev/null @@ -1,178 +0,0 @@ -import sys -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue.dynamicframe import DynamicFrame - -from scripts.helpers.helpers import get_glue_env_var, create_pushdown_predicate -environment = get_glue_env_var("environment") - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - -SqlQuery0 = ''' -With Event_logFULL AS - (SELECT ticket_ref, - CASE - When audit_message like '%NTO printed%' Then 'NTO printed' - When audit_message like '%Appeal accepted%' Then 'Appeal accepted' - When audit_message like '%Arrived in pound%' Then 'Arrived in pound' - When audit_message like '%Cancellation reversed%' Then 'Cancellation reversed' - When audit_message like '%CC printed%' Then 'CC printed' - When audit_message like '%Debt Registration request%' Then 'Debt Registration request' - When audit_message like '%EN printed%' Then 'EN printed' - When audit_message like '%Hold released%' Then 'Hold released' - When audit_message like '%DVLA Response%' Then 'DVLA Response' - When audit_message like '%DVLA Request%' Then 'DVLA Request' - When audit_message like '%Full rate uplift%' Then 'Full rate uplift' - When audit_message like '%Hold until%' Then 'Hold until' - When audit_message like '%Lifted at%' Then 'Lifted at' - When audit_message like '%Lifted by%' Then 'Lifted by' - When audit_message like '%Loaded%' Then 'Loaded' - When audit_message like '%NoR sent%' Then 'NoR sent' - When audit_message like '%Notice held%' Then 'Notice held' - When audit_message like '%OFR printed%' Then 'OFR printed' - When audit_message like '%PCN printed%' Then 'PCN printed' - When audit_message like '%Re-issue NTO requested%' Then 'Re-issue NTO requested' - When audit_message like '%Re-issue PCN%' Then 'Re-issue PCN' - When audit_message like '%Set back to pre-CC stage%' Then 'Set back to pre-CC stage' - When audit_message like '%Vehicle released for auction%' Then 'Vehicle released for auction' - When audit_message like '%Warrant issued%' Then 'Warrant issued' - When audit_message like '%Warrant redistributed%' Then 'Warrant redistributed' - When audit_message like '%Warrant request granted%' Then 'Warrant request granted' - When audit_message like '%Ad-hoc VQ4 request%' Then 'Ad-hoc VQ4 request' - When audit_message like '%Paper VQ5 received%' Then 'Paper VQ5 received' - When audit_message like '%Buslane PCN extracted for Enforcement notice%' - Then 'Buslane PCN extracted for Enforcement notice' - When audit_message like '%PCN extracted for pre-debt screening%' Then 'PCN extracted for pre-debt screening' - When audit_message like '%PCN extracted for collection%' Then 'PCN extracted for collection' - When audit_message like '%PCN extracted for Debt Registration request%' - Then 'PCN extracted for Debt Registration request' - When audit_message like '%PCN extracted for CC%' Then 'PCN extracted for CC' - When audit_message like '%PCN extracted for NTO%' Then 'PCN extracted for NTO' - When audit_message like '%PCN extracted for print%' Then 'PCN extracted for print' - When audit_message like '%Warning notice extracted for print%' Then 'Warning notice extracted for print' - When audit_message like '%PCN extracted for OfR%' Then 'PCN extracted for OfR' - When audit_message like '%PCN extracted for Warrant request%' Then 'PCN extracted for Warrant request' - When audit_message like '%Pre-debt: New debtor details%' Then 'Pre-debt: New debtor details' - else audit_message - END as audit_message, record_date_time, event_date_time, import_year, import_month, import_day, import_date - FROM liberator_pcn_audit - WHERE import_date = (SELECT MAX(import_date) FROM liberator_pcn_audit)), - -Event_Row_no as (select *, - ROW_NUMBER() OVER ( PARTITION BY ticket_ref, audit_message, import_year, import_month, import_day, import_date - ORDER BY ticket_ref,audit_message,event_date_time DESC) row_num - FROM Event_logFULL), - -EVENT_LOG as (Select ticket_ref, - MIN(Case When audit_message = 'NTO printed' Then event_date_time END) AS NTO_printed, - MIN(Case When audit_message = 'Appeal accepted' Then event_date_time END) AS Appeal_accepted, - MIN(Case When audit_message = 'Arrived in pound' Then event_date_time END) AS Arrived_in_pound, - MIN(Case When audit_message = 'Cancellation reversed' Then event_date_time END) AS Cancellation_reversed, - - MIN(Case When audit_message = 'CC printed' Then event_date_time END) AS CC_printed, - MIN(Case When audit_message = 'Debt Registration request' Then event_date_time END) AS DRR, - MIN(Case When audit_message = 'EN printed' Then event_date_time END) AS EN_printed, - MIN(Case When audit_message = 'Hold released' Then event_date_time END) AS Hold_released, - - MIN(Case When audit_message = 'DVLA Response' Then event_date_time END) AS DVLA_Response, - MIN(Case When audit_message = 'DVLA Request' Then event_date_time END) AS DVLA_Request, - MIN(Case When audit_message = 'Full rate uplift' Then event_date_time END) AS Full_rate_uplift, - MIN(Case When audit_message = 'Hold until' Then event_date_time END) AS Hold_until, - - MIN(Case When audit_message = 'Lifted at' Then event_date_time END) AS Lifted_at, - MIN(Case When audit_message = 'Lifted by' Then event_date_time END) AS Lifted_by, - MIN(Case When audit_message = 'Loaded' Then event_date_time END) AS Loaded, - MIN(Case When audit_message = 'NoR sent' Then event_date_time END) AS NoR_sent, - - MIN(Case When audit_message = 'Notice held' Then event_date_time END) AS Notice_held, - MIN(Case When audit_message = 'OFR printed' Then event_date_time END) AS OFR_printed, - MIN(Case When audit_message = 'PCN printed' Then event_date_time END) AS PCN_printed, - MIN(Case When audit_message = 'Re-issue NTO requested' Then event_date_time END) AS ReIssue_NTO_requested, - - MIN(Case When audit_message = 'Re-issue PCN' Then event_date_time END) AS ReIssue_PCN, - MIN(Case When audit_message = 'Set back to pre-CC stage' Then event_date_time END) AS Set_back_to_pre_CC_stage, - MIN(Case When audit_message = 'Vehicle released for auction' Then event_date_time END) AS Vehicle_released_for_auction, - MIN(Case When audit_message = 'Warrant issued' Then event_date_time END) AS Warrant_issued, - - MIN(Case When audit_message = 'Warrant redistributed' Then event_date_time END) AS Warrant_redistributed, - MIN(Case When audit_message = 'Warrant request granted' Then event_date_time END) AS Warrant_request_granted, - MIN(Case When audit_message = 'Ad-hoc VQ4 request' Then event_date_time END) AS Ad_hoc_VQ4_request, - MIN(Case When audit_message = 'Paper VQ5 received' Then event_date_time END) AS Paper_VQ5_received, - - MIN(Case When audit_message = 'Buslane PCN extracted for Enforcement notice' Then event_date_time END) - AS PCN_extracted_for_BusLane, - MIN(Case When audit_message = 'PCN extracted for pre-debt screening' Then event_date_time END) - AS PCN_extracted_for_pre_debt, - MIN(Case When audit_message = 'PCN extracted for collection' Then event_date_time END) - AS PCN_extracted_for_collection, - MIN(Case When audit_message = 'PCN extracted for Debt Registration request' Then event_date_time END) - AS PCN_extracted_for_DRR, - - MIN(Case When audit_message = 'PCN extracted for CC' Then event_date_time END) AS PCN_extracted_for_CC, - MIN(Case When audit_message = 'PCN extracted for NTO' Then event_date_time END) AS PCN_extracted_for_NTO, - MIN(Case When audit_message = 'PCN extracted for print' Then event_date_time END) AS PCN_extracted_for_print, - MIN(Case When audit_message = 'Warning notice extracted for print' Then event_date_time END) - AS Warning_notice_extracted_for_print, - - MIN(Case When audit_message = 'PCN extracted for OfR' Then event_date_time END) AS PCN_extracted_for_OfR, - MIN(Case When audit_message = 'PCN extracted for Warrant request' Then event_date_time END) AS PCN_extracted_for_Warrant_request, - MIN(Case When audit_message = 'Pre-debt: New debtor details' Then event_date_time END) AS Pre_debt_New_debtor_details, - import_year, import_month, import_day, import_date -from Event_Row_no - WHERE row_num = 1 - AND audit_message IN - ('NTO printed', 'Appeal accepted', 'Arrived in pound','Cancellation reversed', 'CC printed', 'Debt Registration request', - 'EN printed','Hold released','DVLA Response','DVLA Request','Full rate uplift','Hold until','Lifted at','Lifted by','Loaded', - 'NoR sent','Notice held','OFR printed','PCN printed','Re-issue NTO requested','Re-issue PCN', - 'Set back to pre-CC stage','Vehicle released for auction','Warrant issued','Warrant redistributed','Warrant request granted', - 'Ad-hoc VQ4 request','Paper VQ5 received','Buslane PCN extracted for Enforcement notice','PCN extracted for pre-debt screening', - 'PCN extracted for collection','PCN extracted for Debt Registration request','PCN extracted for CC','PCN extracted for NTO', - 'PCN extracted for print','Warning notice extracted for print','PCN extracted for OfR','PCN extracted for Warrant request', - 'Pre-debt: New debtor details') -Group by ticket_ref, import_year, import_month, import_day, import_date) - -SELECT A.*, current_timestamp() as ImportDatTime -FROM EVENT_LOG as A - -''' - -## @params: [JOB_NAME] -args = getResolvedOptions(sys.argv, ['JOB_NAME']) - -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args['JOB_NAME'], args) -## @type: DataSource -## @args: [database = "dataplatform-" + environment + "-liberator-raw-zone", table_name = "liberator_pcn_audit", transformation_ctx = "DataSource0"] -## @return: DataSource0 -## @inputs: [] -DataSource0 = glueContext.create_dynamic_frame.from_catalog( - database = "dataplatform-" + environment + "-liberator-raw-zone", - table_name = "liberator_pcn_audit", - transformation_ctx = "DataSource0", - push_down_predicate=create_pushdown_predicate("import_date", 7), - ) -## @type: SqlCode -## @args: [sqlAliases = {"liberator_pcn_audit": DataSource0}, sqlName = SqlQuery0, transformation_ctx = "Transform0"] -## @return: Transform0 -## @inputs: [dfc = DataSource0] -Transform0 = sparkSqlQuery(glueContext, query = SqlQuery0, mapping = {"liberator_pcn_audit": DataSource0}, transformation_ctx = "Transform0") -## @type: DataSink -## @args: [connection_type = "s3", catalog_database_name = "dataplatform-" + environment + "-liberator-refined-zone", format = "glueparquet", connection_options = {"path": "s3://dataplatform-" + environment + "-refined-zone/parking/liberator/PCNFOIDetails_PCN_EVENT_LOG/", "partitionKeys": ["import_year" ,"import_month" ,"import_day" ,"import_date"], "enableUpdateCatalog":true, "updateBehavior":"UPDATE_IN_DATABASE"}, catalog_table_name = "PCNFOIDetails_PCN_EVENT_LOG", transformation_ctx = "DataSink0"] -## @return: DataSink0 -## @inputs: [frame = Transform0] -DataSink0 = glueContext.getSink(path = "s3://dataplatform-" + environment + "-refined-zone/parking/liberator/PCNFOIDetails_PCN_EVENT_LOG/", connection_type = "s3", updateBehavior = "UPDATE_IN_DATABASE", partitionKeys = ["import_year","import_month","import_day","import_date"], enableUpdateCatalog = True, transformation_ctx = "DataSink0") -DataSink0.setCatalogInfo(catalogDatabase = "dataplatform-" + environment + "-liberator-refined-zone",catalogTableName = "PCNFOIDetails_PCN_EVENT_LOG") -DataSink0.setFormat("glueparquet") -DataSink0.writeFrame(Transform0) - -job.commit() \ No newline at end of file diff --git a/scripts/jobs/parking/parking_pcn_denormalisation.py b/scripts/jobs/parking/parking_pcn_denormalisation.py deleted file mode 100644 index b3f79a293..000000000 --- a/scripts/jobs/parking/parking_pcn_denormalisation.py +++ /dev/null @@ -1,371 +0,0 @@ -import sys -import time -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue import DynamicFrame - -from scripts.helpers.helpers import get_glue_env_var, create_pushdown_predicate -environment = get_glue_env_var("environment") - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - """ - Define a function to execute SQL queries using Spark SQL - Register each DynamicFrame as a temporary view to use in the SQL query - Execute the SQL query - Convert the result back to a DynamicFrame - """ - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) - -# Initialize Spark and Glue contexts -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) - -# Script generated for node liberator_pcn_payments -# Load various datasets from the Glue Catalog, applying a pushdown predicate to filter data based on 'import_date' -# Each dataset corresponds to a different aspect of PCN data, such as payments, bailiff actions, tickets, etc. -start_time = time.time() - -liberator_pcn_payments_node1624544303612 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_payments", - transformation_ctx="liberator_pcn_payments_node1624544303612", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - - -# Repeat the process for the other datasets -# Script generated for node Amazon S3 -AmazonS3_node1632737645295 = glueContext.create_dynamic_frame.from_catalog( - database="parking-raw-zone", - table_name="calendar", - transformation_ctx="AmazonS3_node1632737645295", -) - -# Script generated for node liberator_pcn_bailiff -liberator_pcn_bailiff_node1624546972989 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_bailiff", - transformation_ctx="liberator_pcn_bailiff_node1624546972989", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 -AmazonS3_node1632316748934 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_ic", - transformation_ctx="AmazonS3_node1632316748934", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node liberator_pcn_tickets -liberator_pcn_tickets_node1624456646816 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="liberator_pcn_tickets_node1624456646816", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node Amazon S3 -AmazonS3_node1625039493203 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - table_name="pcnfoidetails_pcn_event_log", - transformation_ctx="AmazonS3_node1625039493203", - push_down_predicate=create_pushdown_predicate("import_date",1) -) - -# Script generated for node liberator_pcn_warrant_redistribution -liberator_pcn_warrant_redistribution_node1624611344521 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_warrant_redistribution", - transformation_ctx="liberator_pcn_warrant_redistribution_node1624611344521", - push_down_predicate=create_pushdown_predicate("import_date",1) - ) -) - -# Script generated for node liberator_pcn_appeals -liberator_pcn_appeals_node1624617107363 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_appeals", - transformation_ctx="liberator_pcn_appeals_node1624617107363", - push_down_predicate=create_pushdown_predicate("import_date",1) -) -print(f"loading data {(time.time() - start_time)/60:.2f} minutes") - -# Script generated for node ApplyMapping -# Define a SQL query to denormalise PCN data, combining information from various sources into a single record per PCN -start_time = time.time() -SqlQuery0 = """ -/************************************************************************************************************************* -Parking_PCN_Denormalisation - -The SQL builds a PCN de-normalisation table, single PCN record with all data that is useful - -09/07/2021 - Create SQL. -13/07/2021 - add additional code to ensure latest records are collected -23/07/2012 - add code to identify if the PCN was issued in the last 30 days -07/09/2021 - add Cancel by -22/09/2021 - Add code to identify if Corresp has been sent -27/09/2021 - Add code to identify Current & Previous year and Fin Year -20/10/2021 - add issue date / time -*************************************************************************************************************************/ - -/*** Because of duplication index on the LATEST record ***/ -WITH ETA_Dates as (SELECT *, ROW_NUMBER() OVER ( PARTITION BY ticketserialnumber - ORDER BY ticketserialnumber, decisionreceived DESC) row_num -FROM liberator_pcn_appeals as A -WHERE import_date = (Select MAX(import_date) from liberator_pcn_appeals)), - -/*** Bailiff Records ***/ -Bailiff AS (SELECT *, ROW_NUMBER() OVER ( PARTITION BY ticketreference - ORDER BY ticketreference, warrantissuedate DESC) row_num -FROM liberator_pcn_bailiff as A -WHERE import_date = (Select MAX(import_date) from liberator_pcn_bailiff)), - -Calendar as ( -Select * From calendar -WHERE import_date = (Select MAX(import_date) from calendar)), - -CalendarMAX as ( -Select MAX(fin_year) as Max_Fin_Year From calendar -WHERE import_date = (Select MAX(import_date) from calendar)), - -/*** Identify those PCNs with Disputed Corresp ***/ -Corresp as ( - SELECT DISTINCT - ticketserialnumber, import_date - FROM liberator_pcn_ic - where import_Date = (Select MAX(import_date) from liberator_pcn_ic) - AND date_received != '' AND response_generated_at != '' - AND char_length(ticketserialnumber) = 10 - AND Serviceable IN ('Challenges','Key worker','Removals','TOL','Charge certificate','Representations')), - -/*** Identify those PCNs with Keyworker Disputed Corresp ***/ -KeyWorker_Dispute as ( - SELECT DISTINCT - ticketserialnumber, import_date - FROM liberator_pcn_ic - where import_Date = (Select MAX(import_date) from liberator_pcn_ic) - AND date_received != '' AND response_generated_at != '' - AND char_length(ticketserialnumber) = 10 - AND Serviceable IN ('Key worker')), - -pcn_warrant_redistribution as ( - SELECT *, ROW_NUMBER() OVER ( PARTITION BY PCN - ORDER BY PCN, processedon DESC) row_num - FROM liberator_pcn_warrant_redistribution - WHERE import_date = (Select MAX(import_date) from liberator_pcn_warrant_redistribution)) - -/*** Output the data ***/ -SELECT A.ticketserialnumber AS PCN, - CAST(A.contraventiondateandtime as date) AS PCNIssueDate, - CAST(A.contraventiondateandtimewith as timestamp) AS PCNIssueDateTime, - CASE When A.whencancelled !='' THEN CAST(A.whencancelled as date) ELSE CAST(NULL as date) END AS PCN_Canx_Date, - A.cancellationgroup AS CancellationGroup, - replace(replace(A.cancellationreason, '\r',''), '\n','') AS CancellationReason, - CASE When A.whenpaid !='' THEN CAST(A.whenpaid as date) ELSE CAST(NULL as date) END AS PCN_CaseClosedDate, - - -- Issued Location - assoclocation AS Street_Location, - osoppwhere AS WhereOnLocation, - cpzname AS Zone, - streetusrn AS USRN, - contraventioncode,contraventionsuffix, - - CASE When tickettype = 'hht' Then 'CEO' Else tickettype End AS DebtType, - -- Vehicle Details - A.vrm AS VRM, - make AS VehicleMake, - model AS VehicleModel, - colour AS VehicleColour, - -- CEO Details - shoulder_number AS CEO, - issuingdeviceid AS CEODevice, - - -- Set Flags - CASE - When CAST(A.contraventiondateandtime as date) <= date_Add(current_date,-30) Then 0 - Else 1 - END AS Current_30_Day_Flag, -- MRB 23-07-2021 - - CASE When vehicledrivenaway = 'Yes' Then 1 Else 0 END AS IsVDA, - CASE When A.cancellationreason like '%Void%' Then 1 Else 0 END AS IsVoid, - remove AS IsRemoval, - driverseen, - allwindows, - parkedonfootway, - doctor, - CASE - When contraventioncode like 'W%' Then 1 - When progressionstage = 'warningnoticesent' Then 1 - ELSE 0 - END as WarningFlag, - - -- Progression Stage(s) - progressionstage AS ProgressionStage, - nextprogressionstage AS NextProgressionStage, - nextprogressionstagestarts AS NextProgressionStageStarts, - holdreason AS HoldReason, - - -- Liberator Payment Details - B.total_amount AS Lib_Initial_Debt_Amount, - CASE - WHEN B.payment_received != '0.000' THEN CAST(-CAST(B.payment_received as double) as varchar(10)) - ELSE '0' - END AS Lib_Payment_Received, - - CASE - WHEN B.write_off_amount != '0.000' THEN CAST(-CAST(B.write_off_amount as double) as varchar(10)) - ELSE '0' - END AS Lib_Write_Off_Amount, - - B.payment_void AS Lib_Payment_Void, - B.payment_method AS Lib_Payment_Method, - B.payment_reference AS Lib_Payment_Ref, - - -- Warrant redistribution - C.partyfrom as Baliff_From, - C.redistributedto as Bailiff_To, - C.processedon as Bailiff_ProcessedOn, - C.redistributionreason AS Bailiff_RedistributionReason, - - -- Bailiff PCN Data - D.organisation as Bailiff, - D.warrantissuedate, - D.allocation, - - -- ETA Appeal Details - E.datenotified as ETA_datenotified, - E.packsubmittedon as ETA_packsubmittedon, - E.evidencedate as ETA_evidencedate, - E.adjudicationdate as ETA_adjudicationdate, - E.appealgrounds as ETA_appealgrounds, - E.decisionreceived as ETA_decisionreceived, - E.outcome as ETA_outcome, - E.packsubmittedby as ETA_packsubmittedby, - - /*** Add Cancelled By ***/ - A.cancelledby, - - /*** Owner/Keeper address ***/ - registered_keeper_address, current_ticket_address, - - /*** Dispute Corresp registered ***/ - CASE - When G.ticketserialnumber is not NULL Then 1 - Else 0 - END as Corresp_Dispute_Flag, - - /*** Keyworker Dispute Corresp registered ***/ - CASE - When I.ticketserialnumber is not NULL Then 1 - Else 0 - END as Keyworker_Corresp_Dispute_Flag, - - /*** identify the current year ***/ - CASE - When H.Fin_Year = (Select Max_Fin_Year From CalendarMAX) Then 'Current' - When H.Fin_Year = (Select CAST(Cast(Max_Fin_Year as int)-1 as varchar(4)) From CalendarMAX) Then 'Previous' - Else '' - END as Fin_Year_Flag, - - H.Fin_Year, - - -- Event Log - F.*, - - current_timestamp() as ImportDateTime, - - replace(cast(current_date() as string),'-','') as import_date, - - cast(A.import_year as string) as import_year, - cast(A.import_month as string) as import_month, - cast(A.import_day as string) as import_day - -FROM liberator_pcn_tickets as A - -LEFT JOIN liberator_pcn_payments as B ON A.ticketserialnumber = B.ticket_ref AND -(A.import_date = B.import_date) - -LEFT JOIN pcn_warrant_redistribution as C ON A.ticketserialnumber = C.pcn AND -(A.import_date = C.import_date) AND C.row_num = 1 - -LEFT JOIN Bailiff as D ON A.ticketserialnumber = D.ticketreference AND -(A.import_date = D.import_date) AND D.row_num = 1 - -LEFT JOIN ETA_Dates as E ON A.ticketserialnumber = E.ticketserialnumber AND -(A.import_date = E.import_date) AND E.row_num = 1 - -LEFT JOIN pcnfoidetails_pcn_event_log as F ON A.ticketserialnumber = F.ticket_ref AND -(A.import_date = F.import_date) - -LEFT JOIN Corresp as G ON A.ticketserialnumber = G.ticketserialnumber AND -(A.import_date = G.import_date) - -LEFT JOIN Calendar as H ON CAST(A.contraventiondateandtime as date) - = cast(substr(H.date, 1, 10) as date) - -LEFT JOIN KeyWorker_Dispute as I ON A.ticketserialnumber = I.ticketserialnumber AND -(A.import_date = I.import_date) - -WHERE A.ticketserialnumber not IN ('QZ01017688','QZ08427983','QZ99999990','QZ00887560') AND - A.import_Date = (Select MAX(import_date) from liberator_pcn_tickets) -""" - -# Execute the SQL query defined above, passing the loaded DynamicFrames as mappings to be used in the query -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_payments": liberator_pcn_payments_node1624544303612, - "calendar": AmazonS3_node1632737645295, - "liberator_pcn_bailiff": liberator_pcn_bailiff_node1624546972989, - "liberator_pcn_ic": AmazonS3_node1632316748934, - "liberator_pcn_tickets": liberator_pcn_tickets_node1624456646816, - "pcnfoidetails_pcn_event_log": AmazonS3_node1625039493203, - "liberator_pcn_warrant_redistribution": liberator_pcn_warrant_redistribution_node1624611344521, - "liberator_pcn_appeals": liberator_pcn_appeals_node1624617107363, - }, - transformation_ctx="ApplyMapping_node2", -) -print(f"transform and mapping based on SQL query {(time.time() - start_time)/60:.2f} minutes") - -# Script generated for node PCN_DeNormalisation -# Configure the output sink to write the transformed data to an S3 bucket in Parquet format -# The data is partitioned by import date components for efficient storage and query performance -start_time = time.time() -PCN_DeNormalisation_node3 = glueContext.getSink( - path="s3://dataplatform-" + environment + "-refined-zone/parking/liberator/PCNFOIDetails_PCN_FOI_FULL/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="PCN_DeNormalisation_node3", -) -# Set catalog information for the output data, specifying the database and table name in the Glue Data Catalog -PCN_DeNormalisation_node3.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="PCNFOIDetails_PCN_FOI_FULL", -) -# spefify the output as parquet format -PCN_DeNormalisation_node3.setFormat("glueparquet") -# Write the transformed data frame to the specified S3 path -PCN_DeNormalisation_node3.writeFrame(ApplyMapping_node2) -print(f"writing to S3 bucket {(time.time() - start_time)/60:.2f} minutes") - -job.commit() diff --git a/scripts/jobs/parking/parking_pcn_dvla_response_no_address.py b/scripts/jobs/parking/parking_pcn_dvla_response_no_address.py deleted file mode 100644 index 19620de29..000000000 --- a/scripts/jobs/parking/parking_pcn_dvla_response_no_address.py +++ /dev/null @@ -1,181 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -Note: python file name should be the same as the table name -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "parking_pcn_dvla_response_no_address" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/* - All VRMs with PCNs response from DVLA has no address still open and not due to be written off - - Criteria: - All PCNs issued - and is not void - and does not have a warning flag - and is not vda and pcn issue date before '2021-06-01' or can be vda after pcn issue date '2021-05-31' - and has had a dvla_request - and has had a dvla_response - and registered_keeper_address is not null - and has not had a PCN cancelled date - and has not had a pcn_casecloseddate - and next progression stage not like 'WRITEOFF' - - 11/07/2024 - Created - - */ -with no_resp_ceo as ( - Select distinct vrm as vrm_ceo, - count(distinct pcn) as num_pcns_ceo --, LISTAGG(pcn, ' ') WITHIN GROUP (ORDER BY vrm) as vl_multi_pcns_ceo -, - array_join( - array_distinct(array_agg(pcnfoidetails_pcn_foi_full.pcn)), - -- aggregate that collects distinct values into an array - ' | ' -- delimiter - ) as vl_multi_pcns_ceo - FROM "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - WHERE pcnfoidetails_pcn_foi_full.import_date = ( - SELECT max(pcnfoidetails_pcn_foi_full.import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and isvoid = 0 - and warningflag = 0 - and ( - ( - isvda = 0 - and cast(pcnissuedate as date) < cast('2021-06-01' as date) - ) - or ( - cast(pcnissuedate as date) > cast('2021-05-31' as date) - ) - ) - and dvla_request is not null - and dvla_response is not null - and registered_keeper_address = '' - and pcn_canx_date is null - and pcn_casecloseddate is null - and upper(debttype) like 'CEO' - and upper(nextprogressionstage) not like 'WRITEOFF' --and cast(pcnissuedate as date) > cast('2019-12-31' as date) and cast(pcnissuedate as date) < cast('2024-01-01' as date)--Parking Tickets issued between 01/01/2020 and 31/12/2023. - group by vrm - order by vrm_ceo -), -no_resp_cctv as ( - Select distinct vrm as vrm_cctv, - count(distinct pcn) as num_pcns_cctv --, LISTAGG(pcn, ' ') WITHIN GROUP (ORDER BY vrm) as vl_multi_pcns_cctv -, - array_join( - array_distinct(array_agg(pcnfoidetails_pcn_foi_full.pcn)), - -- aggregate that collects distinct values into an array - ' | ' -- delimiter - ) as vl_multi_pcns_cctv - FROM "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - WHERE pcnfoidetails_pcn_foi_full.import_date = ( - SELECT max(pcnfoidetails_pcn_foi_full.import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and isvoid = 0 - and warningflag = 0 - and ( - ( - isvda = 0 - and cast(pcnissuedate as date) < cast('2021-06-01' as date) - ) - or ( - cast(pcnissuedate as date) > cast('2021-05-31' as date) - ) - ) - and dvla_request is not null - and dvla_response is not null - and registered_keeper_address = '' - and pcn_canx_date is null - and pcn_casecloseddate is null - and upper(debttype) in ( - 'CCTV MOVING TRAFFIC', - 'CCTV STATIC', - 'CCTV BUS LANE' - ) - and upper(nextprogressionstage) not like 'WRITEOFF' --and cast(pcnissuedate as date) > cast('2019-12-31' as date) and cast(pcnissuedate as date) < cast('2024-01-01' as date)--Parking Tickets issued between 01/01/2020 and 31/12/2023. - group by vrm - order by vrm -) -- summary sent to DRT -Select distinct pcnfoidetails_pcn_foi_full.vrm, - min( - cast( - substr(Cast(pcnissuedate as varchar(10)), 1, 10) as date - ) - ) as min_pcnissuedate, - max( - cast( - substr(Cast(pcnissuedate as varchar(10)), 1, 10) as date - ) - ) as max_pcnissuedate, - count(distinct pcn) as num_pcns_all --, LISTAGG(pcn, ' ') WITHIN GROUP (ORDER BY vrm) as vl_multi_pcns_all -, - array_join( - array_distinct(array_agg(pcnfoidetails_pcn_foi_full.pcn)), - -- aggregate that collects distinct values into an array - ' | ' -- delimiter - ) as vl_multi_pcns_all --CEO PCNs -, - num_pcns_ceo, - vl_multi_pcns_ceo --CCTV PCNs -, - num_pcns_cctv, - vl_multi_pcns_cctv - /*Partitions*/ -, - pcnfoidetails_pcn_foi_full.import_year, - pcnfoidetails_pcn_foi_full.import_month, - pcnfoidetails_pcn_foi_full.import_day, - pcnfoidetails_pcn_foi_full.import_date -FROM "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - left join no_resp_cctv on no_resp_cctv.vrm_cctv = pcnfoidetails_pcn_foi_full.vrm - left join no_resp_ceo on no_resp_ceo.vrm_ceo = pcnfoidetails_pcn_foi_full.vrm -WHERE pcnfoidetails_pcn_foi_full.import_date = ( - SELECT max(pcnfoidetails_pcn_foi_full.import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - and isvoid = 0 - and warningflag = 0 - and ( - ( - isvda = 0 - and cast(pcnissuedate as date) < cast('2021-06-01' as date) - ) - or ( - cast(pcnissuedate as date) > cast('2021-05-31' as date) - ) - ) - and dvla_request is not null - and dvla_response is not null - and registered_keeper_address = '' - and pcn_canx_date is null - and pcn_casecloseddate is null - and upper(nextprogressionstage) not like 'WRITEOFF' --and cast(pcnissuedate as date) > cast('2019-12-31' as date) and cast(pcnissuedate as date) < cast('2024-01-01' as date)--Parking Tickets issued between 01/01/2020 and 31/12/2023. -group by pcnfoidetails_pcn_foi_full.vrm --CEO PCNs -, - num_pcns_ceo, - vl_multi_pcns_ceo --CCTV PCNs -, - num_pcns_cctv, - vl_multi_pcns_cctv - /*Partitions*/ -, - pcnfoidetails_pcn_foi_full.import_year, - pcnfoidetails_pcn_foi_full.import_month, - pcnfoidetails_pcn_foi_full.import_day, - pcnfoidetails_pcn_foi_full.import_date -order by pcnfoidetails_pcn_foi_full.vrm -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_pcn_ltn_report_summary.py b/scripts/jobs/parking/parking_pcn_ltn_report_summary.py deleted file mode 100644 index d14b10f8b..000000000 --- a/scripts/jobs/parking/parking_pcn_ltn_report_summary.py +++ /dev/null @@ -1,87 +0,0 @@ -""" -Only need to change the table name and the query prototyped on the Athena UI -by replacing table_name and query_on_athena -Note: python file name should be the same as the table name -""" - -from scripts.helpers.athena_helpers import create_update_table_with_partition -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - -# The target table in liberator refined zone -table_name = "Parking_PCN_LTN_Report_Summary" - -# The exact same query prototyped in pre-prod(stg) or prod Athena -query_on_athena = """ -/************************************************************************************************************************* - Parking_PCN_LTN_Report_Summary - - The SQL builds the PCN Low Traffic Network figures, for use with the answering of the LTN FOI's - - 12/07/2021 - Create SQL. - *************************************************************************************************************************/ -SELECT concat( - substr(Cast(pcnissuedate as varchar(10)), 1, 7), - '-01' - ) AS IssueMonthYear, - street_location, - COUNT(distinct pcn) AS PCNs_Issued, - CAST( - SUM(cast(lib_payment_received as double)) as decimal(11, 2) - ) AS Total_Amount_Paid, - cast(current_timestamp as timestamp) as ImportDateTime, - format_datetime(current_date, 'yyyy') AS import_year, - format_datetime(current_date, 'MM') AS import_month, - format_datetime(current_date, 'dd') AS import_day, - format_datetime(current_date, 'yyyyMMdd') AS import_date -FROM "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full as A -WHERE warningflag = 0 - and isvda = 0 - and isvoid = 0 - AND import_date =( - select max(import_date) - from "dataplatform-prod-liberator-refined-zone".pcnfoidetails_pcn_foi_full - ) - AND vrm != 'T123EST' - AND contraventioncode = '52' - AND street_location IN ( - 'Allen Road', - 'Ashenden Road junction of Glyn Road', - 'Barnabas Road JCT Berger Road', - 'Barnabas Road JCT Oriel Road', - 'Brooke Road (E)', - 'Brooke Road junction of Evering Road', - 'Dove Row', - 'Gore Road junction of Lauriston Road.', - 'Hyde Road', - 'Hyde Road JCT Northport Street', - 'Lee Street junction of Stean Street', - 'Maury Road junction of Evering Road', - 'Meeson Street junction of Kingsmead Way', - 'Nevill Road junction of Osterley Road', - 'Neville Road junction of Osterley Road', - 'Pitfield Street (F)', - 'Pitfield Street JCT Hemsworth Street', - 'Powell Road junction of Kenninghall Road', - 'Pritchard`s Road', - 'Pritchards Road', - 'Richmond Road junction of Greenwood Road', - 'Shepherdess Walk', - 'Ufton Road junction of Downham Road', - 'Wilton Way junction of Greenwood Road' - ) -GROUP BY concat( - substr(Cast(pcnissuedate as varchar(10)), 1, 7), - '-01' - ), - street_location, - import_date, - import_day, - import_month, - import_year; -""" - -create_update_table_with_partition( - environment=environment, query_on_athena=query_on_athena, table_name=table_name -) diff --git a/scripts/jobs/parking/parking_pcn_report_summary.py b/scripts/jobs/parking/parking_pcn_report_summary.py deleted file mode 100644 index 39f7e093b..000000000 --- a/scripts/jobs/parking/parking_pcn_report_summary.py +++ /dev/null @@ -1,104 +0,0 @@ -import sys - -from awsglue.context import GlueContext -from awsglue.dynamicframe import DynamicFrame -from awsglue.job import Job -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext - -from scripts.helpers.helpers import create_pushdown_predicate, get_glue_env_var - -environment = get_glue_env_var("environment") - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -SqlQuery0 = """ -/************************************************************************************************************************* -Parking_PCN_Report_Summary - -The SQL builds the PCN summary data, to get around the Google max number of records - -12/07/2021 - Create SQL. Summerise the PCN's as Total No. issued to Debt Type (CEO, etc), Estate, etc UP to the End the - previous month -*************************************************************************************************************************/ -SELECT concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01') as IssueMonthYear, - debttype as Debt_Type, - CASE When street_location like '%Estate%' OR usrn like '%Z%' Then 1 - ELSE 0 END as Estate_Flag, - CASE When Lib_Payment_Received != '0' Then 1 Else 0 END as Payment_Flag, - count(*) as PCNs_Issued, - - current_timestamp() as ImportDateTime, - date_format(current_date, 'yyyy') AS import_year, - date_format(current_date, 'MM') AS import_month, - date_format(current_date, 'dd') AS import_day, - date_format(current_date, 'yyyyMMdd') AS import_date - -FROM pcnfoidetails_pcn_foi_full as A -WHERE importdattime = (Select MAX(importdattime) from pcnfoidetails_pcn_foi_full) and warningflag = 0 and isvda = 0 - and isvoid = 0 - /*AND pcnissuedate <= date_add('day', -(CAST(substr(cast(current_date as varchar(10)), 9 ,2) as int)), current_date)*/ - -GROUP BY concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01'), debttype, - CASE When street_location like '%Estate%' OR usrn like '%Z%' Then 1 ELSE 0 END, - CASE When Lib_Payment_Received != '0' Then 1 Else 0 END -ORDER BY concat(substr(Cast(pcnissuedate as varchar(10)),1, 7), '-01'), debttype -""" - -## @params: [JOB_NAME] -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) - -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) -## @type: DataSource -## @args: [database = "dataplatform-" + environment + "-liberator-refined-zone", table_name = "pcnfoidetails_pcn_foi_full", transformation_ctx = "DataSource0"] -## @return: DataSource0 -## @inputs: [] -DataSource0 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="DataSource0", - push_down_predicate=create_pushdown_predicate("import_date", 7), -) -## @type: SqlCode -## @args: [sqlAliases = {"pcnfoidetails_pcn_foi_full": DataSource0}, sqlName = SqlQuery0, transformation_ctx = "Transform0"] -## @return: Transform0 -## @inputs: [dfc = DataSource0] -Transform0 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={"pcnfoidetails_pcn_foi_full": DataSource0}, - transformation_ctx="Transform0", -) -## @type: DataSink -## @args: [connection_type = "s3", catalog_database_name = "dataplatform-" + environment + "-liberator-refined-zone", format = "glueparquet", connection_options = {"path": "s3://dataplatform-" + environment + "-refined-zone/parking/liberator/Parking_PCN_Report_Summary/", "partitionKeys": ["import_year" ,"import_month" ,"import_day"], "enableUpdateCatalog":true, "updateBehavior":"UPDATE_IN_DATABASE"}, catalog_table_name = "Parking_PCN_Report_Summary", transformation_ctx = "DataSink0"] -## @return: DataSink0 -## @inputs: [frame = Transform0] -DataSink0 = glueContext.getSink( - path="s3://dataplatform-" - + environment - + "-refined-zone/parking/liberator/Parking_PCN_Report_Summary/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="DataSink0", -) -DataSink0.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="Parking_PCN_Report_Summary", -) -DataSink0.setFormat("glueparquet") -DataSink0.writeFrame(Transform0) - -job.commit() diff --git a/scripts/jobs/parking/parking_persistent_evaders.py b/scripts/jobs/parking/parking_persistent_evaders.py deleted file mode 100644 index 26984fd51..000000000 --- a/scripts/jobs/parking/parking_persistent_evaders.py +++ /dev/null @@ -1,230 +0,0 @@ -import sys - -from awsglue import DynamicFrame -from awsglue.context import GlueContext -from awsglue.job import Job -from awsglue.transforms import * -from awsglue.utils import getResolvedOptions -from pyspark.context import SparkContext - -from scripts.helpers.helpers import get_glue_env_var - -environment = get_glue_env_var("environment") - - -def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFrame: - for alias, frame in mapping.items(): - frame.toDF().createOrReplaceTempView(alias) - result = spark.sql(query) - return DynamicFrame.fromDF(result, glueContext, transformation_ctx) - - -args = getResolvedOptions(sys.argv, ["JOB_NAME"]) -sc = SparkContext() -glueContext = GlueContext(sc) -spark = glueContext.spark_session -job = Job(glueContext) -job.init(args["JOB_NAME"], args) - -# Script generated for node liberator_pcn_bailiff -liberator_pcn_bailiff_node1627663703253 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_bailiff", - transformation_ctx="liberator_pcn_bailiff_node1627663703253", -) - -# Script generated for node liberator_pcn_tickets -liberator_pcn_tickets_node1 = glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-raw-zone", - table_name="liberator_pcn_tickets", - transformation_ctx="liberator_pcn_tickets_node1", -) - -# Script generated for node pcnfoidetails_pcn_foi_full -pcnfoidetails_pcn_foi_full_node1627663704845 = ( - glueContext.create_dynamic_frame.from_catalog( - database="dataplatform-" + environment + "-liberator-refined-zone", - table_name="pcnfoidetails_pcn_foi_full", - transformation_ctx="pcnfoidetails_pcn_foi_full_node1627663704845", - ) -) - -# Script generated for node ApplyMapping -SqlQuery0 = """ -/*** - -15/11/2021 --remove warning notices from the report by removing “left” join, instead inner join using the warning notice flag in pcnfoidetails_pcn_foi -18/11/2021 --use PCases to count the number of PCNs issued against those VRM’s. The existing VRMCount will only give the number of PCN’s fitting the criteria, but others will be returned in the final report when we join on all PCN’s issued to those VRM’s. Having a count of the number in the final report will be useful --remove VRM_Count from final report to avoid confusion. Replace with PCN_Count later in the query --join on TotalPCNs to bring in PCN_Count -24/11/2021 --Added CASE Statement “CaseClosed” to clearly flag cancelled and paid cases. -25/11/2021 --remove the outstanding amount field. Replaced with CASE statement to which only displays the outstandingamount ticket is not cancelled or paid. - -***/ - -/**CTE to determine all PCN's in the given stage criteria in the Persistent Evaders Spec**/ - -WITH PCNCriteria AS - (SELECT DISTINCT ticketserialnumber, VRM - FROM liberator_pcn_tickets - WHERE progressionstage IN ('cc','nodr','foreigncollection','nodrr','predebt','pre-debt','warr','warrant') - AND whenpaid = '' - AND whencancelled = '' - AND import_date = (SELECT MAX(import_date) FROM liberator_pcn_tickets)), - -/*** use PCNCriteria to count the number of PCNs issued against those VRM’s***/ - -MultiPCN AS - (SELECT VRM, Count (*) as VRM_Count - FROM PCNCriteria - GROUP BY VRM), - -/**Inner Join to return VRM's with PCN's in the given criteria which have been issued with 3 or more PCN's, and have received a PCN within the last 60 days***/ - -PCNlast60days AS - (SELECT distinct A.PCN, - A.pcnissuedate, - datediff(current_date, pcnissuedate) as dayssinceissued, - B.VRM, - B.VRM_Count - FROM (SELECT * FROM - pcnfoidetails_pcn_foi_full - WHERE datediff(current_date, pcnissuedate)<=60 - AND import_date = (SELECT MAX(import_date) - FROM pcnfoidetails_pcn_foi_full)) AS A - JOIN (SELECT * FROM MultiPCN - WHERE VRM_Count >=3) AS B ON A.VRM = B.VRM), - -/*** From the VRM's identified in the PCNLast60days create a list of distinct VRM's with PCN's fitting the stage criteria and PCN Count ***/ - -PEVRMS as (SELECT Distinct VRM, VRM_Count FROM PCNlast60days), - -/**return all columns from spec. inner join on distinct list of VRM's**/ - -PCases AS - (SELECT DISTINCT A.ticketserialnumber, - A.VRM, - A.make, - A.model, - A.colour, - A.contraventiondateandtimewith, - B.VRM AS PersistVRM, - CASE - When tickettype IN ('CCTV_BusLane','CCTV Bus Lane','CCTV Bus Lane_On Street') Then 'CCTV_BusLane' - When tickettype IN ('CCTV_Moving','CCTV Moving traffic','CCTV Moving traffic_On Street') Then 'CCTV_Moving' - When tickettype IN ('CCTV_Parking','CCTV static','CCTV static_On Street') Then 'CCTV_Parking' - When tickettype IN ('hht') Then 'CEO' - Else tickettype - End AS DebtType, - A.progressionstage, - A.assoclocation AS location, - A.osoppwhere AS whereonlocation, - A.cpzname AS Zone, - A.contraventioncode, - - CASE - When whenpaid NOT IN ('') or whencancelled NOT IN ('') Then NULL - Else C.lib_initial_debt_amount - End AS OutstandingAmount, - - CASE - When whenpaid NOT IN ('') or whencancelled NOT IN ('') then 'Yes' - Else 'No' - End AS CaseClosed, - - - CASE - When progressionstage IN ('warrant') Then 'Yes' - Else 'No' - End AS WarrantIssued, - - D.warrantissuedate, - D.organisation, - Case - When holdreason in ('') Then 'No' - Else 'Yes' - End AS OnHold, - A.holdreason, - CASE - When CAST(A.contraventiondateandtime as date) <= date_Add(current_date, -60) Then 'No' - Else 'Yes' - END AS PCN_Last_60_days, - - ROW_NUMBER() OVER (PARTITION BY ticketserialnumber ORDER BY ticketserialnumber DESC) as Rn, - - date_format(current_date, 'yyyy') AS import_year, - date_format(current_date, 'MM') AS import_month, - date_format(current_date, 'dd') AS import_day, - date_format(current_date, 'yyyyMMdd') AS import_date - -FROM (SELECT * FROM liberator_pcn_tickets - WHERE import_date = (SELECT MAX(import_date) - FROM liberator_pcn_tickets)) AS A - JOIN (SELECT * FROM PEVRMS) AS B ON A.VRM = B.VRM - - JOIN (SELECT PCN, lib_initial_debt_amount, warningflag - FROM pcnfoidetails_pcn_foi_full - WHERE warningflag = 0 - AND import_date = (SELECT MAX(import_date) - FROM pcnfoidetails_pcn_foi_full)) AS C - ON A.ticketserialnumber = C.PCN - - LEFT JOIN (SELECT ticketreference, warrantissuedate, organisation - FROM liberator_pcn_bailiff - WHERE import_date = (SELECT MAX(import_date) - FROM liberator_pcn_bailiff)) AS D - ON A.ticketserialnumber = D.ticketreference), - -/** count the total number of PCNs issued against the VRM’s in PCases**/ - -TotalPCNs AS (SELECT VRM as PCNVRM, Count (*) as PCN_Count - FROM PCases - WHERE Rn = 1 - GROUP BY VRM), - -/*** Select final list of PCN’s removing the duplicates using a row number filter join with TotalPCNs to bring in the PCN_Count***/ - -PCNPersistentEvaders AS (SELECT A.*, - B.* - FROM (SELECT * FROM PCases - WHERE Rn = 1 - ORDER BY VRM, contraventiondateandtimewith) AS A - JOIN (SELECT PCNVRM, PCN_Count FROM TotalPCNs) AS B - ON A.VRM = B.PCNVRM) - -SELECT * FROM PCNPersistentEvaders - -""" -ApplyMapping_node2 = sparkSqlQuery( - glueContext, - query=SqlQuery0, - mapping={ - "liberator_pcn_bailiff": liberator_pcn_bailiff_node1627663703253, - "liberator_pcn_tickets": liberator_pcn_tickets_node1, - "pcnfoidetails_pcn_foi_full": pcnfoidetails_pcn_foi_full_node1627663704845, - }, - transformation_ctx="ApplyMapping_node2", -) - -# Script generated for node S3 bucket -S3bucket_node3 = glueContext.getSink( - path="s3://dataplatform-" - + environment - + "-refined-zone/parking/liberator/Parking_Persistent_Evaders/", - connection_type="s3", - updateBehavior="UPDATE_IN_DATABASE", - partitionKeys=["import_year", "import_month", "import_day", "import_date"], - enableUpdateCatalog=True, - transformation_ctx="S3bucket_node3", -) -S3bucket_node3.setCatalogInfo( - catalogDatabase="dataplatform-" + environment + "-liberator-refined-zone", - catalogTableName="Parking_Persistent_Evaders", -) -S3bucket_node3.setFormat("glueparquet") -S3bucket_node3.writeFrame(ApplyMapping_node2) -job.commit() diff --git a/terraform/etl/38-aws-glue-job-parking.tf b/terraform/etl/38-aws-glue-job-parking.tf index ea89c308c..9e8c58a49 100644 --- a/terraform/etl/38-aws-glue-job-parking.tf +++ b/terraform/etl/38-aws-glue-job-parking.tf @@ -1,48 +1,7 @@ -module "parking_pcn_denormalisation" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_pcn_denormalisation" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_pcn_denormalisation" - triggered_by_job = module.parking_pcn_create_event_log.job_name - job_description = "This job creates a single de-normalised PCN record with the latest details against it (Events, finance, ETA, etc.). This can then be queried (WITHOUT joins)." - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_persistent_evaders" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_persistent_evaders" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_persistent_evaders" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Job to identify VRM's according to the criteria of Persistent Evaders, and return details of all tickets issued to those VRM's." - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-enable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} +# Migrated job "parking_pcn_denormalisation" to dap-airflow om 30/05/2025 +# >> PCNFOIDetails_PCN_FOI_FULL + +# Migrated job "parking_persistent_evaders" to dap-airflow om 30/05/2025 # Migrated job "parking_school_street_vrms" to dap-airflow om 20/02/2025 @@ -50,133 +9,24 @@ module "parking_persistent_evaders" { # Migrated job "parking_gds_permit_change_comparison" to dap-airflow om 20/02/2025 -module "parking_kpi_gds_summary" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_kpi_gds_summary" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_kpi_gds_summary" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Summarising data from the FOI Summary table to be used in Google Data Studio as need to be under 100,000" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - glue_job_timeout = 240 - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_foi_pcn_gds_summary" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_foi_pcn_gds_summary" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_foi_pcn_gds_summary" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Summarising data from the FOI Google Data Studio dashboard as need to be under 100,000 -" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - glue_job_timeout = 240 - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} +# Migrated job "parking_kpi_gds_summary" to dap-airflow om 30/05/2025 + +# Migrated job "parking_foi_pcn_gds_summary" to dap-airflow om 30/05/2025 # Migrated job "parking_permit_denormalised_gds_street_llpg" to dap-airflow om 20/02/2025 -module "parking_pcn_create_event_log" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_pcn_create_event_log" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_pcn_create_event_log" - triggered_by_job = "${local.short_identifier_prefix}Copy parking Liberator landing zone to raw" - job_description = "This job reviews the PCN Events trying to find the LATEST event date for a number of Events (i.e. DVLA Requested, DVLA Received). The output is a SINGLE PCN record containing some 30+ fields of Dates. The field name identifies what the date field is" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_pcn_report_summary" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_pcn_report_summary" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_pcn_report_summary" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "This job creates the % return figures for the Bailiff data" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_pcn_ltn_report_summary" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_pcn_ltn_report_summary" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_pcn_ltn_report_summary" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "This job creates the LTN PCN count and Total paid" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 2 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-enable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} -# migrated Parking_Suspension_DeNormalised_Data to airflow on 19/05/2025 +# Migrated job "parking_pcn_create_event_log" to dap-airflow om 30/05/2025 +# >> PCNFOIDetails_PCN_EVENT_LOG +# Migrated job "parking_pcn_report_summary" to dap-airflow om 30/05/2025 +# Migrated job "parking_pcn_ltn_report_summary" to dap-airflow om 30/05/2025 +# migrated Parking_Suspension_DeNormalised_Data to airflow on 19/05/2025 # migrated Parking_Permit_DeNormalised_Data to airflow on 20/05/2025 - # The airflow has the latest version of these 7 tables # removed Parking_Deployment_Target_Details # removed parking_ceo_average_on_street @@ -186,180 +36,23 @@ module "parking_pcn_ltn_report_summary" { # removed parking_percent_street_coverage # removed parking_percent_street_coverage_cpz +# Migrated "parking_foreign_vrm_pcns" to dap-airflow on 30/05/2025 -module "parking_foreign_vrm_pcns" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_foreign_vrm_pcns" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_foreign_vrm_pcns" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "This job creates the LTN PCN count and Total paid" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - number_of_workers_for_glue_job = 2 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-enable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - - -module "parking_correspondence_performance_records_with_pcn" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_correspondence_performance_records_with_pcn" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_correspondence_performance_records_with_pcn" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "correspondence performance records with pcn" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} +# Migrated "parking_correspondence_performance_records_with_pcn" to dap-airflow on 30/05/2025 # parking_Disputes_KPI_GDS_Summary not in use anymore - confirmed with Davina -module "parking_foi_pcn_gds_daily_summary" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_foi_pcn_gds_daily_summary" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_foi_pcn_gds_daily_summary" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Daily summarising data from the FOI Google Data Studio dashboard as need to be under 100,000" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - glue_job_timeout = 240 - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY --conf spark.sql.legacy.ctePrecedencePolicy=LEGACY" - } -} -module "parking_eta_decision_records_pcn_dispute_gds" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_eta_decision_records_pcn_dispute_gds" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_eta_decision_records_pcn_dispute_gds" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Daily summarising data from the FOI Google Data Studio dashboard as need to be under 100,000" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 2 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - } -} +# Migrated "parking_foi_pcn_gds_daily_summary" to dap-airflow on 30/05/2025 + +# Migrated "parking_eta_decision_records_pcn_dispute_gds" to dap-airflow on 30/05/2025 # migrated Parking_Permit_diesel_Tends_Bought_in_Month to airflow on 20/05/2025 -module "parking_correspondence_performance_records_with_pcn_gds" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_correspondence_performance_records_with_pcn_gds" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_correspondence_performance_records_with_pcn_gds" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "parking_correspondence_performance_records_with_pcn with no timestamp for GDS" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - - -module "parking_foi_pcn_gds_daily_summary_records" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_foi_pcn_gds_daily_summary_records" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_foi_pcn_gds_daily_summary_records" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Records of daily summarising data from the FOI Google Data Studio dashboard as need to be under 100,000" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - glue_job_timeout = 240 - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY --conf spark.sql.legacy.ctePrecedencePolicy=LEGACY" - } -} - - -module "parking_correspondence_performance_qa_with_totals_gds" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_correspondence_performance_qa_with_totals_gds" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_correspondence_performance_qa_with_totals_gds" - triggered_by_job = "${local.short_identifier_prefix}parking_correspondence_performance_records_with_pcn" - job_description = "For use in Google Studio to calculate the Correspondence performance for each calendar month Total number of cases and Total number of QA reviews for each month." - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - glue_job_timeout = 240 - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} + +# Migrated "parking_correspondence_performance_records_with_pcn_gds" to dap-airflow on 30/05/2025 + +# Migrated "parking_foi_pcn_gds_daily_summary_records" to dap-airflow on 30/05/2025 + +# Migrated job "parking_correspondence_performance_qa_with_totals_gds" to dap-airflow om 30/05/2025 # Migrated job "parking_defect_met_fail" to dap-airflow om 25/02/2025 @@ -371,111 +64,20 @@ module "parking_correspondence_performance_qa_with_totals_gds" { # migrated parking_permit_street_stress_with_cpz to airflow on 20/05/2025 -module "parking_correspondence_performance_records_with_pcn_downtime" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_correspondence_performance_records_with_pcn_downtime" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_correspondence_performance_records_with_pcn_downtime" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "correspondence performance records with pcn FOI records Team details and Downtime data" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_correspondence_performance_records_with_pcn_downtime_gds" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_correspondence_performance_records_with_pcn_downtime_gds" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_correspondence_performance_records_with_pcn_downtime_gds" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "correspondence performance records with pcn FOI records Team details and Downtime data for Google Studio - gds" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 10 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} - -module "parking_open_pcns_vrms_linked_cancelled_ringer" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_open_pcns_vrms_linked_cancelled_ringer" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_open_pcns_vrms_linked_cancelled_ringer" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "Parking Open PCNs linked to VRMs cancelled due to being a Ringer or Clone" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 2 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} -# MRB 15-02-2024 job created +# Migrated "parking_correspondence_performance_records_with_pcn_downtime" to dap-airflow on 30/05/2025 +# Migrated "parking_correspondence_performance_records_with_pcn_downtime_gds" to dap-airflow on 30/05/2025 + +# Migrated "parking_open_pcns_vrms_linked_cancelled_ringer" to dap-airflow on 30/05/2025 +# MRB 15-02-2024 job created # migrated job "Parking_Suspensions_Processed" to dap-airflow on 19/05/2025 # parking_suspensions_processed_with_finyear migrated to dap-airflow on 19/05/2025 - -module "parking_pcn_dvla_response_no_address" { - source = "../modules/aws-glue-job" - is_live_environment = local.is_live_environment - is_production_environment = local.is_production_environment - department = module.department_parking_data_source - job_name = "${local.short_identifier_prefix}parking_pcn_dvla_response_no_address" - helper_module_key = data.aws_s3_object.helpers.key - pydeequ_zip_key = data.aws_s3_object.pydeequ.key - spark_ui_output_storage_id = module.spark_ui_output_storage_data_source.bucket_id - script_name = "parking_pcn_dvla_response_no_address" - triggered_by_job = module.parking_pcn_denormalisation.job_name - job_description = "All VRMs with PCNs response from DVLA has no address still open and not due to be written off" - workflow_name = "${local.short_identifier_prefix}parking-liberator-data-workflow" - trigger_enabled = local.is_production_environment - number_of_workers_for_glue_job = 2 - glue_job_worker_type = "G.1X" - glue_version = "4.0" - job_parameters = { - "--job-bookmark-option" = "job-bookmark-disable" - "--environment" = var.environment - "--conf" = "spark.sql.legacy.timeParserPolicy=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.int96RebaseModeInWrite=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInRead=LEGACY --conf spark.sql.legacy.parquet.datetimeRebaseModeInWrite=LEGACY" - } -} +# Migrated "parking_pcn_dvla_response_no_address" to dap-airflow on 30/05/2025 # Migrated job "parking_motorcycle_permits_480" to dap-airflow om 20/02/2025 # Migrated job "parking_permit_street_cpz_stress_mc" to dap-airflow on 21/05/2025 - # MRB 18-08-2024 job created # Migrated job "parking_permit_denormalisation_mc" to dap-airflow om 01/05/2025