0

Example

There are three tables: A_TBL,B_TBL,C_TBL

CREATE TABLE A_tbl (    
    NUM int
)
insert into a_tbl values('1000');

CREATE TABLE B_tbl (    
    NUM int 
) 
insert into B_tbl values('2000')

CREATE TABLE C_tbl (    
    NUM int
) 

A_TBL

NUM
1000

B_TBL

NUM
2000

I want to put the sum of Tables A and B in Table C.

Desired Output

C_TBL

NUM
3000
Kyo
  • 21
  • 4
  • What have you tried so, and why didn't it work? Have you considered using a (`CROSS`) `JOIN` and the `+` operator? – Thom A Jul 18 '19 at 14:00
  • Possible duplicate of [SQL: How to to SUM two values from different tables](https://stackoverflow.com/questions/19436895/sql-how-to-to-sum-two-values-from-different-tables) – Matti Jul 18 '19 at 14:04

2 Answers2

0

You need UNION ALL :

INSERT INTO c_tbl(num)
  SELECT SUM(num) 
  FROM (SELECT a.NUM FROM a_tbl a UNION ALL
        SELECT b.NUM FROM b_tbl b
       ) ab; 
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
0

You can use subqueries in a select:

insert into c (num)
    select ( (select sum(num) from a) + select sum(num) from b) )
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786