impala sql查询,具有1个表,并查找具有3个主机名的公用项

f87krz0w  于 2021-06-26  发布在  Impala
关注(0)|答案(2)|浏览(290)

我只有一个表,正在尝试使用impala sql获取所有用户共有的destinationhostnames。
代理表:

sourcehostname destinationhostname
comp1          google.com
comp2          google.com
comp1          yahoo.com
comp1          facebook.com
comp2          facebook.com
comp3          facebook.com

当我运行以下命令以从2个sourcehostnames上的一个表中获取不同的destinationhostnames时,这是有效的:

SELECT DISTINCT t1.destinationhostname
FROM proxy_table t1 JOIN proxy_table t2
  ON t1.destinationhostname = t2.destinationhostname AND t1.sourcehostname  ="comp1" AND t2.sourcehostname="comp2";

它返回: google.com 以及 facebook.com 我试图返回值
comp1 comp2 以及 comp3 都有共同点 facebook.com 但我不能完全正确地回答这个问题:

SELECT DISTINCT t1.destinationhostname
FROM proxy_table t1 JOIN proxy_table t2 JOIN proxy_table t3
  ON t1.destinationhostname = t2.destinationhostname AND t1.sourcehostname  ="comp1" AND t2.sourcehostname="comp2" t3.sourcehostname = "comp3";

在查询中,我想指定不同的3台计算机,因为有数千台计算机,但我只想选择特定的计算机。

2ledvvac

2ledvvac1#

你能试试下面吗。

SELECT DISTINCT t1.destinationhostname
FROM proxy_table t1 JOIN proxy_table t2
ON t1.destinationhostname = t2.destinationhostname 
JOIN proxy_table t3
ON t1.destinationhostname = t3.destinationhostname 
and t2.destinationhostname = t3.destinationhostname 
WHERE
t1.sourcehostname  ="comp1" 
AND t2.sourcehostname="comp2"
AND t3.sourcehostname = "comp3";

如果你遇到问题,请告诉我

67up9zun

67up9zun2#

使用聚合。假设没有重复行:

select destinationhostname
from proxy_table 
group by destinationhostname
having count(*) = (select count(distinct sourcehostname) from proxy_table);

如果可以有重复的行,只需更改 having :

having count(distinct sourcehostname) = (select count(distinct sourcehostname) from proxy_table);

如果你想要三个用户,那就使用 = 3 .

相关问题