how to get a sum of two sums from two tables?
SELECT (SELECT SUM(col) FROM table1) + SELECT (SUM(col) from table2)
doesn't work
You are very close. You just need parens around each subquery:
SELECT (SELECT SUM(col) FROM table1) + (SELECT SUM(col) from table2)
If either subquery could return NULL
, you might prefer:
SELECT COALESCE(t1.s, 0) + COALESCE(t2.s)
FROM (SELECT SUM(col) as s FROM table1) t1 CROSS JOIN
(SELECT SUM(col) as s from table2) t2;
Due to this link , you can do that by :
SELECT T1C1 , T2C1
FROM
( select SUM(Col1) T1C1 FROM T1 ) A
CROSS JOIN
( select SUM(Col1) T2C1 FROM T2 ) B
also you can visit these links:
Query SUM for two fields in two different tables
select coalesce(sum(x),0) from
(
Select sum(a) x from tab1
Union all
Select sum(b) from tab2
) Ilv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.