-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery.sql
373 lines (254 loc) · 8.61 KB
/
SQLQuery.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
--ADDING CITY NAMES
select * from resto..Berlin_resto$
ALTER TABLE resto..Berlin_resto$
ADD City varchar(255)
UPDATE resto..Berlin_resto$
SET City = 'Berlin';
--------------------------------------------------
select * from resto..Dortmund_resto$
ALTER TABLE resto..Dortmund_resto$
ADD City varchar(255)
UPDATE resto..Dortmund_resto$
SET City = 'Dortmund';
--------------------------------------------------
select * from resto..Duesseldorf_resto$
ALTER TABLE resto..Duesseldorf_resto$
ADD City varchar(255)
UPDATE resto..Duesseldorf_resto$
SET City = 'Duesseldorf';
--------------------------------------------------
select * from resto..Essen_resto$
ALTER TABLE resto..Essen_resto$
ADD City varchar(255)
UPDATE resto..Essen_resto$
SET City = 'Essen';
--------------------------------------------------
select * from resto..Frankfurt_resto$
ALTER TABLE resto..Frankfurt_resto$
ADD City varchar(255)
UPDATE resto..Frankfurt_resto$
SET City = 'Frankfurt';
--------------------------------------------------
select * from resto..Hamburg_resto$
ALTER TABLE resto..Hamburg_resto$
ADD City varchar(255)
UPDATE resto..Hamburg_resto$
SET City = 'Hamburg';
--------------------------------------------------
select * from resto..Hannover_resto$
ALTER TABLE resto..Hannover_resto$
ADD City varchar(255)
UPDATE resto..Hannover_resto$
SET City = 'Hannover';
--------------------------------------------------
select * from resto..Leipzig_resto$
ALTER TABLE resto..Leipzig_resto$
ADD City varchar(255)
UPDATE resto..Leipzig_resto$
SET City = 'Leipzig';
--------------------------------------------------
select * from resto..Munich_resto$
ALTER TABLE resto..Munich_resto$
ADD City varchar(255)
UPDATE resto..Munich_resto$
SET City = 'Munich';
--------------------------------------------------
select * from resto..Stuttgart_resto$
ALTER TABLE resto..Stuttgart_resto$
ADD City varchar(255)
UPDATE resto..Stuttgart_resto$
SET City = 'Stuttgart';
--------------------------------------------------
select * from resto..Bielefeld_resto$
ALTER TABLE resto..Bielefeld_resto$
ADD City varchar(255)
UPDATE resto..Bielefeld_resto$
SET City = 'Bielefeld';
--------------------------------------------------
select * from resto..Bochum_resto$
ALTER TABLE resto..Bochum_resto$
ADD City varchar(255)
UPDATE resto..Bochum_resto$
SET City = 'Bochum';
ALTER TABLE resto..Bochum_resto$
ALTER COLUMN [min price] nvarchar(255)
ALTER TABLE resto..Bochum_resto$
ALTER COLUMN [max price] nvarchar(255)
--------------------------------------------------
select * from resto..Bonn_resto$
ALTER TABLE resto..Bonn_resto$
ADD City varchar(255)
UPDATE resto..Bonn_resto$
SET City = 'Bonn';
--------------------------------------------------
select * from resto..Dresden_resto$
ALTER TABLE resto..Dresden_resto$
ADD City varchar(255)
UPDATE resto..Dresden_resto$
SET City = 'Dresden';
--------------------------------------------------
select * from resto..Duisburg_resto$
ALTER TABLE resto..Duisburg_resto$
ADD City varchar(255)
UPDATE resto..Duisburg_resto$
SET City = 'Duisburg';
ALTER TABLE resto..Duisburg_resto$
ALTER COLUMN [min price] nvarchar(255)
ALTER TABLE resto..Duisburg_resto$
ALTER COLUMN [max price] nvarchar(255)
--------------------------------------------------
select * from resto..Mannheim_resto$
ALTER TABLE resto..Mannheim_resto$
ADD City varchar(255)
UPDATE resto..Mannheim_resto$
SET City = 'Mannheim';
--------------------------------------------------
select * from resto..Wuppertal_resto$
ALTER TABLE resto..Wuppertal_resto$
ADD City varchar(255)
UPDATE resto..Wuppertal_resto$
SET City = 'Wuppertal';
--------------------------------------------------
select * from resto..Cologne_resto$
ALTER TABLE resto..Cologne_resto$
ADD City varchar(255)
UPDATE resto..Cologne_resto$
SET City = 'Cologne';
--------------------------------------------------
select * from resto..Karlsruhe_resto$
ALTER TABLE resto..Karlsruhe_resto$
ADD City varchar(255)
UPDATE resto..Karlsruhe_resto$
SET City = 'Karlsruhe';
ALTER TABLE resto..Karlsruhe_resto$
ALTER COLUMN [min price] nvarchar(255)
ALTER TABLE resto..Karlsruhe_resto$
ALTER COLUMN [max price] nvarchar(255)
--------------------------------------------------
select * from resto..Nuremberg_resto$
ALTER TABLE resto..Nuremberg_resto$
ADD City varchar(255)
UPDATE resto..Nuremberg_resto$
SET City = 'Nuremberg';
--------------------------------------------------
--Finding the data type to be changed
SELECT
*
FROM
resto.INFORMATION_SCHEMA.TABLES;
GO
SELECT DATA_TYPE
FROM resto.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Berlin_resto$'
SELECT DATA_TYPE
FROM resto.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Karlsruhe_resto$'
SELECT COLUMN_NAME
FROM resto.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Berlin_resto$'
--Creating a new final table
drop table if exists final_table
select a.*
into final_table
from resto..Berlin_resto$ a
union all
select b.* from resto..Dortmund_resto$ b
union all
select c.* from resto..Duesseldorf_resto$ c
union all
select d.* from resto..Essen_resto$ d
union all
select e.* from resto..Frankfurt_resto$ e
union all
select f.* from resto..Hamburg_resto$ f
union all
select g.* from resto..Hannover_resto$ g
union all
select h.* from resto..Leipzig_resto$ h
union all
select i.* from resto..Munich_resto$ i
union all
select j.* from resto..Stuttgart_resto$ j
union all
select k.* from resto..Bielefeld_resto$ k
union all
select l.* from resto..Bonn_resto$ l
union all
select m.* from resto..Dresden_resto$ m
union all
select n.* from resto..Mannheim_resto$ n
union all
select o.* from resto..Wuppertal_resto$ o
union all
select p.* from resto..Cologne_resto$ p
union all
select q.* from resto..Nuremberg_resto$ q
union all
select r.* from resto..Bochum_resto$ r
union all
select s.* from resto..Duisburg_resto$ s
union all
select t.* from resto..Karlsruhe_resto$ t
select * from final_table
select city, avg(rating) from final_table group by City
select name, latitude, longitude, Bar, Italian, Japanese, American, Steakhouse, Cafe, Indian, Asian, Healthy, German,
[Central European], Mediterranean, European, [Vegan Options], [Gluten Free Options], Seafood, rating_category, top1 from
(select *, case when rank1>1 then 'Not Top 1%' else 'Top 1%'end top1 from
(select *, (ranking_position/ranking_denominator)*100 as rank1 from final_table) t1) t2
select City, count(*) from final_table
where Cafe = 'Available'
group by City
--AFTER CLUSTERING
select * from resto..Cluster
select City, count(*) cafes from final_table
where Steakhouse = 'Available'
group by City order by 2
select City, count(*) cafes from final_table
where Cafe = 'Available'
group by City order by 2
select City, a.name, a.latitude, a.longitude, Cluster from resto..Cluster a inner join final_table b on a.name=b.name
order by City
SELECT AVG(rating) cafe FROM final_table
where Cafe = 'Available'
SELECT AVG(rating) Bar FROM final_table
where Bar = 'Available'
SELECT AVG(rating) Steakhouse FROM final_table
where Steakhouse = 'Available'
SELECT AVG(rating) Seafood FROM final_table
where Seafood = 'Available'
select top 10 city, avg(rating) avg_rating from final_table
group by city order by avg(rating) desc
select count(*) Cafe from final_table
where Cafe = 'Available'
select count(*) Bar from final_table
where Bar = 'Available'
select count(*) Steakhouse from final_table
where Steakhouse = 'Available'
select City, count(*) Steakhouse from final_table
where Steakhouse = 'Available'
GROUP BY City ORDER BY count(*)
select * from final_table
SELECT AVG(rating) indian FROM final_table
where Indian = 'Available'
SELECT AVG(rating) am FROM final_table
where American = 'Available'
SELECT AVG(rating) ger FROM final_table
where German = 'Available'
SELECT AVG(rating) ita FROM final_table
where Italian = 'Available'
SELECT AVG(rating) jAP FROM final_table
where Japanese = 'Available'
SELECT AVG(rating) med FROM final_table
where Mediterranean = 'Available'
create table avg_ratings(
category varchar(100),
rating float
);
INSERT INTO avg_ratings (category, rating)
select 'American', 4.367 union all select
'Indian', 4.5 union all select
'German', 4.338 union all select
'Italian', 4.435 union all select
'Japanese', 4.442 union all select
'Mediterranean', 4.463
select * from avg_ratings