SQL 查询值除以 max(value)

发布于 2024-09-17 17:25:27 字数 780 浏览 2 评论 0原文

我为自己感到羞耻,因为我无法正确执行此查询...我有一个像这样的表,

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

我需要做的是将每个“索引”除以最大值(索引)。那是:

  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

我的第一个猜测是:

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

我的问题是“blablabla”实际上是一个带有 6 个参数的函数,我不想在子查询上重复 FROM 子句...

是否有另一种(更好?)的方法来做到这一点?或者我应该另眼相看,

谢谢

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). 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...

Is there another (better?) way to do this? or should i look the other way

Thx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-09-24 17:25:27

你的解决方案看起来不错。由于子查询与外部查询不相关,因此 DBMS 应该只评估该子查询一次。

You solution looks fine. Since the subquery is not correlated to the outer query, the DBMS should evaluate that subquery only once.

弃爱 2024-09-24 17:25:27

我相信 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

I believe Postgresql allows 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文