DatabaseCreation: 03_database_view.sql

File 03_database_view.sql, 5.5 KB (added by 231184, 9 days ago)

databaseViews

Line 
1-- =========================================
2-- 1. CUSTOMER PROFILE VIEW
3-- =========================================
4DROP VIEW IF EXISTS customer_profile_view;
5
6CREATE VIEW customer_profile_view AS
7SELECT
8 u.user_id,
9 u.email,
10 c.customer_id,
11 c.first_name,
12 c.last_name,
13 c.phone,
14
15 COUNT(DISTINCT a.appointment_id) AS total_appointments,
16 COUNT(DISTINCT a.business_id) AS businesses_visited,
17 AVG(r.rating) AS avg_given_rating,
18 COUNT(r.review_id) AS total_reviews_written,
19 MAX(a.created_at) AS last_appointment_date
20
21FROM customer c
22 JOIN "user" u ON c.user_id = u.user_id
23 LEFT JOIN appointment a ON c.customer_id = a.customer_id
24 LEFT JOIN review r ON c.customer_id = r.customer_id
25
26GROUP BY
27 u.user_id,
28 u.email,
29 c.customer_id,
30 c.first_name,
31 c.last_name,
32 c.phone;
33
34
35-- =========================================
36-- 2. CUSTOMER APPOINTMENTS VIEW
37-- =========================================
38DROP VIEW IF EXISTS customer_appointments_view;
39
40CREATE VIEW customer_appointments_view AS
41SELECT
42 a.appointment_id,
43 a.status,
44 a.created_at,
45
46 c.customer_id,
47 c.first_name || ' ' || c.last_name AS customer_name,
48
49 b.business_id,
50 b.name AS business_name,
51
52 s.service_id,
53 s.name AS service_name,
54
55 e.employee_id,
56 e.first_name || ' ' || e.last_name AS employee_name,
57
58 ts.date,
59 ts.start_time,
60 ts.end_time,
61
62 bl.city
63
64FROM appointment a
65 JOIN customer c ON a.customer_id = c.customer_id
66 JOIN business b ON a.business_id = b.business_id
67 JOIN service s ON a.service_id = s.service_id
68 JOIN employee e ON a.employee_id = e.employee_id
69 JOIN time_slot ts ON a.slot_id = ts.slot_id
70 LEFT JOIN business_location bl ON b.business_id = bl.business_id
71
72WHERE a.status <> 'cancelled';
73
74
75-- =========================================
76-- 3. AVAILABLE SLOTS VIEW
77-- =========================================
78DROP VIEW IF EXISTS available_slots;
79
80CREATE VIEW available_slots AS
81SELECT
82 ts.slot_id,
83 ts.date,
84 ts.start_time,
85 ts.end_time,
86
87 b.business_id,
88 b.name AS business_name,
89
90 e.employee_id,
91 e.first_name || ' ' || e.last_name AS employee_name,
92
93 s.service_id,
94 s.name AS service_name,
95
96 bs.price,
97 bs.duration_minutes
98
99FROM time_slot ts
100 JOIN business b ON ts.business_id = b.business_id
101 JOIN employee e ON ts.employee_id = e.employee_id
102 JOIN employee_service es ON e.employee_id = es.employee_id
103 JOIN service s ON es.service_id = s.service_id
104 JOIN business_service bs
105 ON bs.business_id = b.business_id
106 AND bs.service_id = s.service_id
107
108WHERE ts.is_available = TRUE
109 AND bs.is_active = TRUE;
110
111
112-- =========================================
113-- 4. BUSINESS SERVICES VIEW
114-- =========================================
115DROP VIEW IF EXISTS business_services_view;
116
117CREATE VIEW business_services_view AS
118SELECT
119 b.business_id,
120 b.name AS business_name,
121 b.description,
122 s.name AS service_name,
123 bs.price,
124 bs.duration_minutes
125FROM business b
126 JOIN business_service bs ON b.business_id = bs.business_id
127 JOIN service s ON bs.service_id = s.service_id
128WHERE bs.is_active = TRUE;
129
130
131-- =========================================
132-- 5. BUSINESS OVERVIEW VIEW
133-- =========================================
134DROP VIEW IF EXISTS business_overview;
135
136CREATE VIEW business_overview AS
137SELECT
138 b.business_id,
139 b.name,
140 b.email,
141 COUNT(DISTINCT be.employee_id) AS employee_count,
142 COUNT(DISTINCT bm.manager_id) AS manager_count
143FROM business b
144 LEFT JOIN business_employee be ON b.business_id = be.business_id
145 LEFT JOIN business_manager bm ON b.business_id = bm.business_id
146GROUP BY b.business_id, b.name, b.email;
147
148
149-- =========================================
150-- 6. REVIEW SUMMARY VIEW
151-- =========================================
152-- DA KAZHEME DEKA E MATERIJALIZIRAN VIEW
153DROP VIEW IF EXISTS review_summary;
154
155CREATE VIEW review_summary AS
156SELECT
157 b.business_id,
158 b.name AS business_name,
159 AVG(r.rating) AS avg_rating,
160 COUNT(r.review_id) AS total_reviews
161FROM review r
162 JOIN business b ON r.business_id = b.business_id
163GROUP BY b.business_id, b.name;
164
165
166-- =========================================
167-- 7. REVIEW DETAILS VIEW
168-- =========================================
169-- MATERIJALIZIRAN VIEW
170DROP VIEW IF EXISTS review_details;
171
172CREATE VIEW review_details AS
173SELECT
174 r.review_id,
175 b.name AS business_name,
176 r.rating,
177 r.comment,
178 r.created_at
179FROM review r
180 JOIN business b ON r.business_id = b.business_id;
181
182
183-- =========================================
184-- 8. RESCHEDULE OVERVIEW VIEW
185-- =========================================
186DROP VIEW IF EXISTS reschedule_overview;
187
188CREATE VIEW reschedule_overview AS
189SELECT
190 rr.request_id,
191 rr.status,
192 rr.reason,
193 ts_old.date AS old_date,
194 ts_old.start_time AS old_time,
195 ts_new.date AS new_date,
196 ts_new.start_time AS new_time
197FROM reschedule_request rr
198 JOIN time_slot ts_old ON rr.old_slot_id = ts_old.slot_id
199 JOIN time_slot ts_new ON rr.new_slot_id = ts_new.slot_id;
200
201
202-- =========================================
203-- 9. BUSINESS LOCATION VIEW
204-- =========================================
205DROP VIEW IF EXISTS business_location_view;
206
207CREATE VIEW business_location_view AS
208SELECT
209 b.business_id,
210 b.name AS business_name,
211 bl.address,
212 bl.city,
213 bl.phone
214FROM business_location bl
215 JOIN business b ON bl.business_id = b.business_id;