QueryOptimization: views-final.txt

File views-final.txt, 6.1 KB (added by 231504, 4 days ago)
Line 
1-- 1. attraction_popularity_view
2
3CREATE VIEW attraction_popularity_view AS
4SELECT
5 a.name,
6 COUNT(f.trip_participant_id) AS total_facultatives,
7 AVG(a.entry_fee) AS average_entry_fee
8FROM attractions a
9JOIN facultatives f
10 ON a.facultative_id = f.id
11GROUP BY
12 a.id,
13 a.name;
14
15-- 2. flight_revenue_view
16
17CREATE VIEW flight_revenue_view AS
18SELECT
19 fb.flight_route_id,
20 COUNT(*) AS total_bookings,
21 SUM(fb.price) AS total_revenue,
22 AVG(fb.price) AS average_ticket_price
23FROM flight_bookings fb
24GROUP BY fb.flight_route_id;
25
26-- 3. hotel_booking_stats_view
27
28CREATE VIEW hotel_booking_stats_view AS
29SELECT
30 h.id AS hotel_id,
31 h.name AS hotel_name,
32 h.star_rating,
33
34 (
35 SELECT COUNT(*)
36 FROM hotel_rooms hr
37 WHERE hr.hotel_id = h.id
38 ) AS total_rooms,
39
40 (
41 SELECT COUNT(*)
42 FROM hotel_rooms hr
43 JOIN trip_participants_hotel_rooms tphr
44 ON hr.id = tphr.hotel_room_id
45 WHERE hr.hotel_id = h.id
46 ) AS total_guests,
47
48 (
49 SELECT AVG(hr.price)
50 FROM hotel_rooms hr
51 WHERE hr.hotel_id = h.id
52 ) AS average_room_price,
53
54 (
55 SELECT MIN(tphr.check_in)
56 FROM hotel_rooms hr
57 JOIN trip_participants_hotel_rooms tphr
58 ON hr.id=tphr.hotel_room_id
59 WHERE hr.hotel_id=h.id
60 ) AS earliest_check_in,
61
62 (
63 SELECT MAX(tphr.check_out)
64 FROM hotel_rooms hr
65 JOIN trip_participants_hotel_rooms tphr
66 ON hr.id=tphr.hotel_room_id
67 WHERE hr.hotel_id=h.id
68 ) AS latest_check_out
69
70FROM hotels h;
71
72-- 4. review_photo_engagement_view
73
74CREATE VIEW review_photo_engagement_view AS
75SELECT
76 r.id AS review_id,
77
78 r.rating,
79
80 r.created_at,
81
82 tp.user_id,
83
84 COUNT(p.id) AS photo_count,
85
86 LENGTH(r.comment) AS comment_length,
87
88 CASE
89
90 WHEN r.rating >= 4 THEN 'positive'
91
92 WHEN r.rating = 3 THEN 'neutral'
93
94 ELSE 'negative'
95
96 END AS sentiment
97
98FROM reviews r
99
100JOIN trip_participants tp
101 ON r.trip_participant_id = tp.id
102
103LEFT JOIN photos_url p
104 ON r.id = p.review_id
105
106GROUP BY
107 r.id,
108 r.rating,
109 r.created_at,
110 tp.user_id,
111 r.comment;
112
113-- 5. transport_usage_stats_view
114
115CREATE VIEW transport_usage_stats_view AS
116
117SELECT
118
119 fb.flight_route_id AS route_id,
120
121 'FLIGHT' AS transport_type,
122
123 COUNT(*) AS bookings,
124
125 COUNT(*) AS passengers,
126
127 SUM(fb.price) AS total_revenue,
128
129 AVG(fb.price) AS avg_price
130
131FROM flight_bookings fb
132
133GROUP BY fb.flight_route_id
134
135UNION ALL
136
137SELECT
138
139 bb.route_id,
140
141 'BUS',
142
143 COUNT(*),
144
145 COUNT(*),
146
147 SUM(bb.price),
148
149 AVG(bb.price)
150
151FROM bus_bookings bb
152
153GROUP BY bb.route_id;
154
155-- 6. trip_duration_stats_view
156
157CREATE VIEW trip_duration_stats_view AS
158SELECT
159 t.id AS trip_id,
160 AVG(te.end_date - te.start_date) AS avg_duration,
161 MIN(te.end_date - te.start_date) AS shortest_trip,
162 MAX(te.end_date - te.start_date) AS longest_trip
163FROM trips t
164JOIN trip_executions te
165 ON t.id = te.trip_id
166GROUP BY t.id;
167
168-- 7. trip_execution_pacing_view
169
170CREATE VIEW trip_execution_pacing_view AS
171
172SELECT
173
174te.id,
175
176t.name,
177
178te.start_date,
179
180te.end_date,
181
182(te.end_date-te.start_date) AS duration_days,
183
184(
185SELECT COUNT(*)
186
187FROM trip_locations tl
188
189WHERE tl.trip_id=t.id
190
191) AS total_location_visits,
192
193ROUND(
194
195(
196SELECT COUNT(*)
197
198FROM trip_locations tl
199
200WHERE tl.trip_id=t.id
201
202)::numeric
203
204/
205
206NULLIF((te.end_date-te.start_date),0),
207
2082
209
210) AS visits_per_day,
211
212ROUND(
213
214(
215SELECT COUNT(DISTINCT tl.location_id)
216
217FROM trip_locations tl
218
219WHERE tl.trip_id=t.id
220
221)::numeric
222
223/
224
225NULLIF((te.end_date-te.start_date),0),
226
2272
228
229) AS unique_locations_per_day
230
231FROM trip_executions te
232
233JOIN trips t
234
235ON te.trip_id=t.id;
236
237-- 8. trip_execution_stats_view
238
239CREATE VIEW trip_execution_stats_view AS
240
241SELECT
242
243te.id AS trip_execution_id,
244
245t.name,
246
247te.start_date,
248
249te.end_date,
250
251(te.end_date-te.start_date) AS duration_days,
252
253(
254SELECT COUNT(*)
255
256FROM trip_participants tp
257
258WHERE tp.trip_execution_id=te.id
259
260) AS participant_count,
261
262(
263SELECT COUNT(*)
264
265FROM trip_locations tl
266
267WHERE tl.trip_id=t.id
268
269) AS visited_locations,
270
271(
272SELECT AVG(r.rating)
273
274FROM trip_participants tp
275
276JOIN reviews r
277
278ON tp.id=r.trip_participant_id
279
280WHERE tp.trip_execution_id=te.id
281
282) AS average_rating
283
284FROM trip_executions te
285
286JOIN trips t
287
288ON te.trip_id=t.id;
289
290-- 9. trip_locations_by_day_view
291
292CREATE VIEW trip_locations_by_day_view AS
293SELECT
294
295day_number,
296
297COUNT(location_id) AS total_locations,
298
299COUNT(DISTINCT trip_id) AS total_trips
300
301FROM trip_locations
302
303GROUP BY day_number;
304
305-- 10. trip_participant_count_view
306
307CREATE VIEW trip_participant_count_view AS
308SELECT
309
310t.id AS trip_id,
311
312t.name AS trip_name,
313
314COUNT(tp.id) AS participants
315
316FROM trips t
317
318LEFT JOIN trip_executions te
319ON t.id=te.trip_id
320
321LEFT JOIN trip_participants tp
322ON te.id=tp.trip_execution_id
323
324GROUP BY
325
326t.id,
327
328t.name;
329
330-- 11. trip_participant_status_view
331
332CREATE VIEW trip_participant_status_view AS
333SELECT
334
335status,
336
337COUNT(*) AS total_participants
338
339FROM trip_participants
340
341GROUP BY status;
342
343-- 12. user_trip_review_stats_view
344
345CREATE VIEW user_trip_review_stats_view AS
346SELECT
347
348u.id,
349
350u.username,
351
352(
353SELECT COUNT(DISTINCT tp.trip_execution_id)
354
355FROM trip_participants tp
356
357WHERE tp.user_id=u.id
358
359) AS trips_joined,
360
361(
362SELECT COUNT(*)
363
364FROM trip_participants tp
365
366JOIN reviews r
367ON tp.id=r.trip_participant_id
368
369WHERE tp.user_id=u.id
370
371) AS total_reviews,
372
373(
374SELECT AVG(r.rating)
375
376FROM trip_participants tp
377
378JOIN reviews r
379ON tp.id=r.trip_participant_id
380
381WHERE tp.user_id=u.id
382
383) AS average_rating,
384
385(
386SELECT MIN(r.rating)
387
388FROM trip_participants tp
389
390JOIN reviews r
391ON tp.id=r.trip_participant_id
392
393WHERE tp.user_id=u.id
394
395) AS lowest_rating,
396
397(
398SELECT MAX(r.rating)
399
400FROM trip_participants tp
401
402JOIN reviews r
403ON tp.id=r.trip_participant_id
404
405WHERE tp.user_id=u.id
406
407) AS highest_rating
408
409FROM users u;