Sql 有一个airbnb数据集,需要使用连接函数确定哪些主机在同一位置上有多个单元

Sql 有一个airbnb数据集,需要使用连接函数确定哪些主机在同一位置上有多个单元,sql,apache-spark-sql,inner-join,Sql,Apache Spark Sql,Inner Join,根据airbnb数据集,我需要找出哪些主机在同一位置上有多个房间 我有两张桌子:airbnb和airbnb_位置 airbnb表的前六行如下所示: host_id room_id latitude longitude 2536 2318 47.61 -122.29 35749 4291 47.68 -122.31 8993 5682 47.52 -122.35 14942

根据airbnb数据集,我需要找出哪些主机在同一位置上有多个房间

我有两张桌子:airbnb和airbnb_位置

airbnb表的前六行如下所示:

host_id    room_id    latitude    longitude
2536       2318       47.61       -122.29
35749      4291       47.68       -122.31
8993       5682       47.52       -122.35
14942      6606       47.65       -122.33
30559      9419       47.55       -122.31
30832      9460       47.60       -122.32
room_id    latitude    longitude
2318       47.61       -122.29
4291       47.68       -122.31
5682       47.52       -122.35
6606       47.65       -122.33
9419       47.55       -122.31
9460       47.60       -122.32
SELECT 
    host_id
    ,latitude
    ,longitude
FROM airbnb
GROUP BY
    host_id
    ,latitude
    ,longitude
HAVING COUNT(DISTINCT room_id) > 1
airbnb_locations表的前六行如下所示:

host_id    room_id    latitude    longitude
2536       2318       47.61       -122.29
35749      4291       47.68       -122.31
8993       5682       47.52       -122.35
14942      6606       47.65       -122.33
30559      9419       47.55       -122.31
30832      9460       47.60       -122.32
room_id    latitude    longitude
2318       47.61       -122.29
4291       47.68       -122.31
5682       47.52       -122.35
6606       47.65       -122.33
9419       47.55       -122.31
9460       47.60       -122.32
SELECT 
    host_id
    ,latitude
    ,longitude
FROM airbnb
GROUP BY
    host_id
    ,latitude
    ,longitude
HAVING COUNT(DISTINCT room_id) > 1
因此,两个表之间的唯一区别是host_id属性

这是我第一次尝试:

select distinct t1.host_id, t1.room_id, t1.latitude, t1.longitude
from airbnb t1 inner join airbnb_locations t2
where t1.latitude = t2.latitude
and t1.longitude = t2.longitude
and t1.room_id <> t2.room_id
选择不同的t1.host\u id、t1.room\u id、t1.lation、t1.longitude
从airbnb t1内部连接airbnb_位置t2
其中t1.纬度=t2.纬度
t1.经度=t2.经度
t1.房间id t2.房间id
这段代码的问题在于,它还产生了属于同一位置上不同主机的房间。我曾尝试按主机对房间进行分组,但由于某些原因,这给了我错误的结果

解决方案可以是将host_id属性添加到airbnb_位置,然后:

select distinct t1.host_id, t1.room_id, t1.latitude, t1.longitude
from airbnb t1 inner join airbnb_locations t2
where t1.host_id = t2.host_id
and t1.latitude = t2.latitude
and t1.longitude = t2.longitude
and t1.room_id <> t2.room_id
选择不同的t1.host\u id、t1.room\u id、t1.lation、t1.longitude
从airbnb t1内部连接airbnb_位置t2
其中t1.host\u id=t2.host\u id
和t1.纬度=t2.纬度
t1.经度=t2.经度
t1.房间id t2.房间id

但我剩下两张完全相同的桌子,我不确定这是本练习的重点。因此,我很好奇,我是否错过了一个明显的解决方案,无法根据使用连接函数的两个初始表来解决问题?

我想您需要聚合:

select a.host_id, a.latitude, a.longitude
from airbnb a
group by a.host_id, a.latitude, a.longitude
having count(*) > 1;
这实际上返回了位置。如果您真的只需要主机,那么这是一个非常罕见的时间,
select distinct
适用于
groupby

select distinct a.host_id
from airbnb a
group by a.host_id, a.latitude, a.longitude
having count(*) > 1;

我认为你不需要在这里加入
JOIN
。您可能只需按主机id、纬度和经度对主机进行分组,然后只返回具有多个房间的主机。您的查询可能如下所示:

host_id    room_id    latitude    longitude
2536       2318       47.61       -122.29
35749      4291       47.68       -122.31
8993       5682       47.52       -122.35
14942      6606       47.65       -122.33
30559      9419       47.55       -122.31
30832      9460       47.60       -122.32
room_id    latitude    longitude
2318       47.61       -122.29
4291       47.68       -122.31
5682       47.52       -122.35
6606       47.65       -122.33
9419       47.55       -122.31
9460       47.60       -122.32
SELECT 
    host_id
    ,latitude
    ,longitude
FROM airbnb
GROUP BY
    host_id
    ,latitude
    ,longitude
HAVING COUNT(DISTINCT room_id) > 1

什么类型的SQL?你能提供一个你的模式的例子和一些样本数据吗?我已经编辑了这篇文章,所以它包括了表格。我对SQL完全陌生,但我使用的是databricks,所以我猜是sparkSQL。我已将其添加到关键字中。给定示例数据,您预期的结果是什么?现在的结果只是一个主机在一个位置有多个房间的情况下的所有属性值的列表。