Changes between Version 8 and Version 9 of AnalyticalStatisticalQuerying
- Timestamp:
- 09/27/25 14:31:19 (2 weeks ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
AnalyticalStatisticalQuerying
v8 v9 265 265 266 266 267 2. Trigger to apply discount to student and child tickets 267 == 2. Trigger to apply discount to student and child tickets == 268 268 269 269 When purchasing a ticket, if its a student or a child ticket there exist special discounts. Since the application supports only buying regular tickets (covered by the application use cases), we will present how buying tickets of this type will automatically apply the discounts. … … 283 283 $$ LANGUAGE plpgsql; 284 284 CREATE TRIGGER apply_student_ticket_discount 285 AFTERINSERT ON student_ticket285 BEFORE INSERT ON student_ticket 286 286 FOR EACH ROW EXECUTE FUNCTION apply_ticket_discount(); 287 287 288 288 CREATE TRIGGER apply_child_ticket_discount 289 AFTERINSERT ON child_ticket F289 BEFORE INSERT ON child_ticket F 290 290 OR EACH ROW EXECUTE FUNCTION apply_ticket_discount(); 291 291 }}} 292 292 293 ''Example'' 294 If we select the existing child tickets and their appropriate base prices (i.e original prices) using a simple query: 293 ''Example: If we select the existing child tickets and their appropriate base prices (i.e original prices) using a simple query:'' 295 294 296 295 … … 306 305 }}} 307 306 308 We will get the following results: 309 310 311 312 307 ''We will get the following results:'' 308 [[Image(child_ticket_prices.png)]] 309 310 311 == 3. Update total payment == 312 313 This trigger calculates the total payment for an account and should work together with the previous trigger for situations where we buy multiple tickets, including special types of tickets (child and student tickets). It is executed **AFTER** applying the discounts. 314 315 {{{ 316 -- UPDATE TOTAL PAYMENT TOTAL PRICE (TRIGGER AFTER CHILD/STUDENT DISCOUNT IS APPLIED) 317 318 CREATE OR REPLACE FUNCTION update_payment_total() 319 RETURNS TRIGGER AS $$ 320 BEGIN UPDATE payment 321 SET total_price = ( SELECT COALESCE(SUM(price), 0) 322 FROM ticket 323 WHERE payment_id = NEW.payment_id) 324 WHERE payment_id = NEW.payment_id; 325 326 RETURN NEW; END; $$ 327 LANGUAGE plpgsql; 328 329 CREATE TRIGGER trg_update_payment_total_insert 330 AFTER INSERT ON ticket 331 FOR EACH ROW EXECUTE FUNCTION update_payment_total(); 332 333 CREATE TRIGGER trg_update_payment_total_update 334 AFTER UPDATE OF price, payment_id ON ticket 335 FOR EACH ROW EXECUTE FUNCTION update_payment_total(); 336 337 CREATE TRIGGER trg_update_payment_total_delete 338 AFTER DELETE ON ticket 339 FOR EACH ROW EXECUTE FUNCTION update_payment_total(); 340 }}} 341 342 343