DatabaseCreation: views.sql

File views.sql, 4.9 KB (added by 231088, 13 hours ago)
Line 
1-- 1. AVAILABLE BOOKABLES VIEW
2-- Client searches available artists/bands/DJs
3
4CREATE OR REPLACE VIEW vw_available_bookables AS
5SELECT
6 b.bookable_id,
7 b.display_name,
8 b.bookable_type,
9
10 l.city,
11
12 g.genre_name,
13
14 av.start_datetime,
15 av.end_datetime,
16
17 av.status
18
19FROM AvailabilitySlot av
20
21JOIN Bookable b
22 ON av.bookable_id = b.bookable_id
23
24LEFT JOIN Location l
25 ON b.location_id = l.location_id
26
27LEFT JOIN BookableGenre bg
28 ON b.bookable_id = bg.bookable_id
29
30LEFT JOIN Genre g
31 ON bg.genre_id = g.genre_id
32
33WHERE av.status = 'AVAILABLE';
34
35
36
37-- 2. CLIENT BOOKING HISTORY VIEW
38-- Logged client sees all bookings
39
40CREATE VIEW vw_client_booking_history AS
41SELECT
42 cp.client_id,
43
44 bk.booking_id,
45
46 b.display_name,
47 b.bookable_type,
48
49 br.event_type,
50 br.event_date,
51
52 bk.booking_status,
53
54 p.amount,
55 p.payment_status
56
57FROM ClientProfile cp
58
59JOIN BookingRequest br
60 ON cp.client_id = br.client_id
61
62JOIN Offer o
63 ON br.request_id = o.request_id
64
65JOIN Booking bk
66 ON bk.offer_id = o.offer_id
67
68JOIN Bookable b
69 ON o.bookable_id = b.bookable_id
70
71LEFT JOIN Payment p
72 ON p.booking_id = bk.booking_id;
73
74
75
76
77-- 3. PUBLIC ARTIST PROFILE VIEW
78-- artist public page
79
80CREATE VIEW vw_public_artist_profiles AS
81SELECT
82 b.bookable_id,
83 b.display_name,
84 b.bookable_type,
85
86 l.city,
87
88 g.genre_name,
89
90 ROUND(AVG(r.rating), 2) AS average_rating,
91
92 COUNT(r.review_id) AS total_reviews
93
94FROM Bookable b
95
96LEFT JOIN Location l
97 ON b.location_id = l.location_id
98
99LEFT JOIN BookableGenre bg
100 ON b.bookable_id = bg.bookable_id
101
102LEFT JOIN Genre g
103 ON bg.genre_id = g.genre_id
104
105LEFT JOIN Offer o
106 ON b.bookable_id = o.bookable_id
107
108LEFT JOIN Booking bk
109 ON bk.offer_id = o.offer_id
110
111LEFT JOIN Review r
112 ON r.booking_id = bk.booking_id
113
114GROUP BY
115 b.bookable_id,
116 b.display_name,
117 b.bookable_type,
118 l.city,
119 g.genre_name;
120
121
122
123-- 4. TRENDING BOOKABLES VIEW
124-- Homepage recommendations
125
126CREATE VIEW vw_trending_bookables AS
127SELECT
128 b.bookable_id,
129 b.display_name,
130 b.bookable_type,
131
132 COUNT(bk.booking_id) AS total_bookings,
133
134 ROUND(AVG(r.rating), 2) AS average_rating
135
136FROM Bookable b
137
138LEFT JOIN Offer o
139 ON b.bookable_id = o.bookable_id
140
141LEFT JOIN Booking bk
142 ON bk.offer_id = o.offer_id
143
144LEFT JOIN Review r
145 ON r.booking_id = bk.booking_id
146
147GROUP BY
148 b.bookable_id,
149 b.display_name,
150 b.bookable_type;
151
152
153
154
155-- 5. ARTIST DASHBOARD VIEW
156-- Artist homepage
157
158CREATE VIEW vw_artist_dashboard AS
159SELECT
160 b.bookable_id,
161 b.display_name,
162
163 COUNT(bk.booking_id) AS total_bookings,
164
165 SUM(
166 CASE
167 WHEN p.payment_status = 'PAID'
168 THEN p.amount
169 ELSE 0
170 END
171 ) AS total_earnings,
172
173 ROUND(AVG(r.rating), 2) AS average_rating
174
175FROM Bookable b
176
177LEFT JOIN Offer o
178 ON b.bookable_id = o.bookable_id
179
180LEFT JOIN Booking bk
181 ON bk.offer_id = o.offer_id
182
183LEFT JOIN Payment p
184 ON p.booking_id = bk.booking_id
185
186LEFT JOIN Review r
187 ON r.booking_id = bk.booking_id
188
189GROUP BY
190 b.bookable_id,
191 b.display_name;
192
193
194
195-- 6. ARTIST SCHEDULE VIEW
196-- Artist availability page
197
198CREATE VIEW vw_artist_schedule AS
199SELECT
200 b.bookable_id,
201 b.display_name,
202
203 av.slot_id,
204
205 av.start_datetime,
206 av.end_datetime,
207
208 av.status
209
210FROM Bookable b
211
212JOIN AvailabilitySlot av
213 ON b.bookable_id = av.bookable_id;
214
215
216
217-- 7. ARTIST REVIEWS VIEW
218-- Artist reviews page
219
220CREATE VIEW vw_artist_reviews AS
221SELECT
222 b.bookable_id,
223 b.display_name,
224
225 r.review_id,
226
227 r.rating,
228 r.comment
229
230FROM Bookable b
231
232JOIN Offer o
233 ON b.bookable_id = o.bookable_id
234
235JOIN Booking bk
236 ON bk.offer_id = o.offer_id
237
238JOIN Review r
239 ON r.booking_id = bk.booking_id;
240
241
242
243
244
245-- 8. ARTIST INCOMING REQUESTS VIEW
246-- Artist sees incoming booking requests
247
248CREATE VIEW vw_artist_incoming_requests AS
249SELECT
250 b.bookable_id,
251 b.display_name,
252
253 br.request_id,
254
255 br.event_type,
256 br.event_date,
257
258 l.city,
259
260 o.total_price
261
262FROM Bookable b
263
264JOIN Offer o
265 ON b.bookable_id = o.bookable_id
266
267JOIN BookingRequest br
268 ON o.request_id = br.request_id
269
270LEFT JOIN Location l
271 ON br.location_id = l.location_id;
272
273
274
275-- 9.
276
277CREATE VIEW vw_pending_payments AS
278SELECT
279 p.payment_id,
280 bk.booking_id,
281 b.bookable_id,
282 b.display_name,
283 p.amount,
284 p.payment_status,
285 br.event_date,
286 l.city
287
288FROM Payment p
289
290JOIN Booking bk
291 ON p.booking_id = bk.booking_id
292
293JOIN Offer o
294 ON bk.offer_id = o.offer_id
295
296JOIN Bookable b
297 ON o.bookable_id = b.bookable_id
298
299JOIN BookingRequest br
300 ON o.request_id = br.request_id
301
302LEFT JOIN Location l
303 ON br.location_id = l.location_id
304
305WHERE p.payment_status = 'PENDING';