有两种方法可以做到这一点,我不确定在 Redshift 中哪种方法更快。一种是union all和group by:
select sid, sum(totalcorrect) as totalcorrect, sum(totalquestions) as totalquestions
from ((select sid, totalcorrect, totalquestions
from t1
) union all
(select sid, totalcorrect, totalquestions
from t2
)
) t
group by sid;
第二个使用full join,为此我推荐使用using子句:
select sid,
coalesce(t1.totalcorrect, 0) + coalesce(t2.totalcorrect, 0) as totalcorrect,
coalesce(t1.totalquestions, 0) + coalesce(t2.totalquestions, 0) as totalquestions
from t1 full join
t2
using (sid);
这两种方法之间存在差异。第一个保证结果集中每个sid 有一行,即使其中一个表中有重复项。第一个还将NULL 的sid 值合并到一行中。