首页 > 解决方案 > 通过添加其他两个表的列在 Redshift 中创建一个表

问题描述

我想通过添加其他两个表的列在 Redshift 中创建一个表。

表格1
表1数据

表 2
表2数据

想在以下条件下创建新表

  1. 如果 table1.sid = table2.sid
    那么 t1.totalcorrect+t2.totalcorrect, t1.totalquestions+t2.totalquestions。即 s4 到 s7
  2. else 两个表中的数据原样

预期产出
输出表

使用连接结果表只给我 S4 到 S7 而不是其他所需的列。请帮我

标签: sqljoinsumamazon-redshiftfull-outer-join

解决方案


那是一个full join

select 
    coalesce(t1.sid, t2.sid) sid, 
    coalesce(t1.totalcorrect,   0) + coalesce(t2.totalcorrect,   0) totalcorrect,
    coalesce(t1.totalquestions, 0) + coalesce(t2.totalquestions, 0) totalquestions
from t1 
full join t2 on t2.sid = t1.sid

推荐阅读