我用的是蜂巢,所以HiveQL。
我有表格上的数据:
placename | Value | id
A | 1.1 | 1
A | 1.05 | 2
A | 2 | 3
A | 3 | 4
B | 2 | 1
B | 5 | 2
B | 2.1 | 3
B | 7 | 4
C | 1 | 1
C | 2 | 2
C | 3 | 3
C | 17 | 4
C | 17.11 | 5
C | 17.6 | 6 最后,我试图在给定范围内找到多个值的地名和ids列表。
我不确定--我不是SQL专家,在我的组织中也没有任何访问权限。
例如,
如果我有一个参数valuerange,我想要找到所有在另一个值的值范围内有多个值的地名和if。因此,在valuerange为0.5的情况下,我将返回:
因为A有1.1和1.05 -它们彼此在0.5以内,所以B和C的B-1和B -3在这里,因为2.1和2的值在0.5之内。
发现C-4,5,6是因为17,17.11,17.6在0.5之内。17在17.11的0.5以内,17.6在17.11的0.5之内。
发布于 2019-07-14 14:48:41
您需要表的自联接和ON子句中的valuerange条件:
select distinct t.placename, t.id
from tablename t inner join tablename tt
on t.placename = tt.placename
where t.id <> tt.id and tt.value between t.value - 0.5 and t.value + 0.5
order by t.placename, t.id或存在:
select distinct t.placename, t.id
from tablename t
where exists (
select 1 from tablename
where placename = t.placename and id <> t.id
and value between t.value - 0.5 and t.value + 0.5
)
order by t.placename, t.id请参阅演示(对于Server,但由于代码是标准SQL,我猜它也适用于Hive )。
结果:
> placename | id
> :-------- | -:
> A | 1
> A | 2
> B | 1
> B | 3
> C | 4
> C | 5
> C | 6https://stackoverflow.com/questions/57028340
复制相似问题