如何检查配置单元中having子句的顺序?

gab6jxml  于 2021-05-29  发布在  Hadoop
关注(0)|答案(3)|浏览(384)

我希望能够编写一个查询,告诉我哪些组的数据不具有序列中的每个数字。
例如,我的table是这样的:

Columns:   sequence  group1
           0         ADM
           1         ADM
           0         GDM
           2         GDM
           3         GDM
           0         WJK

而且,我想知道 group1 包含从开始的所有数字 0 还有数数。所以,在这种情况下, ADM 以及 WJK 会被退回,但是 GDM 不会的。 GDM 不会,因为它从0、2、3开始并跳过1。
如何在中编写查询 HIVE 告诉我列中的唯一值 group1 是否按顺序包含所有整数?

vwoqyblh

vwoqyblh1#

可以使用count和max,因为序列从零开始

select group1
 from my_table 
 group by group1
 having count(*) = max(sequence) +1

最后一句话你可以用

select group1
 from my_table 
 group by group1
 having count(distinct sequence) = max(sequence) +1
ct3nt3jp

ct3nt3jp2#

如果 sequence 可能有重复的,那你就要小心了。一种方法是取一个实数序列,减去它,并确保差值是连续的:

select group1
from (select t.*,
             (sequence - row_number() over (partition by group1 order by sequence)) as diff
      from t
     ) t
group by group1
having min(diff) = max(diff);
utugiqy6

utugiqy63#

SELECT group1
FROM
    TableName
GROUP BY
    group1
HAVING
    COUNT(*) = MAX(sequence) - MIN(sequence) + 1

如果基于0或正整数,则此操作有效

相关问题