-
Notifications
You must be signed in to change notification settings - Fork 0
/
WEEK2.sql
83 lines (68 loc) · 2.16 KB
/
WEEK2.sql
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
USE DATABASE FROSTYFRIDAY;
USE SCHEMA CHALLENGES;
CREATE STAGE IF NOT EXISTS S3_FF_WEEK2
URL = 's3://frostyfridaychallenges/challenge_2/';
-- SHOW STAGES;
-- LIST @S3_FF_WEEK2;
CREATE FILE FORMAT IF NOT EXISTS PARQUET_FORMAT
TYPE = 'PARQUET'
-- COMPRESSION = 'SNAPPY'
;
-- SHOW FILE FORMATS;
-- SELECT DISTINCT METADATA$FILENAME AS FILENAME
-- FROM @S3_FF_WEEK2;
-- see results
SELECT $1
FROM @S3_FF_WEEK2
(FILE_FORMAT => 'PARQUET_FORMAT');
-- create table: infer schema from file instead of manually mapping the column names - preserves order of columns, avoids missing column names wich for some rows might not be present
CREATE TABLE IF NOT EXISTS CHALLENGES.WEEK2 USING template (
SELECT array_agg(object_construct(*))
FROM table (
infer_schema(
location => '@S3_FF_WEEK2/'
, file_format => 'PARQUET_FORMAT'
, ignore_case => TRUE
)
)
);
--load the data
COPY INTO CHALLENGES.WEEK2
FROM (
SELECT
$1:"employee_id"
, $1:"first_name"
, $1:"last_name"
, $1:"email"
, $1:"street_num"
, $1:"street_name"
, $1:"city"
, $1:"postcode"
, $1:"country"
, $1:"country_code"
, $1:"time_zone"
, $1:"payroll_iban"
, $1:"dept"
, $1:"job_title"
, $1:"education"
, $1:"title"
, $1:"suffix"
FROM @S3_FF_WEEK2
(FILE_FORMAT => 'PARQUET_FORMAT')
);
SELECT * FROM FROSTYFRIDAY.CHALLENGES.WEEK2;
CREATE VIEW IF NOT EXISTS FROSTYFRIDAY.CHALLENGES.WEEK2_VIEW
AS
SELECT
"EMPLOYEE_ID","DEPT","JOB_TITLE" from FROSTYFRIDAY.CHALLENGES.WEEK2;
SELECT * FROM FROSTYFRIDAY.CHALLENGES.WEEK2_VIEW;
CREATE STREAM IF NOT EXISTS STREAM_WEEK2
ON VIEW FROSTYFRIDAY.CHALLENGES.WEEK2_VIEW;
-- SHOW STREAMS;
SELECT * FROM STREAM_WEEK2;
UPDATE FROSTYFRIDAY.CHALLENGES.WEEK2 SET COUNTRY = 'Japan' WHERE EMPLOYEE_ID = 8;
UPDATE FROSTYFRIDAY.CHALLENGES.WEEK2 SET LAST_NAME = 'Forester' WHERE EMPLOYEE_ID = 22;
UPDATE FROSTYFRIDAY.CHALLENGES.WEEK2 SET DEPT = 'Marketing' WHERE EMPLOYEE_ID = 25;
UPDATE FROSTYFRIDAY.CHALLENGES.WEEK2 SET TITLE = 'Ms' WHERE EMPLOYEE_ID = 32;
UPDATE FROSTYFRIDAY.CHALLENGES.WEEK2 SET JOB_TITLE = 'Senior Financial Analyst' WHERE EMPLOYEE_ID = 68;
SELECT * FROM STREAM_WEEK2;