I am trying to calculate some values and adding them to another table.
So I have booking, treatment and invoice. the user books some treatments and then the booking session generates the invoice. I already iserted the invoice insert statements manually, just trying to populate the calculated values in accordance to the number of booking sessions. ps. the treatment price are in the treatment table.
SQL> SELECT * FROM INVOICE;
INV_ID|INV_DATETIME |INV_SUBTOTAL|INV_DISCOUNT| INV_TOTAL
----------|------------------------------|------------|------------|-----------
100|14-NOV-12 09.40.06.918000 | $.00| $.00| $.00
101|18-MAR-12 10.03.00.000000 | $.00| $.00| $.00
102|18-MAR-12 10.15.00.000000 | $.00| $.00| $.00
103|18-MAR-12 10.55.00.000000 | $.00| $.00| $.00
104|18-MAR-12 10.38.00.000000 | $.00| $.00| $.00
105|12-JUN-12 15.15.00.000000 | $.00| $.00| $.00
106|06-AUG-12 12.13.00.000000 | $.00| $.00| $.00
107|04-MAY-12 09.15.00.000000 | $.00| $.00| $.00
108|29-NOV-12 13.16.00.000000 | $.00| $.00| $.00
109|18-MAR-12 10.37.00.000000 | $.00| $.00| $.00
110|21-MAR-12 11.26.00.000000 | $.00| $.00| $.00
111|24-APR-12 11.16.00.000000 | $.00| $.00| $.00
112|12-MAY-12 10.27.00.000000 | $.00| $.00| $.00
113|21-MAY-12 13.33.00.000000 | $.00| $.00| $.00
114|18-JAN-12 12.17.00.000000 | $.00| $.00| $.00
115|20-JUN-12 11.18.00.000000 | $.00| $.00| $.00
116|09-JUN-12 10.14.00.000000 | $.00| $.00| $.00
So i am using the following sql query to calculate the subtotal price for an invoice as many booking sessions can be booked at once, these will appear in the same invoice *..1
SQL> COLUMN total FORMAT $99,999.99
SQL> SELECT IV.INV_ID, SUM(TR.TR_PRICE) as Total
2 FROM BOOKING_SESSION BS, TREATMENT TR, INVOICE IV
3 WHERE BS.INV_ID = IV.INV_ID
4 AND BS.BK_TREATMENT = TR.TR_ID
5 GROUP BY IV.INV_ID
6 ORDER BY BS.INV_ID ASC;
INV_ID| TOTAL
----------|-----------
100| $605.00
101| $45.00
102| $70.00
103| $65.00
104| $50.00
105| $25.00
106| $25.00
107| $25.00
108| $25.00
109| $50.00
110| $25.00
111| $50.00
112| $15.00
113| $15.00
114| $25.00
115| $25.00
116| $25.00
Now i want to grab each value and add it to the INV_SUBTOTAL
I tried doing the following but doesnt seem to work.
CREATE OR REPLACE TRIGGER INV_SUBTOTAL
BEFORE INSERT OR UPDATE ON BOOKING_SESSION
FOR EACH ROW
BEGIN
SELECT
(SELECT SUM(TR.TR_PRICE) FROM BOOKING_SESSION BS, TREATMENT TR, INVOICE IV WHERE BS.INV_ID = IV.INV_ID AND BS.BK_TREATMENT = TR.TR_ID)
INTO :NEW.INV_SUBTOTAL
FROM DUAL;
END;
/
Warning: Trigger created with compilation errors.
SQL> SHOW ERROR
Errors for TRIGGER INV_SUBTOTAL:
LINE/COL|ERROR
--------|-----------------------------------------------------------------
4/12 |PLS-00049: bad bind variable 'NEW.INV_SUBTOTAL'
SQL>
Perhaps i am doing it wrong T_T
Here is the data for relating tables;
SQL> select * from treatment;
TR_ID|TR_NAME |TR_SPECIALIST| TR_PRICE
----------|--------------------|-------------|-----------
1|Hair removal | 1| $25.00
2|Hair styling | 1| $50.00
3|Nails | 1| $15.00
4|Botox | 1| $30.00
5|Make up | 5| $35.00
6|Reflexology | 3| $25.00
7|Massage therapy | 3| $25.00
8|Facial care | 4| $25.00
9|Weight loss | 2| $40.00
10|Consultation | 2| $20.00
10 rows selected.
SQL> select * from booking_session
2 where inv_id > 100;
BK_ID|BK_DATE | BK_BOOKER|BK_CUSTOMER|BK_TREATMENT|START_SESSION |END_SESSION |STAFFAPPOINTED| BK_ROOM| INV_ID
----------|------------------------------|----------|-----------|------------|------------------------------|------------------------------|--------------|----------|----------
4|18-MAR-12 10.35.00.000000 | 1| 4| 10|20-MAR-12 12.00.00.000000 |20-MAR-12 13.00.00.000000 | 1| 4| 109
5|18-MAR-12 10.36.00.000000 | 1| 4| 4|21-MAR-12 11.00.00.000000 |20-MAR-12 12.00.00.000000 | 1| 5| 109
6|18-MAR-12 10.50.00.000000 | 5| 5| 2|20-MAR-12 11.00.00.000000 |20-MAR-12 12.00.00.000000 | 5| 6| 103
11|18-MAR-12 10.09.00.000000 | 10| 10| 10|20-MAR-12 11.00.00.000000 |20-MAR-12 12.00.00.000000 | 10| 10| 102
12|18-MAR-12 10.12.00.000000 | 10| 10| 6|22-MAR-12 11.00.00.000000 |22-MAR-12 12.00.00.000000 | 11| 11| 102
16|18-MAR-12 10.00.00.000000 | 15| 14| 10|20-MAR-12 11.00.00.000000 |20-MAR-12 12.00.00.000000 | 15| 14| 101
17|18-MAR-12 10.02.00.000000 | 15| 14| 7|20-MAR-12 12.00.00.000000 |20-MAR-12 13.00.00.000000 | 16| 15| 101
31|21-MAR-12 11.25.00.000000 | 1| 4| 1|24-MAR-12 11.00.00.000000 |24-MAR-12 12.00.00.000000 | 2| 1| 110
32|24-APR-12 11.15.00.000000 | 1| 4| 2|26-APR-12 12.00.00.000000 |26-APR-12 13.00.00.000000 | 3| 2| 111
33|12-MAY-12 10.25.00.000000 | 1| 4| 3|21-MAY-12 13.00.00.000000 |21-MAY-12 14.00.00.000000 | 4| 3| 112
34|21-MAY-12 13.32.00.000000 | 1| 4| 3|26-MAY-12 15.00.00.000000 |26-MAY-12 16.00.00.000000 | 4| 3| 113
35|18-JAN-12 12.14.00.000000 | 1| 4| 6|21-JAN-12 11.00.00.000000 |21-JAN-12 12.00.00.000000 | 17| 5| 114
36|20-JUN-12 11.16.00.000000 | 1| 4| 7|25-JUN-12 11.00.00.000000 |25-JUN-12 12.00.00.000000 | 22| 5| 115
37|09-JUN-12 10.12.00.000000 | 1| 4| 8|11-JUL-12 11.00.00.000000 |11-JUL-12 12.00.00.000000 | 24| 5| 116
20|18-MAR-12 10.30.00.000000 | 15| 17| 7|20-MAR-12 09.00.00.000000 |20-MAR-12 10.00.00.000000 | 16| 17| 104
39|18-MAR-12 10.35.00.000000 | 1| 17| 8|25-MAR-12 13.00.00.000000 |25-MAR-12 14.00.00.000000 | 24| 5| 104
40|12-JUN-12 15.11.00.000000 | 1| 17| 8|11-JUL-12 11.00.00.000000 |11-JUL-12 12.00.00.000000 | 24| 5| 105
41|06-AUG-12 12.13.00.000000 | 1| 17| 8|11-AUG-12 11.00.00.000000 |11-AUG-12 12.00.00.000000 | 24| 5| 106
42|04-MAY-12 09.15.00.000000 | 1| 17| 8|28-JUN-12 11.00.00.000000 |28-JUN-12 12.00.00.000000 | 24| 5| 107
43|29-NOV-12 13.16.00.000000 | 1| 17| 8|01-DEC-12 11.00.00.000000 |01-DEC-12 12.00.00.000000 | 24| 5| 108
44|18-MAR-12 10.53.00.000000 | 5| 5| 3|20-MAR-12 13.00.00.000000 |20-MAR-12 14.00.00.000000 | 6| 7| 103
38|18-MAR-12 10.13.00.000000 | 1| 10| 8|25-MAR-12 12.00.00.000000 |25-MAR-12 13.00.00.000000 | 24| 5| 102
Here is the bit of the ERD related to this question.

If the goal is to update the
INV_SUBTOTALcolumn inINVOICEfor a particularINV_IDwith theTOTALthat is computed in the second query, you would need anUPDATEstatement. My guess (since I don’t know what theTREATMENTorBOOKING_SESSIONtables look like or what data they contain) is that you want a correlated update that looks something likeA trigger on
BOOKING_SESSIONwon’t do anything unless you actually update (or insert into) theBOOKING_SESSIONtable. A row-level trigger onBOOKING_SESSIONalso cannot, in general, query theBOOKING_SESSIONtable without generating a mutating table exception that we discussed in one of your questions a couple of days ago. And the:newrecord in a trigger is based on the table the trigger is defined on– you can only reference columns that are part of theBOOKING_SESSIONtable. You can’t reference:new.inv_subtotalunlessINV_SUBTOTALis a column in the table you’ve defined the trigger on.In the real world, there would also be a serious denormalization problem here. Storing aggregated data separately from the row-level data violates basic normalization and will almost certainly lead to data quality issues where the aggregate data doesn’t match the lower-level data that is stored in the
TREATMENTtable. Since this is just a homework assignment, perhaps your professor wants you to ignore that issue but it’s something you’d want to be very sensitive to in the real world.