案例陈述

时间:2017-01-17 10:51:23

标签: sql hadoop hive

我正在尝试编写一个简单的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_qpot_sls_q都是Hive表格中的列,我想要pot_sls_qpit_sls_q平均值高2倍的记录比例。但是我得到错误:

  

FAILED: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)

请建议正确的方法。

1 个答案:

答案 0 :(得分:1)

您在AVGSUM使用了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; 这个:

.nav-primary {
  background: #CCC;
}
.nav-primary li {
  list-style-type: none;
  display: inline-block;
  text-align: left;
  font-size: 26px;
  line-height: 0px;
  vertical-align: middle;  /* add this */
}
.nav-primary a {
  display: inline-block;
  padding: 32px 20px;
  border: 1px dashed black;
  /* border only used for display purpose of alignment*/
  margin-right: -4px;
}
.nav-primary a:hover {
  background: #FFF;
}
.menu {
  width: 100%;
}
.menu-item {} .home a {
  font-size: 0px;
}
.home a:after {
  font-family: "dashicons";
  content: " \f102";
  font-size: 40px;
  display: inline-block;
  vertical-align: middle;
  line-height: 0px;
}
相关问题