【问题标题】:Update row and variable in PostgreSQL function更新 PostgreSQL 函数中的行和变量
【发布时间】:2021-08-01 16:06:36
【问题描述】:

我的表结构如下:

CREATE TABLE perf
(
startDate DATE,
performance DOUBLE PRECISION,
indexLevel  DOUBLE PRECISION
PRIMARY KEY (startDate)
);

startDateperformance 列是在上一步中填充的。现在我想以一种省时的方式设置indexLevel。在 MSSQL 中,这样做是这样的:

SET @Index = 100;
UPDATE perf SET @Index = @Index * (1+performance), indexLevel = @Index;

这如何在 PostgreSQL 中实现?

有没有比遍历表更好的解决方案:Iterate through table, perform calculation on each row

【问题讨论】:

  • 请提供样本数据和所需结果。

标签: sql postgresql performance sql-update


【解决方案1】:

如果我理解正确,您需要累积产品。一种方法使用算术进行计算:

select p.*,
       100 * exp(sum(1 + ln(performance)) over (order by startdate))
from perf p;

您可以将其合并到update

update perf p
    set indexLevel as p2.calculated_indexLevel
    from (select p.*,
                 100 * exp(sum(1 + ln(performance)) over (order by startdate)) as calculated_indexLevel
          from perf p
         ) p2
    where p2.startdate = p.startdate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多