簡體   English   中英

Hive [錯誤 10025]:表達式不在 GROUP BY 鍵名中

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

我正在嘗試選擇由組內變量中的關鍵術語定義的記錄。

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

我看不出我的代碼有什么問題,但我遇到了同樣的錯誤:

[錯誤 10025]:表達式不在 GROUP BY 鍵名中

感謝您的任何幫助

你可以用窗口函數來做到這一點:

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)記錄的標志的窗口總和。

如果不想重復計算標志的表達式,可以使用附加子查詢:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM