Skip to content

Commit 6e57ce5

Browse files
committed
permit diesel trends using single digit day and month
Fix the single-digit month and day – that's the one I missed half a year ago. I've just changed the S3 path in both prod and stag. Update the script to prevent issues tomorrow.
1 parent eb877d7 commit 6e57ce5

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

scripts/jobs/parking/parking_permit_diesel_trends_bought_in_month.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
6464
/*** Create the Calendar formatted data ***/
6565
With Calendar_Data as (
6666
SELECT
67-
date as Calendar_date, workingday, dow, holiday,fin_year,
67+
date as Calendar_date, workingday, dow, holiday,fin_year,
6868
cast(substr(cast(date as string),1, 8)||'01' as date) as MonthStartDate,
6969
CASE
7070
When cast(substr(date, 6,2) as int) = 1 Then 'Q4' -- Jan
71-
When cast(substr(date, 6,2) as int) = 2 Then 'Q4' -- Feb
71+
When cast(substr(date, 6,2) as int) = 2 Then 'Q4' -- Feb
7272
When cast(substr(date, 6,2) as int) = 3 Then 'Q4' -- March
73-
When cast(substr(date, 6,2) as int) = 4 Then 'Q1' -- Apr
74-
When cast(substr(date, 6,2) as int) = 5 Then 'Q1' -- May
75-
When cast(substr(date, 6,2) as int) = 6 Then 'Q1' -- June
76-
When cast(substr(date, 6,2) as int) = 7 Then 'Q2' -- Jul
77-
When cast(substr(date, 6,2) as int) = 8 Then 'Q2' -- Aug
73+
When cast(substr(date, 6,2) as int) = 4 Then 'Q1' -- Apr
74+
When cast(substr(date, 6,2) as int) = 5 Then 'Q1' -- May
75+
When cast(substr(date, 6,2) as int) = 6 Then 'Q1' -- June
76+
When cast(substr(date, 6,2) as int) = 7 Then 'Q2' -- Jul
77+
When cast(substr(date, 6,2) as int) = 8 Then 'Q2' -- Aug
7878
When cast(substr(date, 6,2) as int) = 9 Then 'Q2' -- Sept
79-
When cast(substr(date, 6,2) as int) = 10 Then 'Q3' -- Oct
80-
When cast(substr(date, 6,2) as int) = 11 Then 'Q3' -- Nov
79+
When cast(substr(date, 6,2) as int) = 10 Then 'Q3' -- Oct
80+
When cast(substr(date, 6,2) as int) = 11 Then 'Q3' -- Nov
8181
When cast(substr(date, 6,2) as int) = 12 Then 'Q3' -- Dec
8282
Else ''
8383
END as QTR,
84-
84+
8585
ROW_NUMBER() OVER ( PARTITION BY date ORDER BY date, import_date DESC) row_num
8686
FROM calendar),
8787
@@ -94,17 +94,17 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
9494
END as date) as Format_date, fin_year, QTR
9595
FROM Calendar_Data
9696
WHERE row_num = 1),
97-
97+
9898
Latest_Report_Year as (
9999
SELECT
100100
/**** Find the Latest Financial Year ***/
101101
CASE
102102
/** Q1 **/
103-
When cast(substr(cast(Format_date as string),6,2) as int) = 4 Then
103+
When cast(substr(cast(Format_date as string),6,2) as int) = 4 Then
104104
cast(cast(fin_year as int) - 1 as string)
105-
When cast(substr(cast(Format_date as string),6,2) as int) = 5 Then
105+
When cast(substr(cast(Format_date as string),6,2) as int) = 5 Then
106106
cast(cast(fin_year as int) - 1 as string)
107-
When cast(substr(cast(Format_date as string),6,2) as int) = 6 Then
107+
When cast(substr(cast(Format_date as string),6,2) as int) = 6 Then
108108
cast(cast(fin_year as int) - 1 as string)
109109
/** Q2 **/
110110
When cast(substr(cast(Format_date as string),6,2) as int) = 7 Then fin_year
@@ -140,29 +140,29 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
140140
END as LatestQTR,
141141
142142
CASE
143-
When cast(substr(cast(Format_date as string),6,2) as int) = 4 Then
144-
cast(substr(cast(Format_date as string),1,5)||'03-01' as date)
145-
When cast(substr(cast(Format_date as string),6,2) as int) = 5 Then
146-
cast(substr(cast(Format_date as string),1,5)||'03-01' as date)
147-
When cast(substr(cast(Format_date as string),6,2) as int) = 6 Then
143+
When cast(substr(cast(Format_date as string),6,2) as int) = 4 Then
144+
cast(substr(cast(Format_date as string),1,5)||'03-01' as date)
145+
When cast(substr(cast(Format_date as string),6,2) as int) = 5 Then
146+
cast(substr(cast(Format_date as string),1,5)||'03-01' as date)
147+
When cast(substr(cast(Format_date as string),6,2) as int) = 6 Then
148148
cast(substr(cast(Format_date as string),1,5)||'03-01' as date)
149-
When cast(substr(cast(Format_date as string),6,2) as int) = 7 Then
150-
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
151-
When cast(substr(cast(Format_date as string),6,2) as int) = 8 Then
152-
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
153-
When cast(substr(cast(Format_date as string),6,2) as int) = 9 Then
154-
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
155-
When cast(substr(cast(Format_date as string),6,2) as int) = 10 Then
156-
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
157-
When cast(substr(cast(Format_date as string),6,2) as int) = 11 Then
158-
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
159-
When cast(substr(cast(Format_date as string),6,2) as int) = 12 Then
160-
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
161-
When cast(substr(cast(Format_date as string),6,2) as int) = 1 Then
162-
cast(substr(cast(Format_date as string),1,5)||'12-01' as date)
163-
When cast(substr(cast(Format_date as string),6,2) as int) = 2 Then
164-
cast(substr(cast(Format_date as string),1,5)||'12-01' as date)
165-
When cast(substr(cast(Format_date as string),6,2) as int) = 3 Then
149+
When cast(substr(cast(Format_date as string),6,2) as int) = 7 Then
150+
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
151+
When cast(substr(cast(Format_date as string),6,2) as int) = 8 Then
152+
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
153+
When cast(substr(cast(Format_date as string),6,2) as int) = 9 Then
154+
cast(substr(cast(Format_date as string),1,5)||'06-01' as date)
155+
When cast(substr(cast(Format_date as string),6,2) as int) = 10 Then
156+
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
157+
When cast(substr(cast(Format_date as string),6,2) as int) = 11 Then
158+
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
159+
When cast(substr(cast(Format_date as string),6,2) as int) = 12 Then
160+
cast(substr(cast(Format_date as string),1,5)||'09-01' as date)
161+
When cast(substr(cast(Format_date as string),6,2) as int) = 1 Then
162+
cast(substr(cast(Format_date as string),1,5)||'12-01' as date)
163+
When cast(substr(cast(Format_date as string),6,2) as int) = 2 Then
164+
cast(substr(cast(Format_date as string),1,5)||'12-01' as date)
165+
When cast(substr(cast(Format_date as string),6,2) as int) = 3 Then
166166
cast(substr(cast(Format_date as string),1,5) ||'12-01' as date)
167167
END as Adj_Date
168168
From CalendarFormat
@@ -172,16 +172,16 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
172172
SELECT distinct
173173
MonthStartDate
174174
FROM Calendar_Data as A
175-
WHERE MonthStartDate >= cast('2021-04-01' as date) and
175+
WHERE MonthStartDate >= cast('2021-04-01' as date) and
176176
MonthStartDate <= current_date),
177177
/***********************************************************************
178178
Collect the Permit 2019 data as benchmark
179179
************************************************************************/
180180
Permit_VRMS as (
181-
Select distinct new_vrm, new_make,new_model,new_fuel,new_engine_capactiy,
181+
Select distinct new_vrm, new_make,new_model,new_fuel,new_engine_capactiy,
182182
new_co2_emission
183-
From liberator_permit_vrm_update
184-
WHERE import_date = (Select MAX(import_date) from
183+
From liberator_permit_vrm_update
184+
WHERE import_date = (Select MAX(import_date) from
185185
liberator_permit_vrm_update)),
186186
PERMIT_2019 as (
187187
SELECT
@@ -201,9 +201,9 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
201201
END as co2_emission
202202
From parking_permit_denormalised_data as A
203203
LEFT JOIN Permit_VRMS as B ON A.vrm = B.new_vrm
204-
WHERE ImportDateTime = (Select MAX(ImportDateTime) from
204+
WHERE ImportDateTime = (Select MAX(ImportDateTime) from
205205
parking_permit_denormalised_data)
206-
and cast(substr(cast(application_date as string), 1, 10) as date)
206+
and cast(substr(cast(application_date as string), 1, 10) as date)
207207
between cast('2020-03-01' as date) and cast('2020-03-31' as date)
208208
and permit_type != 'Dispensation' AND latest_permit_status not IN ('Cancelled','Rejected','RENEW_REJECTED')
209209
AND cpz_name IN ('Zone A', 'Zone D','Zone F','Zone G','Zone G2','Zone H','Zone K','Zone L',
@@ -215,7 +215,7 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
215215
count(*) as TotalPermits,
216216
SUM(CASE When Fuel = 'DIESEL' Then 1 Else 0 END) as DieselPermitTotal,
217217
SUM(CASE When Fuel = 'ELECTRIC' Then 1 Else 0 END) as ELECTRICPermitTotal,
218-
218+
219219
/*** Calc the percantage ***/
220220
(cast(count(*) as decimal(10,4)) - SUM(CASE When Fuel = 'DIESEL' Then 1 Else 0 END))
221221
/
@@ -235,18 +235,18 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
235235
Collect the 'current' permit data, from 1st April 2021 (after COVID)
236236
*********************************************************************************************************************/
237237
Current_Permit as (
238-
SELECT *
238+
SELECT *
239239
FROM parking_permit_denormalised_data as A
240240
LEFT JOIN Permit_VRMS as B ON A.vrm = B.new_vrm
241-
WHERE ImportDateTime = (Select MAX(ImportDateTime) from
241+
WHERE ImportDateTime = (Select MAX(ImportDateTime) from
242242
parking_permit_denormalised_data)
243243
AND permit_type != 'Dispensation' AND latest_permit_status not IN ('Cancelled','Rejected','RENEW_REJECTED')
244244
AND cpz_name IN ('Zone A', 'Zone D','Zone F','Zone G','Zone G2','Zone H','Zone K','Zone L',
245245
'Zone N','Zone P','Zone Q','Zone S','Zone U','Zone T')),
246246
247247
Permit_Summary as (
248248
SELECT
249-
permit_reference, application_date, vrm, start_date, end_date,
249+
permit_reference, application_date, vrm, start_date, end_date,
250250
cast(substr(cast(application_date as string), 1, 8)||'01' as date) as MonthDate,
251251
CASE
252252
When new_vrm is NULL Then fuel
@@ -261,7 +261,7 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
261261
ELSE new_co2_emission
262262
END as co2_emission
263263
FROM Current_Permit as A
264-
WHERE cast(substr(cast(application_date as string), 1, 10) as date) >=
264+
WHERE cast(substr(cast(application_date as string), 1, 10) as date) >=
265265
cast('2021-04-01' as date)),
266266
267267
/*** Total the number of 'open' Permits & Diesel, etc Permits annd the 2019 data ***/
@@ -270,8 +270,8 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
270270
MonthDate,
271271
count(*) as TotalPermits,
272272
SUM(CASE When Fuel = 'DIESEL' Then 1 Else 0 END) as DieselPermitTotal,
273-
SUM(CASE When Fuel = 'ELECTRIC' Then 1 Else 0 END) as ELECTRICPermitTotal,
274-
273+
SUM(CASE When Fuel = 'ELECTRIC' Then 1 Else 0 END) as ELECTRICPermitTotal,
274+
275275
/*** Calc the percantage ***/
276276
(cast(count(*) as decimal(10,4)) - SUM(CASE When Fuel = 'DIESEL' Then 1 Else 0 END))
277277
/
@@ -294,11 +294,10 @@ def sparkSqlQuery(glueContext, query, mapping, transformation_ctx) -> DynamicFra
294294
SELECT
295295
*,
296296
current_timestamp() as ImportDateTime,
297-
replace(cast(current_date() as string),'-','') as import_date,
298-
299-
cast(Year(current_date) as string) as import_year,
300-
cast(month(current_date) as string) as import_month,
301-
cast(day(current_date) as string) as import_day
297+
format_datetime(current_date, 'yyyy') AS import_year,
298+
format_datetime(current_date, 'MM') AS import_month,
299+
format_datetime(current_date, 'dd') AS import_day,
300+
format_datetime(current_date, 'yyyyMMdd') AS import_date
302301
FROM Permit_Report_ALL
303302
"""
304303
SQL_node1658765472050 = sparkSqlQuery(

0 commit comments

Comments
 (0)