如何对两个日期之间两个不同 table 中的两个不同列求和

how to SUM two different columns in two different table between two date

下面是我的查询:

select sum(tableA.value)+sum(tableB.value1) ) from tableA,tableB where tableA.data between '2016-01-21' and '2016-03-09' and tableB.date2 between '2016-01-21' and '2016-03-09'

但是当行数不同时,结果是错误的。 示例 tableA.value = 2 and tableB values =3 and 5 the result =12

这是错误的。结果应该是 10

请尝试以下查询

SELECT 
    SUM(A.value) OVER (PARTITION BY A.date order by A.date) + 
    SUM(B.value) OVER (PARTITION BY B.date order by B.date) 
FROM
(select value, date from tableA where date between '2016-01-21' and '2016-03-09')A --2, d1
CROSS JOIN 
(select value, date from tableB where date between '2016-01-21' and '2016-03-09')B --3,d2 and 5,d3
--Result of CROSS JOIN is below
--2,d1,3,d2
--2,d1,5,d3
-- which gives result as 
--2+8=10
;with sumA as (
    select sum(tableA.value) value from tableA 
     where tableA.data between '2016-01-21' and '2016-03-09'
), sumB as (
    select sum(tableB.value) value from tableB 
     where tableB.date2 between '2016-01-21' and '2016-03-09'
)
select a.Value + b.Value from sumA, sumB;