我有一个按州和县计数的数据集,我想按州和县计算中位数和平均值,例如:
有:
ID state county count
1 MD aa 2
2 MD aa 4
3 VA bb 1
4 VA bb 2
5 VA bb 4
6 VA cc 7
7 VA cc 8
想要:
到目前为止我给我的错误:
Select id, STATE,COUNTY,count,
percentile(cast(count as BIGINT), 0.5) OVER() as overall_median,
round(avg(count),2) OVER() as overall_avg,
percentile(cast(count as bigint),0.5) OVER(PARTITION BY id,STATE) as med_state,
percentile(cast(count as bigint),0.5) as med_county,
AVG(count) OVER (PARTITION BY id, STATE) as avg_state,
AVG(count) AS avg_county,
from have
group by id, state, county
不使用组时收到错误:
错误:执行错误:org.apache.hive.service.cli.HiveSQLException:编译语句时出错:FAILED:SemanticException无法将窗口调用分解为组。至少1组必须仅取决于输入列。同时检查循环依赖项。基本错误:org.apache.hadoop.hive.ql.parse.SemanticException:行1:457表达式不在GROUP BY键'id'
不带组的代码:
Select id, STATE,county,count,
percentile(cast(count as BIGINT), 0.5) OVER() as overall_median,
round(avg(count),2) OVER() as overall_avg,
percentile(cast(count as bigint),0.5) OVER(PARTITION BY id,STATE) as med_state,
percentile(cast(count as bigint),0.5) OVER(PARTITION BY id,STATE,county) as med_county,
AVG(count) OVER (PARTITION BY id, STATE) as avg_state,
AVG(count) OVER (PARTITION BY id, STATE, county) as avg_county,
from have
谢谢!
修复它:round(avg(count)OVER(),2)
select
id, STATE, county, count,
percentile(cast(count as BIGINT), 0.5) OVER() as overall_median,
round(avg(count) OVER(), 2) as overall_avg,
percentile(cast(count as bigint), 0.5) OVER(PARTITION BY id, STATE) as med_state,
percentile(cast(count as bigint), 0.5) OVER(PARTITION BY id, STATE, county) as med_county,
AVG(count) OVER (PARTITION BY id, STATE) as avg_state,
AVG(count) OVER (PARTITION BY id, STATE, county) as avg_county
from
have
提示:请勿将关键字(即计数)用作列名-将来会遇到很多问题