-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.py
334 lines (305 loc) · 8.53 KB
/
transform.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import psycopg2
update_employers = """
INSERT INTO employers
(
employer_name
)
SELECT DISTINCT
lower(trim(employer))
FROM salary_staging
WHERE lower(trim(employer)) NOT IN(SELECT employer_name FROM employers) and employer IS NOT NULL
;"""
update_locations = """
INSERT INTO locations
(
location_name
)
SELECT DISTINCT
lower(trim(location))
FROM salary_staging
WHERE lower(trim(location)) NOT IN(SELECT location_name FROM locations) and location IS NOT NULL
;"""
update_location_countries = """
UPDATE locations SET
location_country = cca2
FROM
countries
where (locations.location_name LIKE '%' || LOWER(substring(name from 0 for position(',' in name))) || '%')
AND location_country IS NULL
;"""
update_location_states = """
UPDATE locations SET
location_state = abbreviation,
location_country = 'US'
FROM
us_states
WHERE (locations.location_name LIKE '%' || LOWER(us_states.name) || '%'
OR locations.location_name LIKE '%, ' || LOWER(us_states.abbreviation) || '%'
OR locations.location_name LIKE '% ' || LOWER(us_states.abbreviation))
AND locations.location_state IS NULL AND locations.location_country IS NULL
;"""
update_location_ca_states = """
UPDATE locations SET
location_state = abbreviation,
location_country = 'CA'
FROM
ca_states
WHERE (locations.location_name LIKE '%' || LOWER(ca_states.name) || '%'
OR locations.location_name LIKE '%, ' || LOWER(ca_states.abbreviation) || '%'
OR locations.location_name LIKE '% ' || LOWER(ca_states.abbreviation))
AND locations.location_state IS NULL and locations.location_country IS NULL
;"""
update_location_cities = """
UPDATE locations SET
location_city = nullif(substring(location_name from 0 for position(',' in location_name)), '')
WHERE location_city IS NULL
;"""
update_zip_codes = """
UPDATE locations SET
location_city = lower("City"),
location_state="State",
location_country='US'
FROM zip_codes
WHERE location_name="Zipcode"::VARCHAR
AND location_city IS NULL
;"""
update_lat_long_by_state = """
UPDATE locations
SET location_latitude="Lat",
location_longitude="Long"
FROM zip_codes
WHERE location_city = lower("City") and location_state="State"
;"""
update_lat_long_by_country = """
UPDATE locations
SET location_latitude = substring(latlng from 0 for position(',' in latlng))::decimal(6,2),
location_longitude = substring(latlng from position(',' in latlng)+1)::decimal(6,2)
FROM countries
WHERE location_country = cca2
AND location_latitude IS NULL and location_longitude IS NULL
;"""
update_county = """
UPDATE locations
SET location_county = lower(county)
FROM cities_counties_xref
WHERE locations.location_city=lower(city)
AND lower(locations.location_state)=lower(state)
AND location_county IS NULL
;"""
update_job_titles = """
INSERT INTO job_titles
(
job_title
)
SELECT DISTINCT
lower(trim(job_title))
FROM salary_staging
WHERE lower(trim(job_title)) NOT IN(SELECT job_title FROM job_titles) and job_title IS NOT NULL
;"""
update_job_categories_exec = """
UPDATE job_titles
SET job_title_category='Management'
WHERE (job_title LIKE 'c_o%'
OR job_title LIKE 'founder%'
OR job_title LIKE '%co-founder%'
OR job_title LIKE '%exec%'
OR job_title LIKE '%president%'
OR job_title LIKE 'vp%'
OR job_title LIKE '%director%'
OR job_title LIKE '%admin%'
OR job_title LIKE '%manager%'
OR job_title LIKE '%team lead'
)
AND job_title_category IS NULL
;"""
update_job_categories_data = """
UPDATE job_titles
SET job_title_category='Data'
WHERE (job_title LIKE '%data%'
OR job_title LIKE '%analyst%'
)
AND job_title_category IS NULL
;"""
update_job_categories_web = """
UPDATE job_titles
SET job_title_category='Web'
WHERE (job_title LIKE 'full_stack%'
OR job_title LIKE '%fullstack%'
OR job_title LIKE '%front_end%'
OR job_title LIKE '%frontend%'
OR job_title LIKE '%web %'
)
AND job_title_category IS NULL
;"""
update_job_categories_sw = """
UPDATE job_titles
SET job_title_category='Software'
WHERE (job_title LIKE '%swe%'
OR job_title LIKE '%sde%'
OR job_title LIKE '%developer%'
OR job_title LIKE '%software%'
OR job_title LIKE '%development%'
OR job_title = 'computer scientist'
OR job_title LIKE '%tech lead'
OR job_title LIKE 'technical lead%'
)
AND job_title_category IS NULL
;"""
update_job_categories_eng = """
UPDATE job_titles
SET job_title_category='Engineering'
WHERE (job_title LIKE '%engineer%')
AND job_title_category IS NULL
;"""
update_job_categories_ops = """
UPDATE job_titles
SET job_title_category='Operations'
WHERE (job_title LIKE 'devop_'
OR job_title LIKE '%operations%'
OR job_title LIKE '% devops'
)
AND job_title_category IS NULL
;"""
update_job_categories_sci = """
UPDATE job_titles
SET job_title_category='Applied Science'
WHERE (job_title LIKE '%science%'
OR job_title LIKE '%scientist%'
OR job_title LIKE '%physics%'
OR job_title LIKE '%scientific%'
)
AND job_title_category IS NULL;
"""
update_job_categories_other = """
UPDATE job_titles
SET job_title_category='Other'
WHERE job_title_category IS NULL
;"""
update_job_rank_4 = """
UPDATE job_titles
SET job_title_rank = '4'
WHERE (job_title LIKE '% iv'
OR job_title LIKE '%iv'
OR job_title LIKE '% 4'
OR job_title LIKE '%4'
)
AND job_title_rank IS NULL
;"""
update_job_rank_3 = """
UPDATE job_titles
SET job_title_rank = '3'
WHERE (job_title LIKE '% iii'
OR job_title LIKE '%iii'
OR job_title LIKE '% 3'
OR job_title LIKE '%3'
)
AND job_title_rank IS NULL
;"""
update_job_rank_2 = """
UPDATE job_titles
SET job_title_rank = '2'
WHERE (job_title LIKE '% ii'
OR job_title LIKE '% 2'
OR job_title LIKE '%2'
)
AND job_title_rank IS NULL
;"""
update_job_rank_1 = """
UPDATE job_titles
SET job_title_rank = '1'
WHERE (job_title LIKE '% i'
OR job_title LIKE '% 1'
OR job_title LIKE '%1'
)
AND job_title_rank IS NULL
;"""
update_job_rank_sr = """
UPDATE job_titles
SET job_title_rank = 'Senior'
WHERE (job_title LIKE 'senior%'
OR job_title LIKE 'sr_%'
)
AND job_title_rank IS NULL
;"""
update_job_rank_jr = """
UPDATE job_titles
SET job_title_rank = 'Junior'
WHERE (job_title LIKE 'junior%'
OR job_title LIKE 'jr_%'
)
AND job_title_rank IS NULL
;"""
update_job_rank_intern = """
UPDATE job_titles
SET job_title_rank = 'Intern'
WHERE (job_title LIKE '%intern%'
OR job_title LIKE '%internship%'
)
AND job_title_rank IS NULL
;"""
insert_salaries = """
INSERT INTO salaries
(
salary_id,
employer_key,
location_key,
job_title_key,
submitted_at,
total_experience_years,
employer_experience_years,
annual_base_pay,
signing_bonus,
annual_bonus,
stock_value_bonus,
gender,
comments
)
SELECT
stg.id as salary_id,
em.employer_key as employer_key,
lo.location_key as location_key,
jt.job_title_key as job_title_key,
stg.ts::TIMESTAMP as submitted_at,
stg.total_experience_years::DECIMAL(6,2),
stg.employer_experience_years::DECIMAL(6,2),
stg.annual_base_pay::DECIMAL(12,2),
stg.signing_bonus::DECIMAL(12,2),
stg.annual_bonus::DECIMAL(12,2),
stg.stock_value_bonus,
stg.gender::gender_type,
stg.comments
FROM salary_staging stg
LEFT JOIN employers em ON lower(trim(stg.employer))=em.employer_name
LEFT JOIN locations lo ON lower(trim(stg.location))=lo.location_name
LEFT JOIN job_titles jt ON lower(trim(stg.job_title))=jt.job_title
WHERE stg.id NOT IN (SELECT salary_id FROM salaries);
;"""
with psycopg2.connect(host='localhost') as conn:
with conn.cursor() as cur:
cur.execute(update_employers)
cur.execute(update_locations)
cur.execute(update_location_countries)
cur.execute(update_location_states)
cur.execute(update_location_ca_states)
cur.execute(update_location_cities)
cur.execute(update_zip_codes)
cur.execute(update_lat_long_by_state)
cur.execute(update_lat_long_by_country)
cur.execute(update_county)
cur.execute(update_job_titles)
cur.execute(update_job_categories_exec)
cur.execute(update_job_categories_data)
cur.execute(update_job_categories_web)
cur.execute(update_job_categories_sw)
cur.execute(update_job_categories_eng)
cur.execute(update_job_categories_ops)
cur.execute(update_job_categories_sci)
cur.execute(update_job_categories_other)
cur.execute(update_job_rank_4)
cur.execute(update_job_rank_3)
cur.execute(update_job_rank_2)
cur.execute(update_job_rank_1)
cur.execute(update_job_rank_sr)
cur.execute(update_job_rank_jr)
cur.execute(update_job_rank_intern)
cur.execute(insert_salaries)