简体   繁体   English

聚合 function 上的算术运算

[英]Arithmetic Operations on Aggregate function

I have a table foo我有一张foo

  x   |  y  | value 
------+-----+-------
  274 | 617 |     1
   91 | 374 |    10
  996 | 716 |    40
  121 | 442 |     5

I want to add a computed column cal as (value - min(value))/(max(value) - min(value))我想将计算列cal添加为(value - min(value))/(max(value) - min(value))

Output Output

  x   |  y  | value | cal
------+-----+-------+-----
  274 | 617 |     1 | 0
   91 | 374 |    10 | 0.23
  996 | 716 |    40 | 1
  121 | 442 |     5 | 0.10

You can use window functions:您可以使用 window 函数:

select x, y, value, (value - min(value) over ()) / (max(value) over () - min(value) over ()) from t;

If value is an integer, you need to be careful about integer arithmetic:如果value integer,则需要注意 integer 算法:

select x, y, value, (value - min(value) over ()) * 1.0 / (max(value) over () - min(value) over ()) from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM