2016-11-23 46 views

回答

2

我建議union all,然後彙總:

select date, user, sum(vale) 
from ((select t1.user, t1.date, t1.vale from t1) union all 
     (select t2.user, t2.date, t2.vale from t2) 
    ) t 
group by date, user; 

如果只想要一個用戶的結果(對外部查詢或對每個子查詢),您可以添加where user = 'x'

0

提取年份和月份,並在衍生表格中執行union all。然後GROUP BY其結果:

select y, m, user, sum(vale) 
from 
(
    select user, year(date) as y, month(date) as m, vale from t1 
    union all 
    select user, year(date) as y, month(date) as m, vale from t2 
) dt 
group by y, m, user