-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2023_week_03.sql
More file actions
45 lines (40 loc) · 1.6 KB
/
2023_week_03.sql
File metadata and controls
45 lines (40 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
USE DATABASE til_playground;
USE SCHEMA preppin_data_inputs;
--- preppin data week 3 ---
--Input the data
--For the transactions file:
--Filter the transactions to just look at DSB (help)
--These will be transactions that contain DSB in the Transaction Code field
--Rename the values in the Online or In-person field, Online of the 1 values and In-Person for the 2 values
--Change the date to be the quarter (help)
--Sum the transaction values for each quarter and for each Type of Transaction (Online or In-Person) (help)
--For the targets file:
--Pivot the quarterly targets so we have a row for each Type of Transaction and each Quarter (help)
-- Rename the fields
--Remove the 'Q' from the quarter field and make the data type numeric (help)
--Join the two datasets together (help)
--You may need more than one join clause!
--Remove unnecessary fields
--Calculate the Variance to Target for each row (help)
--Output the data
WITH transactions AS (
SELECT
CASE
WHEN online_or_in_person = 1 THEN 'Online'
ELSE 'In-Person'
END online_or_in_person,
QUARTER(TO_DATE(transaction_date, 'dd/mm/yyyy hh24:mi:ss')) quarter,
SUM(value) as value
FROM PD2023_WK01
WHERE transaction_code LIKE 'DSB-%'
GROUP BY 1,2
)
SELECT
tg.online_or_in_person,
REPLACE(tg.quarter, 'Q', '')::int quarter,
tg.quarterly_target,
t.value,
t.value - tg.quarterly_target variance_from_target
FROM PD2023_WK03_TARGETS
UNPIVOT (quarterly_target FOR quarter IN (q1, q2, q3, q4)) as tg
INNER JOIN transactions as t ON tg.online_or_in_person = t.online_or_in_person AND REPLACE(tg.quarter, 'Q', '')::int = t.quarter;