korgidis
korgidis

Reputation: 5

how to sum values in sql and store in a different table (oracle database)

I have a table ORDER_DETAIL :

             ID |  ORDER_ID | TOTALCOST 
              ------------------------  
              1 |    1000   | 50
              2 |    1000   | 50
              3 |    2000   | 200
              4 |    2000   | 200

the second table is the MAIN_ORDER and I want to calculate the sum in the ORDER_DETAIL table of the TOTALCOST based on the ORDER_ID and to store it to the MAIN_ORDER table so that will be like that

             ORDER_ID | ORDER_PRICE 
              ------------------------  
               1000   | 100
               2000   | 400

how I can syntax the command? is it possible with a trigger? thank you in advance!

Upvotes: 0

Views: 67

Answers (1)

umbersar
umbersar

Reputation: 1931

how about this:

INSERT INTO MAIN_ORDER(ORDER_ID, ORDER_PRICE ) 
    SELECT ORDER_ID, SUM(TOTALCOST) 
    FROM ORDER_DETAIL 
    GROUP BY ORDER_ID;

Haven't tested it but should work.

Upvotes: 1

Related Questions