forked from ojalaquellueva/gnrs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_user_data.sql
More file actions
executable file
·45 lines (40 loc) · 1 KB
/
import_user_data.sql
File metadata and controls
executable file
·45 lines (40 loc) · 1 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
-- ----------------------------------------------------------
-- Import raw user data to table user_data_raw
--
-- Requires parameters:
-- $raw_data_tbl_temp --> :raw_data_tbl_temp (job-specific temp table)
-- $infile --> :infile
-- ----------------------------------------------------------
-- Create job-specific raw data table
DROP TABLE IF EXISTS :"raw_data_tbl_temp";
CREATE TABLE :"raw_data_tbl_temp" (
user_id text DEFAULT NULL,
country text DEFAULT NULL,
state_province text DEFAULT NULL,
county_parish text DEFAULT NULL
)
;
-- Import raw data
\COPY user_data_raw FROM :'infile' DELIMITER ',' CSV NULL AS 'NA' HEADER;
-- Delete existing records for this job, if any
DELETE FROM user_data_raw
WHERE job=:'job'
;
-- Insert records plus job# to main raw data table
INSERT INTO user_data_raw (
job,
user_id,
country,
state_province,
county_parish
)
SELECT
:'job',
user_id,
country,
state_province,
county_parish
FROM :"raw_data_temp"
;
-- Drop job-specific raw data table
DROP TABLE IF EXISTS :"raw_data_tbl_temp";