Hive [错误 10025]:表达式不在 GROUP BY 键名中

Hive [Error 10025]: Expression not in GROUP BY key name

我正在尝试 select 由组内变量中的关键术语定义的记录。

name 是一个包含感兴趣的关键术语的字符串。

组由 id1 和 id2 的组合定义。

我对按包含关键术语的组提取记录很感兴趣。

select id1, id2, name
   case
    when name LIKE '%LOAD_TIME' then 1
    when name LIKE '%LOGIN_SESSION_TIME' then 1
   end as b_flag
   from df1
   group by id1, id2
   having (sum(b_flag) > 0 )

df1:

id1  id2  name                               
1     1    xxxLOAD_TIME
1     1    xxxLOGIN_LOGIN_SESSION_TIMExxx
1     1    xxxxSome other timexxxx
2     2    xxSome other timex
3     1    xxxLOAD_TIME
3     1    xxSome other timexx

创建 b_flag 后,新数据集应如下所示:

id1  id2  name                             b_flag   
1     1    xxxLOAD_TIME                      1
1     1    xxxLOGIN_LOGIN_SESSION_TIMExxx    1
1     1    xxxxSome other timexxxx   
2     2    xxSome other timex
3     1    xxxLOAD_TIME                      1
3     1    xxSome other timexx

期望的输出:

   id1  id2  name                             b_flag   
    1     1    xxxLOAD_TIME                      1
    1     1    xxxLOGIN_LOGIN_SESSION_TIMExxx    1
    1     1    xxxxSome other timexxxx   
    3     1    xxxLOAD_TIME                      1
    3     1    xxSome other timexx

我看不出我的代码有什么问题,但我一直遇到同样的错误:

[Error 10025]: Expression not in GROUP BY key name

感谢您的帮助

您可以使用 window 函数来做到这一点:

select id1, id2, name, b_flag
from (
    select 
        t.*, 
        case when name LIKE '%LOAD_TIME' or name LIKE '%LOGIN_SESSION_TIME' then 1 end b_flag,
        sum(case when name LIKE '%LOAD_TIME' or name LIKE '%LOGIN_SESSION_TIME' then 1 end) 
            over(partition by id1, id2) sum_b_flag
    from mytable t
) t
where sum_b_flag > 0

内部查询检查当前行是否符合条件,并为具有相同 (id1, id2).

的记录的标志计算 window 总和

如果不想重复计算标志的表达式,可以使用额外的子查询:

select id1, id2, name, b_flag
from (
    select t.*, sum(b_flag) over(partition by id1, id2) sum_b_flag
    from (
        select 
            t.*, 
            case when name LIKE '%LOAD_TIME' or name LIKE '%LOGIN_SESSION_TIME' then 1 end b_flag,
        from mytable t
    ) t
) t
where sum_b_flag > 0

尝试

select 
    main.id1, 
    main.id2, 
    main.name,
    case
        when main.name LIKE '%LOAD_TIME' then 1
        when main.name LIKE '%LOGIN_SESSION_TIME' then 1
    end as b_flag
from 
    df1 main
    left semi join (
        select distinct id1, id2 from df1 
        where (case
            when name LIKE '%LOAD_TIME' then 1
            when name LIKE '%LOGIN_SESSION_TIME' then 1
        end)=1 ) f 
on main.id1=f.id1 and main.id2=f.id2