加入组ID,以获取该表中缺少的个别ID

时间:2018-02-12 17:01:04

标签: sql amazon-redshift

对于以下数据集(超简化):

t1

ext_id, tid, aid, aum, actions
z1,     1,   a,   100, 100
z2,     1,   b,   100, 100
x1,     2,   d,   200, 200
x2,     2,   e,   200, 200

t2

tid, aid, aum, actions
1,   a,   100, 100
1,   b,   100, 100
1,   c,   100, 100
2,   d,   200, 200
2,   e,   200, 200
2,   f,   200, 200

我希望t1t2匹配,并根据aid获取其余tid个加入的所有数据:

即。输出应该是:

ext_id, tid, aid, aum, actions
(null), 1,   c,   100, 100
(null), 2,   f,   200, 200

我试过了:

select (null) as ext_id, b.*
from #t1 a
right join #t2 b
on a.tid=b.tid
where a.aid is null

但它看起来不正确。

谢谢

5 个答案:

答案 0 :(得分:2)

使用外连接和IS NULL运算符:

select * from t2
left join t1
using (`tid`, `aid`, `aum`, `actions`)
where ext_id is null

演示:http://sqlfiddle.com/#!9/9935c7/4

答案 1 :(得分:1)

我认为在你的情况下你可以使用not exists来获得你想要的东西。

我建议如下:

SELECT  STR_TO_DATE(A.`Timestamp`,'%Y-%m-%d') `Timestamp`, A.DataValue 
Crop_name, COUNT(*) Quantity
FROM eventlog A INNER JOIN crops B 
ON A.DataValue=B.DataValue
WHERE A.Datakey='Crop'
GROUP BY STR_TO_DATE(A.`Timestamp`,'%Y-%m-%d'), A.DataValue
ORDER BY STR_TO_DATE(A.`Timestamp`,'%Y-%m-%d'), COUNT(*) DESC;

答案 2 :(得分:0)

试试这个:

 select '(Null)' as ext_id, t2.tid, t2.aid, t2.aum, t2.actions
 from t2 left join t1 
 on t2.tid=t1.tid 
 and t2.aid=t1.aid
 where t1.tid is null and t1.aid is null

答案 3 :(得分:0)

这是对我有用的正确(可扩展)答案:

select distinct b.*
from t1 a
join t2 b on a.tid=b.tid
left join t1 c on b.aid=c.aid
where c.aid is null

答案 4 :(得分:0)

运行以下查询以获得所需结果

select t1.ext_id,t2.tid,t2.aid,t2.aum,t2.actions from t1 
right join t2 
on  t1.aid= t2.aid 
where t1.ext_id is null