0

I have the following table structure:

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

The columns startDate and performance are filled in a previous step. Now I want to set the indexLevel in a time-efficient way. In MSSQL this was done like:

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

How could this be implemented in PostgreSQL?

Is there a better solution than iterating through the table like here: Iterate through table, perform calculation on each row

1
  • Please provide sample data and desired rsults. Commented May 11, 2021 at 11:11

1 Answer 1

1

If I understand correctly, you want a cumulative product. One method uses arithmetic for the calculation:

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

You can incorporate this into an 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.