[英]Hive summary function inside case statement
我正在嘗試編寫一個簡單的 Hive 查詢:
select sum(case when pot_sls_q > 2* avg(pit_sls_q) then 1 else 0)/count(*) from prd_inv_fnd.item_pot_sls where dept_i=43 and class_i=3 where p_wk_end_d = 2014-06-28;
這里pit_sls_q
和pot_sls_q
都是 Hive 表中的列,我想要pot_sls_q
是pit_sls_q
平均值的 2 倍以上的記錄比例。 但是我得到錯誤:
失敗:語義異常 [錯誤 10128]:第 1:95 行尚不支持 UDAF 'avg' 的位置
為了鬼混,我什至嘗試使用一些窗口函數:
select sum(case when pot_sls_q > 2* avg(pit_sls_q) over (partition by dept_i,class_i) then 1 else 0 end)/count(*) from prd_inv_fnd.item_pot_sls where dept_i=43 and class_i=3 and p_wk_end_d = '2014-06-28';
考慮到在相同條件下對數據進行過濾或分區這一事實本質上是“相同”數據,這很好,但即使如此我也會出錯:
失敗:SemanticException [錯誤 10002]:第 1:36 行無效的列引用“avg”:(可能的列名是:p_wk_end_d、dept_i、class_i、item_i、pit_sls_q、pot_sls_q)
請建議正確的方法。
您正在SUM
中使用AVG
,這將不起作用(以及其他語法錯誤)。
嘗試解析AVG OVER ()
這個:
select sum(case when pot_sls_q > 2 * avg_pit_sls_q then 1 else 0 end) / count(*)
from (
select t.*,
avg(pit_sls_q) over () avg_pit_sls_q
from prd_inv_fnd.item_pot_sls t
where dept_i = 43
and class_i = 3
and p_wk_end_d = '2014-06-28'
) t;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.