-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoric_db.sql
246 lines (227 loc) · 7.71 KB
/
historic_db.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
-------------- INFO ----------------------
/*
* To test this program you need to:
* 1. run create_sgms.sql
* 2. run populate.sql
* 3. run this file
* */
-- CREATE LAST_BACKUP
INSERT INTO last_backup (date) VALUES (current_date);
-- DROP EXISTING VIEWS
DROP VIEW IF EXISTS il_pricing_info CASCADE;
DROP VIEW IF EXISTS students_with_siblings CASCADE;
DROP VIEW IF EXISTS gl_and_nof_students CASCADE;
DROP VIEW IF EXISTS gl_pricing_info CASCADE;
DROP VIEW IF EXISTS e_and_nof_students CASCADE;
DROP VIEW IF EXISTS e_pricing_info CASCADE;
-- CREATE VIEWS
CREATE VIEW students_with_siblings AS
SELECT DISTINCT UNNEST(ARRAY[first_student_id, second_student_id]) AS student_id, 'true' AS has_siblings
FROM sibling_relationship sr;
CREATE VIEW il_pricing_info AS
SELECT individual_lesson_id AS
lesson_id,
il.student_id,
date,
'individual' AS "type",
LEVEL,
has_siblings,
il.pricing_schema_id,
base_price,
individual_lesson_quanitifier,
beginner_quantifier,
intermediate_quantifier,
advanced_quantifier,
discount_percentage
FROM individual_lesson il
LEFT JOIN LEVEL
ON il.level_id = LEVEL.level_id
LEFT JOIN students_with_siblings sws
ON il.student_id = sws.student_id
LEFT JOIN pricing_schema ps
ON il.pricing_schema_id = ps.pricing_schema_id;
CREATE VIEW gl_and_nof_students AS
SELECT group_lesson_id, COUNT(student_id) AS nof_students FROM student_group_lesson GROUP BY group_lesson_id;
CREATE VIEW gl_pricing_info AS
SELECT sgl.group_lesson_id AS lesson_id,
sgl.student_id,
date,
'group' AS "type",
gl.min_no_of_students,
nof_students,
LEVEL,
has_siblings,
gl.pricing_schema_id,
base_price,
ps.group_lesson_quantifier,
beginner_quantifier,
intermediate_quantifier,
advanced_quantifier, ps.
discount_percentage
FROM student_group_lesson sgl
LEFT JOIN group_lesson gl
ON sgl.group_lesson_id = gl.group_lesson_id
LEFT JOIN gl_and_nof_students glans
ON glans.group_lesson_id = sgl.group_lesson_id
LEFT JOIN LEVEL l
ON gl.level_id = l.level_id
LEFT JOIN students_with_siblings sws
ON sgl.student_id = sws.student_id
LEFT JOIN pricing_schema ps
ON gl.pricing_schema_id = ps.pricing_schema_id;
CREATE VIEW e_and_nof_students AS
SELECT ensemble_id, COUNT(student_id) AS nof_students FROM student_ensemble GROUP BY ensemble_id;
CREATE VIEW e_pricing_info AS
SELECT se.ensemble_id AS lesson_id,
se.student_id,
date,
'group' AS "type",
e.min_no_of_students,
nof_students,
has_siblings,
e.pricing_schema_id,
base_price,
ps.ensemble_quantifier,
beginner_quantifier,
intermediate_quantifier,
advanced_quantifier, ps.
discount_percentage
FROM student_ensemble se
LEFT JOIN ensemble e
ON se.ensemble_id = e.ensemble_id
LEFT JOIN e_and_nof_students eans
ON eans.ensemble_id = e.ensemble_id
LEFT JOIN students_with_siblings sws
ON se.student_id = sws.student_id
LEFT JOIN pricing_schema ps
ON e.pricing_schema_id = ps.pricing_schema_id;
-- INDIVIDUAL LESSON
-- lesson_historic table
INSERT INTO lesson_historic (lesson_id, time, date, "type", genre, "level", instrument, last_backup_id)
SELECT
individual_lesson_id AS lesson_id,
time,
date,
'individual' AS "type",
NULL AS genre,
(SELECT "level" FROM "level" AS l WHERE l.level_id = il.level_id) ,
(SELECT instrument FROM instrument i WHERE i.instrument_id = il.instrument_id),
(SELECT last_backup_id FROM last_backup WHERE date = (SELECT max(date) FROM last_backup))
FROM individual_lesson il
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup))
AND date <= current_date
AND student_id IS NOT NULL;
-- student_lesson
INSERT INTO student_lesson(student_id, lesson_id, price)
SELECT
student_id,
lesson_id,
(
CASE WHEN has_siblings IS NOT NULL
THEN (CASE WHEN LEVEL = 'beginner'
THEN (SELECT (base_price * beginner_quantifier * individual_lesson_quanitifier * (100-discount_percentage)/100) AS price)
WHEN LEVEL = 'intermediate'
THEN (SELECT (base_price * intermediate_quantifier * individual_lesson_quanitifier * (100-discount_percentage)/100) AS price)
ELSE (SELECT (base_price * advanced_quantifier * individual_lesson_quanitifier * (100-discount_percentage)/100) AS price)
END)
ELSE (CASE WHEN LEVEL = 'beginner'
THEN (SELECT (base_price * beginner_quantifier * individual_lesson_quanitifier) AS price)
WHEN LEVEL = 'intermediate'
THEN (SELECT (base_price * intermediate_quantifier * individual_lesson_quanitifier) AS price)
ELSE (SELECT (base_price * advanced_quantifier * individual_lesson_quanitifier) AS price)
END)
END
)
FROM il_pricing_info ilpi
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup))
AND date <= current_date
AND student_id IS NOT NULL;
-- GROUP LESSON
-- lesson_historic table
-- all group lessons between now and last update
INSERT INTO lesson_historic (lesson_id, time, date, "type", genre, "level", instrument, last_backup_id)
SELECT
group_lesson_id AS lesson_id,
time, date,
'group' AS "type",
NULL AS genre,
(SELECT "level" FROM "level" AS l WHERE l.level_id = gl.level_id) ,
(SELECT instrument FROM instrument i WHERE i.instrument_id = gl.instrument_id),
(SELECT last_backup_id FROM last_backup WHERE date = (SELECT max(date) FROM last_backup))
FROM group_lesson gl
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup))
AND date <= current_date
AND (SELECT count(*)
FROM student_group_lesson sgl
WHERE gl.group_lesson_id = sgl.group_lesson_id) >= gl.min_no_of_students;
-- student_lesson
INSERT INTO student_lesson(student_id, lesson_id, price)
SELECT
student_id,
glpi.lesson_id AS lesson_id ,
(CASE WHEN has_siblings IS NOT NULL
THEN (CASE WHEN LEVEL = 'beginner'
THEN (SELECT (base_price * beginner_quantifier * group_lesson_quantifier * (100-discount_percentage)/100) AS price)
WHEN LEVEL = 'intermediate'
THEN (SELECT (base_price * intermediate_quantifier * group_lesson_quantifier * (100-discount_percentage)/100) AS price)
ELSE (SELECT (base_price * advanced_quantifier * group_lesson_quantifier * (100-discount_percentage)/100) AS price)
END)
ELSE (CASE WHEN LEVEL = 'beginner'
THEN (SELECT (base_price * beginner_quantifier * group_lesson_quantifier) AS price)
WHEN LEVEL = 'intermediate'
THEN (SELECT (base_price * intermediate_quantifier * group_lesson_quantifier) AS price)
ELSE (SELECT (base_price * advanced_quantifier * group_lesson_quantifier) AS price)
END)
END)
FROM gl_pricing_info glpi
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup)) -- lower date bound
AND date <= current_date -- upper date bound
AND nof_students >= min_no_of_students;
-- ENSEMBLE
-- lesson_historic table
INSERT INTO lesson_historic (lesson_id, time, date, "type", genre, "level", instrument, last_backup_id)
SELECT
ensemble_id AS lesson_id,
time,
date,
'ensemble' AS "type",
genre, NULL AS "level" ,
NULL AS instrument,
(SELECT last_backup_id FROM last_backup WHERE date = (SELECT max(date) FROM last_backup))
FROM ensemble e
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup))
AND date <= current_date
AND (SELECT count(*)
FROM student_ensemble se
WHERE e.ensemble_id = se.ensemble_id) >= e.min_no_of_students;
-- student_lesson
INSERT INTO student_lesson(student_id, lesson_id, price)
SELECT
student_id,
epi.lesson_id AS lesson_id,
(CASE WHEN has_siblings IS NOT NULL
THEN (SELECT (base_price * ensemble_quantifier * (100-discount_percentage)/100) AS price)
ELSE (SELECT (base_price * ensemble_quantifier) AS price)
END)
FROM e_pricing_info epi
WHERE date > (SELECT MAX (date)
FROM last_backup
WHERE date NOT IN (SELECT Max (date)
FROM last_backup)) -- lower date bound
AND date <= current_date -- upper date bound
AND nof_students >= min_no_of_students;