Skip to content
Advertisement

mysql calculate using combination of sum and product formula

I have a table in mysql with data looking like this example.

I want to calculate a columnn colc using the formula:
C1 = A1*B1;
C2 = (C1+A2)*B2;
C3 = (C2+A3)*B3;
C4 = (C3+A4)*B4;

ColA ColB ColC
1 5 5
2 6 42
3 7 315
4 8 2552

Advertisement

Answer

You can use a recursive CTE:

See demo here.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement