Skip to content

Commit 304c373

Browse files
authored
Create parking_bailiff_totals.py
Bailiff Warrant and cedar monthly summary totals
1 parent 54deb4e commit 304c373

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_bailiff_totals"
13+
14+
# The exact same query prototyped in pre-prod(stg) orprod Athena
15+
query_on_athena = """
16+
/*********************************************************************************
17+
Parking_Bailiff_Totals
18+
19+
This SQL summeries the number of Warrants issues against the total Income
20+
21+
20/11/2024 - Create Query
22+
*********************************************************************************/
23+
/*** Warrants ***/
24+
with bailiff_warrants as (
25+
SELECT *, cast(substr(cast(warrantissuedate as varchar), 1, 8)||'01' as date) as Bailiff_Date
26+
FROM "dataplatform-stg-liberator-raw-zone".liberator_pcn_bailiff
27+
WHERE import_Date = format_datetime(current_date, 'yyyyMMdd')),
28+
29+
/*** Obtain the Cedar payments ***/
30+
cedar_pay as (
31+
SELECT *,
32+
CAST(substr(cast(trandate as varchar), 7, 4)||'-'||
33+
substr(cast(trandate as varchar), 4, 2)||'-01' as date) as cedar_month
34+
FROM "parking-raw-zone".cedar_parking_payments
35+
WHERE import_Date = (Select MAX(import_date) from "parking-raw-zone".cedar_parking_payments)
36+
AND description = 'NBI'),
37+
38+
/*** Summerise the payments ***/
39+
Cedar_Summ as (
40+
SELECT
41+
cedar_month,
42+
-(sum(cast(financialvalue as double))) as Monthly_Paid
43+
FROM cedar_pay
44+
group by cedar_month),
45+
46+
/*** Summerise the warrants ***/
47+
warrant_sum as (
48+
SELECT
49+
Bailiff_Date,
50+
count(*) as Monthly_Totals
51+
FROM bailiff_warrants
52+
group by Bailiff_Date)
53+
54+
/*** Bring the two togather ***/
55+
SELECT
56+
A.*, B.*,
57+
58+
format_datetime(CAST(CURRENT_TIMESTAMP AS timestamp), 'yyyy-MM-dd HH:mm:ss') AS importdatetime
59+
format_datetime(current_date, 'yyyy') AS import_year,
60+
format_datetime(current_date, 'MM') AS import_month,
61+
format_datetime(current_date, 'dd') AS import_day,
62+
format_datetime(current_date, 'yyyyMMdd') AS import_date
63+
FROM warrant_sum as A
64+
LEFT JOIN Cedar_Summ as B ON A.Bailiff_Date = B.cedar_month
65+
"""
66+
67+
create_update_table_with_partition(
68+
environment=environment, query_on_athena=query_on_athena, table_name=table_name
69+
)

0 commit comments

Comments
 (0)