|
| 1 | +""" |
| 2 | +Only need to change the table name and the query prototyped on the Athena UI |
| 3 | +by replacing table_name and query_on_athena |
| 4 | +""" |
| 5 | + |
| 6 | +from scripts.helpers.athena_helpers import create_update_table_with_partition |
| 7 | +from scripts.helpers.helpers import get_glue_env_var |
| 8 | + |
| 9 | +environment = get_glue_env_var("environment") |
| 10 | + |
| 11 | +# The target table in liberator refined zone |
| 12 | +table_name = "parking_cycle_hangar_allocation_detail" |
| 13 | + |
| 14 | +# The exact same query prototyped in pre-prod(stg) orprod Athena |
| 15 | +query_on_athena = """ |
| 16 | +/******************************************************************************************************************* |
| 17 | +parking_cycle_hangar_allocation_Details |
| 18 | +
|
| 19 | +The SQL details the number of cycle spaces that are occupied |
| 20 | +in each cycle hangar. It also identifies the number of Parties |
| 21 | +that are on the waiting list. The code has been amended to use |
| 22 | +Tom's hangar list |
| 23 | +
|
| 24 | +29/10/2024 - Create Query |
| 25 | +******************************************************************************************************************/ |
| 26 | +With TomHangar as ( |
| 27 | + SELECT |
| 28 | + asset_no, asset_type, street_or_estate, zone, status, key_number, fob, location_description, |
| 29 | + road_name, postcode, date_installed, easting, northing, road_or_pavement, |
| 30 | + case |
| 31 | + When asset_no like '%Bikehangar_1577%' Then '1577' |
| 32 | + When asset_no like '%Bikehangar_H1439%' Then 'H1439' |
| 33 | + When asset_no like '%Bikehangar_H1440%' Then 'Hangar_H1440' |
| 34 | + When asset_no like '%Bikehangar_1435%' Then 'Bikehangar_H1435' |
| 35 | + ELSE replace(asset_no, ' ','_') |
| 36 | + END as HangarID |
| 37 | + from "parking-raw-zone".parking_parking_ops_cycle_hangar_list |
| 38 | + WHERE import_Date = (Select MAX(import_date) from "parking-raw-zone".parking_parking_ops_cycle_hangar_list) |
| 39 | + AND asset_type = 'Hangar' AND lower(status) IN ('active', 'estate locked gate issue') |
| 40 | + /** 20-08-2024 add check for blank records **/ |
| 41 | + AND Length(trim(asset_no))> 2), |
| 42 | +
|
| 43 | +/*** Get the Hangar details from Liberator ***/ |
| 44 | +HangarDetails AS ( |
| 45 | + SELECT A.ID , |
| 46 | + HANGAR_TYPE , |
| 47 | + HANGAR_ID , |
| 48 | + IN_SERVICE , |
| 49 | + MAINTENANCE_KEY , |
| 50 | + SPACES , |
| 51 | + HANGAR_LOCATION , |
| 52 | + USRN , |
| 53 | + LATITUDE , |
| 54 | + LONGITUDE , |
| 55 | + START_OF_LIFE , |
| 56 | + END_OF_LIFE, |
| 57 | + ROW_NUMBER() OVER (PARTITION BY HANGAR_TYPE, HANGAR_ID, IN_SERVICE, MAINTENANCE_KEY, SPACES, |
| 58 | + HANGAR_LOCATION, USRN, LATITUDE,LONGITUDE, START_OF_LIFE, END_OF_LIFE |
| 59 | + ORDER BY HANGAR_TYPE, HANGAR_ID, IN_SERVICE, MAINTENANCE_KEY, SPACES, HANGAR_LOCATION, USRN, LATITUDE, LONGITUDE, |
| 60 | + START_OF_LIFE, END_OF_LIFE DESC) AS ROW |
| 61 | + FROM "dataplatform-stg-liberator-raw-zone".liberator_hangar_details as A |
| 62 | + WHERE import_Date = format_datetime(current_date, 'yyyyMMdd') |
| 63 | + ORDER BY HANGAR_ID), |
| 64 | +
|
| 65 | +/*** Party List ***/ |
| 66 | +Licence_Party as ( |
| 67 | + SELECT * from "dataplatform-stg-liberator-raw-zone".liberator_licence_party |
| 68 | + WHERE import_Date = format_datetime(current_date, 'yyyyMMdd')), |
| 69 | +/******************************************************************************* |
| 70 | +Cycle Hangar allocation details |
| 71 | +*******************************************************************************/ |
| 72 | +Cycle_Hangar_allocation as ( |
| 73 | + SELECT |
| 74 | + *, |
| 75 | + ROW_NUMBER() OVER ( PARTITION BY hanger_id, trim(upper(space)) |
| 76 | + ORDER BY hanger_id, trim(upper(space)), date_of_allocation DESC, fee_due_date DESC, id DESC) row_num |
| 77 | + FROM "dataplatform-stg-liberator-raw-zone".liberator_hangar_allocations |
| 78 | + WHERE import_Date = format_datetime(current_date, 'yyyyMMdd') |
| 79 | + AND key_issued = 'Y' AND length(key_id) > 2 AND space != 'null') |
| 80 | +
|
| 81 | +SELECT |
| 82 | + hanger_id, asset_no, asset_type, street_or_estate, zone, status,location_description, road_name, |
| 83 | + C.postcode as Hangar_Postcode, date_installed, easting, northing road_or_pavement, key_id,USRN, |
| 84 | + space, key_issued, date_of_allocation, allocation_status, fee_due_date, |
| 85 | + A.party_id, title, first_name, surname, |
| 86 | + address1, address2, address3, D.postcode, uprn, telephone_number, email_address, |
| 87 | +
|
| 88 | + format_datetime(CAST(CURRENT_TIMESTAMP AS timestamp), |
| 89 | + 'yyyy-MM-dd HH:mm:ss') AS importdatetime, |
| 90 | +
|
| 91 | + format_datetime(current_date, 'yyyy') AS import_year, |
| 92 | + format_datetime(current_date, 'MM') AS import_month, |
| 93 | + format_datetime(current_date, 'dd') AS import_day, |
| 94 | + format_datetime(current_date, 'yyyyMMdd') AS import_date |
| 95 | + |
| 96 | +from Cycle_Hangar_allocation as A |
| 97 | +LEFT JOIN TomHangar as C ON A.hanger_id = C.HangarID |
| 98 | +LEFT JOIN Licence_Party as D ON A.party_id = D.business_party_id |
| 99 | +LEFT JOIN HangarDetails as E ON A.hanger_id = E.HANGAR_ID |
| 100 | +where row_num = 1 AND allocation_status not IN ('cancelled', 'key_returned','key_not_returned') |
| 101 | +ORDER BY hanger_id, space |
| 102 | +""" |
| 103 | + |
| 104 | +create_update_table_with_partition( |
| 105 | + environment=environment, query_on_athena=query_on_athena, table_name=table_name |
| 106 | +) |
0 commit comments