Changes between Version 8 and Version 9 of AnalyticalStatisticalQuerying


Ignore:
Timestamp:
09/27/25 14:31:19 (2 weeks ago)
Author:
221507
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AnalyticalStatisticalQuerying

    v8 v9  
    265265
    266266
    267 2. Trigger to apply discount to student and child tickets
     267== 2. Trigger to apply discount to student and child tickets ==
    268268
    269269When 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.
     
    283283$$ LANGUAGE plpgsql;
    284284CREATE TRIGGER apply_student_ticket_discount
    285         AFTER INSERT ON student_ticket
     285        BEFORE INSERT ON student_ticket
    286286        FOR EACH ROW EXECUTE FUNCTION apply_ticket_discount();
    287287
    288288CREATE TRIGGER apply_child_ticket_discount
    289         AFTER INSERT ON child_ticket F
     289        BEFORE INSERT ON child_ticket F
    290290        OR EACH ROW EXECUTE FUNCTION apply_ticket_discount();
    291291}}}
    292292
    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:''
    295294
    296295
     
    306305}}}
    307306
    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
     313This 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
     318CREATE OR REPLACE FUNCTION update_payment_total()
     319RETURNS TRIGGER AS $$
     320BEGIN 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
     326RETURN NEW; END; $$
     327LANGUAGE plpgsql;
     328
     329CREATE TRIGGER trg_update_payment_total_insert
     330AFTER INSERT ON ticket
     331FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     332
     333CREATE TRIGGER trg_update_payment_total_update
     334AFTER UPDATE OF price, payment_id ON ticket
     335FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     336
     337CREATE TRIGGER trg_update_payment_total_delete
     338AFTER DELETE ON ticket
     339FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     340}}}
     341
     342
     343