-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadData.sql
More file actions
155 lines (134 loc) · 4.62 KB
/
loadData.sql
File metadata and controls
155 lines (134 loc) · 4.62 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
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
-- Insert Users
INSERT INTO Users (user_id, first_name, last_name, year_of_birth, month_of_birth, day_of_birth, gender)
SELECT DISTINCT user_id, first_name, last_name, year_of_birth, month_of_birth, day_of_birth, gender
-- Since user_id is unique, different id + same attr will also be unique.
-- this allow attr to be the same for different user
FROM project1.Public_User_Information;
-- Insert Friends
INSERT INTO Friends (user1_id, user2_id)
SELECT DISTINCT LEAST(user1_id, user2_id), GREATEST(user1_id, user2_id)
FROM project1.Public_Are_Friends;
-- Insert Cities
INSERT INTO Cities (city_name, state_name, country_name)
SELECT DISTINCT city_name, state_name, country_name
FROM (
SELECT current_city AS city_name, current_state AS state_name, current_country AS country_name
FROM project1.Public_User_Information
UNION
SELECT hometown_city AS city_name, hometown_state AS state_name, hometown_country AS country_name
FROM project1.Public_User_Information
);
-- Insert User_Current_Cities
INSERT INTO User_Current_Cities (user_id, current_city_id)
SELECT DISTINCT user_id, city_id
FROM project1.Public_User_Information
JOIN Cities ON (Public_User_Information.current_city = Cities.city_name AND
Public_User_Information.current_state = Cities.state_name AND
Public_User_Information.current_country = Cities.country_name);
-- Insert User_Hometown_Cities
INSERT INTO User_Hometown_Cities (user_id, hometown_city_id)
SELECT DISTINCT user_id, city_id
FROM project1.Public_User_Information
JOIN Cities ON (Public_User_Information.hometown_city = Cities.city_name AND
Public_User_Information.hometown_state = Cities.state_name AND
Public_User_Information.hometown_country = Cities.country_name);
-- Insert Programs
INSERT INTO Programs (institution, concentration, degree)
SELECT DISTINCT institution_name, program_concentration, program_degree
FROM project1.Public_User_Information
WHERE institution_name IS NOT NULL;
-- Since all 3 attr will not be NULL if one of them are not NULL
-- Insert Education
INSERT INTO Education (user_id, program_id, program_year)
SELECT DISTINCT user_id, program_id, program_year
FROM project1.Public_User_Information
JOIN Programs ON (Public_User_Information.institution_name = Programs.institution AND
Public_User_Information.program_concentration = Programs.concentration AND
Public_User_Information.program_degree = Programs.degree);
-- Insert User_Events
INSERT INTO User_Events (
event_id,
event_creator_id,
event_name,
event_tagline,
event_description,
event_host,
event_type,
event_subtype,
event_address,
event_city_id,
event_start_time,
event_end_time
)
SELECT DISTINCT
event_id,
event_creator_id,
event_name,
event_tagline,
event_description,
event_host,
event_type,
event_subtype,
event_address,
city_id,
event_start_time,
event_end_time
FROM
project1.Public_Event_Information
JOIN
Cities ON (
Public_Event_Information.event_city = Cities.city_name AND
Public_Event_Information.event_state = Cities.state_name AND
Public_Event_Information.event_country = Cities.country_name);
-- Turn Off Autocommit for Circular Dependencies
SET AUTOCOMMIT OFF;
-- Insert Albums
INSERT INTO Albums (
album_id,
album_owner_id,
album_name,
album_created_time,
album_modified_time,
album_link,
album_visibility,
cover_photo_id
)
SELECT DISTINCT
album_id,
owner_id AS album_owner_id,
album_name,
album_created_time,
album_modified_time,
album_link,
/* CASE
WHEN album_visibility = 'Everyone' THEN 'Everyone'
WHEN album_visibility = 'Friends' THEN 'Friends'
WHEN album_visibility = 'Friends_Of_Friends' THEN 'Friends_Of_Friends'
WHEN album_visibility = 'Myself' THEN 'Myself'
ELSE NULL
END AS album_visibility, */
album_visibility,
cover_photo_id
FROM project1.Public_Photo_Information;
-- Insert Photos
INSERT INTO Photos (photo_id, album_id, photo_caption, photo_created_time, photo_modified_time, photo_link)
SELECT DISTINCT photo_id, album_id, photo_caption, photo_created_time, photo_modified_time, photo_link
FROM project1.Public_Photo_Information;
-- Turn On Autocommit
COMMIT;
SET AUTOCOMMIT ON;
-- Tags
INSERT INTO Tags (
tag_photo_id,
tag_subject_id,
tag_created_time,
tag_x,
tag_y)
SELECT DISTINCT
photo_id AS tag_photo_id,
tag_subject_id,
tag_created_time,
tag_x_coordinate AS tag_x,
tag_y_coordinate AS tag_y
FROM
project1.Public_Tag_Information;