hiveql:将一列的所有值除以该列的最大值

l7wslrjt  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(634)

如何将一列的所有值除以该列的最大值?
例如,假设我有:

id value
1  10
2  20
3  30

我想要:

id value
1  0.33333
2  0.66666
3  0.99999

我试过:

SELECT col_a/MAX(col_a)
FROM db.table

SELECT col_a/(SELECT MAX(col_a) FROM db.table)
FROM db.table

两次尝试都失败了,错误信息很长。
当我复制粘贴下面的第一个答案时,我可以让它的代码工作,但是我不能用自己的表复制它的结果。我试过:

WITH temp AS (SELECT * FROM mydb.tablenamehere)
SELECT colA/MAX(colA) OVER()
FROM temp;

还有:

USE mydb;
WITH temp AS (SELECT * FROM tablenamehere)
SELECT colA/MAX(colA) OVER()
FROM temp;

但我得到以下两个错误:

FAILED: SemanticException Line 1:28 Table not found 'tablenamehere' in definition of CTE temp [
SELECT * FROM tablenamehere
] used as temp at Line 1:28
qcuzuvrc

qcuzuvrc1#

使用分析 max() :

with your_table as ( --use your table instead of this
select stack(3,
1,  10 ,
2,  20 ,
3,  30 ) as (id, value)
)

select id, value/max(value) over() as value 
  from your_table
order by id --remove order if not necessary
;

退货:

OK
1       0.3333333333333333
2       0.6666666666666666
3       1.0
Time taken: 80.474 seconds, Fetched: 3 row(s)

相关问题