-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathextract.sql
154 lines (140 loc) · 3.74 KB
/
extract.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
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
--- Create a function to save data in every table
--- Select distinct data together with it's
--- save in save regions func
CREATE OR REPLACE FUNCTION save_regions(r_code int, r_name text, country int, general int ) RETURNS void AS $$
BEGIN
EXECUTE
'
INSERT INTO regions(region_code, region_name, country_id, general_locations_id) VALUES($1, $2, $3, $4)
ON CONFLICT (region_code) DO NOTHING
'
USING r_code, r_name, country, general;
END;
$$ LANGUAGE plpgsql;
WITH
distinct_regions AS (
SELECT DISTINCT(region), country_id, regioncode, id FROM general
),
regions_save AS (
SELECT save_regions(
F.regioncode,
F.region,
F.country_id,
F.id
)
FROM distinct_regions AS F
)
SELECT * FROM regions_save;
CREATE OR REPLACE FUNCTION save_districts(d_code int, d_name text, region int, country int, general int) RETURNS void AS $$
BEGIN
EXECUTE
'
INSERT INTO districts(district_code, district_name, region_id, country_id, general_locations_id) VALUES($1, $2, $3, $4, $5)
ON CONFLICT (district_code) DO NOTHING
'
USING d_code, d_name, region, country, general;
END;
$$ LANGUAGE plpgsql;
WITH
distinct_districts AS (
SELECT DISTINCT(district), districtcode, country_id, regioncode, id FROM general
),
district_save AS (
SELECT save_districts(
U.districtcode,
U.district,
U.regioncode,
U.country_id,
U.id
) FROM distinct_districts AS U
)
SELECT * FROM district_save;
CREATE OR REPLACE FUNCTION save_wards(w_code int, w_name text, district int, region int, country int, general int) RETURNS void AS $$
BEGIN
EXECUTE
'
INSERT INTO wards(ward_code, ward_name, district_id, region_id, country_id, general_locations_id) VALUES($1, $2, $3, $4, $5, $6)
ON CONFLICT (ward_code) DO NOTHING
'
USING w_code, w_name, district, region, country, general;
END;
$$ LANGUAGE plpgsql;
WITH
distinct_wards AS (
SELECT DISTINCT(ward), wardcode, regioncode, districtcode, country_id, id
FROM general
),
ward_save AS (
SELECT save_wards(
C.wardcode,
C.ward,
C.districtcode,
C.regioncode,
C.country_id,
C.id
)
FROM distinct_wards AS C
)
SELECT * FROM ward_save;
CREATE OR REPLACE FUNCTION save_places(place text, ward int, district int, region int, country int, general int) RETURNS void AS $$
BEGIN
EXECUTE
'
INSERT INTO places(
place_name,
ward_id,
district_id,
region_id,
country_id,
general_locations_id
) VALUES($1, $2, $3, $4, $5, $6)
'
USING place, ward, district, region, country, general;
END;
$$ LANGUAGE plpgsql;
WITH
distinct_streets AS (
SELECT
DISTINCT(wardcode),
street,
regioncode,
districtcode,
country_id,
id
FROM general
),
places_save_streets AS (
SELECT save_places(
K.street,
K.wardcode,
K.districtcode,
K.regioncode,
K.country_id,
K.id
) FROM distinct_streets AS K
WHERE street IS NOT NULL
)
SELECT * FROM places_save_streets;
WITH
distinct_places AS (
SELECT
DISTINCT(wardcode),
places,
regioncode,
districtcode,
country_id,
id
FROM general
),
places_save_places AS (
SELECT save_places(
R.places,
R.wardcode,
R.districtcode,
R.regioncode,
R.country_id,
R.id
) FROM distinct_places AS R
WHERE places IS NOT NULL
)
SELECT * FROM places_save_places;