-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_views.sql
283 lines (266 loc) · 8.06 KB
/
03_views.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
USE vetdb;
-- animals basic view ---------------------------
CREATE OR REPLACE VIEW animals_basic_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
a.a_type 'TYPE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
CASE a.a_requeststatus
WHEN 0 THEN 'AVAILABLE'
WHEN 1 THEN 'REQUESTED'
WHEN 2 THEN 'ACCEPT_BY_ADMIN'
WHEN 3 THEN 'READY'
WHEN 4 THEN 'CANCEL'
WHEN 5 THEN 'REJECT'
END 'REQUEST_STATUS',
CONCAT( (CASE WHEN o.o_firstname IS NULL THEN "" ELSE o.o_firstname END),
(CASE WHEN o.o_middlename IS NULL THEN "" ELSE CONCAT(" ", o.o_middlename ) END),
(CASE WHEN o.o_lastname IS NULL THEN "" ELSE CONCAT(" ", o.o_lastname ) END)) 'OWNER',
o.o_address 'LOCATION'
FROM
animals a
LEFT JOIN owners o ON o.o_ownerid = a.a_ownerid
ORDER BY
a.a_animalid;
-- ----------------------------------------------
-- animals profile view -------------------------
CREATE OR REPLACE VIEW animals_profile_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
a.a_type 'TYPE',
a.a_region 'REGION',
a.a_sex 'SEX',
a.a_birthdate 'BIRTH_DATE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
a.a_profilepic 'PROFILE_PIC',
CONCAT( (CASE WHEN o.o_firstname IS NULL THEN "" ELSE o.o_firstname END),
(CASE WHEN o.o_middlename IS NULL THEN "" ELSE CONCAT(" ", o.o_middlename ) END),
(CASE WHEN o.o_lastname IS NULL THEN "" ELSE CONCAT(" ", o.o_lastname ) END)) 'OWNER',
o.o_emailid 'EMAIL',
o.o_contactnumber 'CONTACT',
o.o_address 'LOCATION',
a.a_tattoonum 'TATTOO',
a.a_citytattoo 'CITY_TATTOO',
a.a_rfidnumber 'RFID',
a.a_microchipnumber 'MICROCHIP',
a.a_coatcolor 'COAT_COLOR',
a.a_continuousmedication 'MEDICATION',
a.a_distinctfeature 'DISTINCT_FEATURE'
FROM
animals a
LEFT JOIN owners o ON o.o_ownerid = a.a_ownerid
ORDER BY
a.a_animalid, o.o_ownerid;
-- ----------------------------------------------
-- animals weights view -------------------------
CREATE OR REPLACE VIEW animals_weights_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
-- a.a_type 'TYPE',
-- a.a_region 'REGION',
a.a_distinctfeature 'DISTINCT_FEATURE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
w.w_massinkg 'WEIGHT_KG',
w.w_recorddate 'RECORD_DATE',
CONCAT( (CASE WHEN u.u_firstname IS NULL THEN "" ELSE u.u_firstname END),
(CASE WHEN u.u_middlename IS NULL THEN "" ELSE CONCAT(" ", u.u_middlename ) END),
(CASE WHEN u.u_lastname IS NULL THEN "" ELSE CONCAT(" ", u.u_lastname ) END)) 'RECORDER_NAME',
u.u_emailid 'RECORDER_EMAIL'
FROM
animals a
RIGHT JOIN weights w ON w.w_animalid = a.a_animalid
LEFT JOIN users u ON u.u_userid = w.w_recordedby
ORDER BY
a.a_animalid, w.w_recorddate DESC;
-- ----------------------------------------------
-- animals photos view --------------------------
CREATE OR REPLACE VIEW animals_photos_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
a.a_distinctfeature 'DISTINCT_FEATURE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
p.p_photolink 'PHOTO_LINK',
p.p_alttext 'ALT_TEXT',
p.p_photodesc 'PHOTO_DESC',
p.p_uploaddate 'UPLOAD_DATE',
CONCAT( (CASE WHEN u.u_firstname IS NULL THEN "" ELSE u.u_firstname END),
(CASE WHEN u.u_middlename IS NULL THEN "" ELSE CONCAT(" ", u.u_middlename ) END),
(CASE WHEN u.u_lastname IS NULL THEN "" ELSE CONCAT(" ", u.u_lastname ) END)) 'UPLOADER_NAME',
u.u_emailid 'UPLOADER_EMAIL'
FROM
animals a
RIGHT JOIN photos p ON p.p_animalid = a.a_animalid
LEFT JOIN users u ON u.u_userid = p.p_uploader
ORDER BY
a.a_animalid, p.p_uploaddate DESC;
-- ----------------------------------------------
-- animals comments view ------------------------
CREATE OR REPLACE VIEW animals_comments_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
-- a.a_type 'TYPE',
-- a.a_region 'REGION',
a.a_distinctfeature 'DISTINCT_FEATURE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
c.c_commentdesc 'COMMENT',
c.c_commentdate 'COMMENT_DATE',
CONCAT( (CASE WHEN u.u_firstname IS NULL THEN "" ELSE u.u_firstname END),
(CASE WHEN u.u_middlename IS NULL THEN "" ELSE CONCAT(" ", u.u_middlename ) END),
(CASE WHEN u.u_lastname IS NULL THEN "" ELSE CONCAT(" ", u.u_lastname ) END)) 'COMMENTER_NAME',
u.u_emailid 'COMMENTER_EMAIL'
FROM
animals a
RIGHT JOIN comments c ON c.c_animalid = a.a_animalid
LEFT JOIN users u ON u.u_userid = c.c_commenter
ORDER BY
a.a_animalid, c.c_commentdate DESC;
-- ----------------------------------------------
-- animals issues view --------------------------
CREATE OR REPLACE VIEW animals_issues_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
-- a.a_type 'TYPE',
-- a.a_region 'REGION',
a.a_distinctfeature 'DISTINCT_FEATURE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
i.i_issuedesc 'ISSUE',
i.i_detecteddate 'DETECTION_DATE',
CONCAT( (CASE WHEN u.u_firstname IS NULL THEN "" ELSE u.u_firstname END),
(CASE WHEN u.u_middlename IS NULL THEN "" ELSE CONCAT(" ", u.u_middlename ) END),
(CASE WHEN u.u_lastname IS NULL THEN "" ELSE CONCAT(" ", u.u_lastname ) END)) 'RAISEDBY_NAME',
u.u_emailid 'RAISEDBY_EMAIL'
FROM
animals a
RIGHT JOIN issues i ON i.i_animalid = a.a_animalid
LEFT JOIN users u ON u.u_userid = i.i_raisedby
ORDER BY
a.a_animalid, i.i_detecteddate DESC;
-- ----------------------------------------------
-- animals treatments view ----------------------
CREATE OR REPLACE VIEW animals_treatments_view AS
SELECT
a.a_animalid 'ID',
a.a_name 'NAME',
a.a_species 'SPECIES',
a.a_subspecies 'SUBSPECIES',
a.a_breed 'BREED',
-- a.a_type 'TYPE',
-- a.a_region 'REGION',
a.a_distinctfeature 'DISTINCT_FEATURE',
CASE a.a_status
WHEN 0 THEN 'GREEN'
WHEN 1 THEN 'YELLOW'
WHEN 2 THEN 'RED'
WHEN 3 THEN 'INACTIVE'
END 'STATUS',
t.t_treatmentdesc 'TREATMENT',
t.t_drugname 'DRUG_USED',
t.t_drugdose 'DRUG_DOSE',
t.t_deliverymethod 'DRUG_DELIVERY',
t.t_treatmentdate 'TREATMENT_DATE',
CONCAT( (CASE WHEN u.u_firstname IS NULL THEN "" ELSE u.u_firstname END),
(CASE WHEN u.u_middlename IS NULL THEN "" ELSE CONCAT(" ", u.u_middlename ) END),
(CASE WHEN u.u_lastname IS NULL THEN "" ELSE CONCAT(" ", u.u_lastname ) END)) 'TREATEDBY_NAME',
u.u_emailid 'TREATEDBY_EMAIL'
FROM
animals a
RIGHT JOIN treatments t ON t.t_animalid = a.a_animalid
LEFT JOIN users u ON u.u_userid = t.t_treatedby
ORDER BY
a.a_animalid, t.t_treatmentdate DESC;
-- ----------------------------------------------
-- owners view ----------------------------------
CREATE OR REPLACE VIEW owners_view AS
SELECT
o.o_ownerid 'ID',
o.o_firstname 'FIRST_NAME',
o.o_middlename 'MID_NAME',
o.o_lastname 'LAST_NAME',
o.o_emailid 'EMAIL',
o.o_contactnumber 'PHONE_NUMBER',
o.o_address 'LOCATION'
FROM
owners o
ORDER BY o.o_ownerid;
-- ----------------------------------------------
-- users view -----------------------------------
CREATE OR REPLACE VIEW users_view AS
SELECT
u.u_userid 'ID',
u.u_firstname 'FIRST_NAME',
u.u_middlename 'MID_NAME',
u.u_lastname 'LAST_NAME',
u.u_emailid 'EMAIL',
u.u_passwordhash 'PASSWORD',
CASE u.u_role
WHEN 0 THEN 'ADMIN'
WHEN 1 THEN 'ATTENDANT'
WHEN 2 THEN 'TECHNICIAN'
WHEN 3 THEN 'TEACHER'
WHEN 4 THEN 'STUDENT'
END 'ROLE',
CASE u.u_status
WHEN 0 THEN 'INACTIVE'
WHEN 1 THEN 'ACTIVE'
END 'STATUS',
u.u_joiningdate 'ACC_CREATION_DATE',
u.u_activationdate 'ACC_ACTIVATION_DATE',
u.u_terminationdate 'ACC_TERMINATION_DATE'
FROM
users u
ORDER BY u.u_userid;
-- ----------------------------------------------