Sql 案例语句中的配置单元摘要函数

Sql 案例语句中的配置单元摘要函数,sql,hadoop,hive,Sql,Hadoop,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\u sls\u q和pot\u sls\u q都是配置单元表中的列,我想要的是pot\u sls\u q的

我正在尝试编写一个简单的配置单元查询:

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\u sls\u q
pot\u sls\u q
都是配置单元表中的列,我想要的是
pot\u sls\u q
的记录比例超过
pit\u sls\u q
平均值的2倍。但是我得到了一个错误:

失败:SemanticException[错误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;