我有一个表,其中包含唯一ID的列表和描述这些ID特征的列。它的形式如下:
ID Tall Funny Weight
1 1 0 200
2 0 0 180
3 1 1 250诸若此类。我有另一张桌子,它只是一个有特征的人的身份证清单,比如收入超过10万美元。
Rich
1
3 我想要做的是在第一个表中创建一个列,如果它们位于第二个表中,则为=1,否则为0。我可以在R里这样做:
TableA$Rich <- TableA$ID %in% TableB但是它非常慢,如果没有其他原因,因为我的postgres (ParAccel/PaDB)集群拥有比我可以运行的资源更多的资源。你能帮我完成这个任务吗?
我试着做一个左外连接,就像.
create table c as(select a.id, tall, funny, weight,b.id as rich
from tablea a
left outer join tableb b
on a.id = b.id);但它产生了意想不到的结果。它给了我
ID Tall Funny Weight Rich
1 1 0 200 1
2 0 0 180 2
3 1 1 250 3即使应该是"1,NULL,3“,我也倾向于1和0。我担心这可能是数据的错误,但数据看起来是正确的。我在一个案例中尝试了同样的方法,在语句中得到了相同的结果,但是对于Rich的所有值,我都使用了"TRUE“。
发布于 2013-08-16 23:24:33
case语句解决了您的问题:
create table c as
select a.id, tall, funny, weight,
(case when b.id is null then 0 else 1 end) as rich
from tablea a left outer join
tableb b
on a.id = b.id;发布于 2013-08-16 23:23:48
select
a.id, tall, funny, weight,
(b.id is not null)::integer as rich
from
tablea a
left outer join
tableb b on a.id = b.idhttps://stackoverflow.com/questions/18283438
复制相似问题