简体   繁体   English

SQL查询值除以max(值)

[英]SQL Query value divided by the max(value)

I'm ashamed of myself 'cause i can't do this query properly... I have a table like this 我为自己感到羞耻,因为我不能正确地做这个问题......我有一张这样的桌子

  nom  | code_geo | valeur |       indice
-------+----------+--------+--------------------
 AISNE | 02       |  81573 | 0.05
 SOMME | 80       |  79520 | 0.03
 OISE  | 60       |  70004 | 0.09

what i need to do is divide each "indice" by the max(indice). 我需要做的是将每个“indice”除以max(indice)。 That is: 那是:

  nom  | code_geo | valeur |       indice
-------+----------+--------+--------------------
 AISNE | 02       |  81573 | 0.05 / 0.09
 SOMME | 80       |  79520 | 0.03 / 0.09
 OISE  | 60       |  70004 | 0.09 / 0.09

my first guess is: 我的第一个猜测是:

SELECT nom,code_geo,valeur,indice/(SELECT max(indice) FROM blablabla) FROM blablabla;

my problem is that "blablabla" is actually a function with 6 parameter and i don't want to repeat the FROM clause on the subquery... 我的问题是“blablabla”实际上是一个带有6个参数的函数,我不想在子查询上重复FROM子句...

Is there another (better?) way to do this? 还有另一种(更好的?)方法吗? or should i look the other way 或者我应该看另一种方式

Thx in advance Thx提前

You solution looks fine. 你解决方案看起来不错 Since the subquery is not correlated to the outer query, the DBMS should evaluate that subquery only once. 由于子查询与外部查询无关,因此DBMS应仅评估该子查询一次。

I believe Postgresql allows CTE. 我相信Postgresql允许CTE。

Select * into yourtable from blablabla


WITH T2(MaxNumber) as
(
select MAX(indice) as MaxNumber from yourtable
)
update t1
set t1.indice=t1.indice / T2.MaxNumber
from yourtable t1, T2

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

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