Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:08:58+00:00 2026-06-14T14:08:58+00:00

I am trying to calculate some values and adding them to another table. So

  • 0

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.

erd2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:09:00+00:00Added an answer on June 14, 2026 at 2:09 pm

    If the goal is to update the INV_SUBTOTAL column in INVOICE for a particular INV_ID with the TOTAL that is computed in the second query, you would need an UPDATE statement. My guess (since I don’t know what the TREATMENT or BOOKING_SESSION tables look like or what data they contain) is that you want a correlated update that looks something like

    UPDATE invoice inv
       SET inv_subtotal = (SELECT SUM(tr.tr_price)
                             FROM treatment tr,
                                  booking_session bs
                            WHERE bs.inv_id = inv.inv_id
                              AND bs.bk_treatment = tr.tr_id)
    

    A trigger on BOOKING_SESSION won’t do anything unless you actually update (or insert into) the BOOKING_SESSION table. A row-level trigger on BOOKING_SESSION also cannot, in general, query the BOOKING_SESSION table without generating a mutating table exception that we discussed in one of your questions a couple of days ago. And the :new record in a trigger is based on the table the trigger is defined on– you can only reference columns that are part of the BOOKING_SESSION table. You can’t reference :new.inv_subtotal unless INV_SUBTOTAL is 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 TREATMENT table. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to conditionally calculate some values based on user inputs in a
I'm trying to iterate through some values, and calculate a rank. I have a
I'm trying to figure out how to best pre-calculate some sin and cosine values,
I'm trying to call a php function from jquery function to calculate some values
I'm trying to generate arrays and calculate there values depending on some function. and
I'm trying to use the RQuantLib package to calculate greeks for some options but
I am trying to write some code to calculate dividend yields. I need to
Am trying to calculate the number of rows in a table depending on a
I have a procedure that calculates the price of travel based on some values
I'm trying to generate some sql that is used to calculate some final scores

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.