2016-03-15 50 views
0

我试图使用sqldf编写嵌套查询。数据集是'contact_fb'。我试图只采取没有clubmahindra和列'from_name'中的不同名称的行,然后与'contact_fb'左连接以获取其他列中的所有信息。这是不给我想要的结果。如何使用sqldf创建嵌套查询

contact_fb =structure(list(X = 1:6, from_name = c("Club Mahindra", "Club Mahindra","pinto", "valencia", "valencia", "Club Mahindra"), type = structure(c(2L, 2L, 2L, 1L, 1L, 2L), .Label = c("link","photo", "status", "video"), class = "factor")), .Names = c("X","from_name", "type"), row.names = c(NA, 6L), class = "data.frame") 

我在此尝试

names_cm=sqldf("select t1.from_name, t2.* from (select distinct from_name from contact_fb where from_name!='Club Mahindra') as t1 left join (select * from contact_fb) as t2 on t1.from_name=t2.t1.from_name") 

我能得到它,最后通过

sqldf("select distinct(t1.from_name),t2.* from df t1 left join df t2 on (t1.from_name=t2.from_name) where t1.from_name!='Club Mahindra' group by t1.from_name") 

我不明白的地方我去错了。我仍然可以通过我的方式得到它吗?

输出

3 Pinto photo 
4 valencia link 
+0

仔细看看你的查询,你没有从表中选择任何东西。 – fhlgood

+0

请用语言解释您正在尝试做什么并显示预期输出。 –

+0

@谢谢MIKE芳我可以得到它的工作,但它仍然没有给出reqd结果。 –

回答

0

在问题的末尾显示的输出似乎是组不同from_nametype对针对from_name"Club Mahindra"为此,我们并不需要一个连接:

sqldf("select distinct from_name, type 
     from contact_fb 
     where from_name != 'Club Mahindra'") 

,并提供:

from_name type 
1  pinto photo 
2 valencia link 
+0

我不想这样做,因为我必须编写所有变量的名称,所以我试图加入它采取不同的值和其余的信息。 –

+0

'选择不同*从...'会做所有变量。 –

+0

那会给我。会有额外的valenica。 'X from_name type' '1 3 pinto photo' '2 4 valencia link' '3 5 valencia link' –