So part of my query is like this
select
column_a,
0.75 * power(0.93, (0.1428 * column_b))
let's say column_b
has the value of 3
is there a way to represent this line 0.75 * power(0.93, (0.1428 * column_b))
as this?
0.75 * power(0.93, (0.1428 * 3)) 0.75 * power(0.93, (0.1428 * 2)) 0.75 * power(0.93, (0.1428 * 1))
Basically, for what ever value column_b has, the function repeats. Is there a way to represent this in Postgres sql?
CodePudding user response:
Since this calculation will find the sum of 0.75 * power(0.93, (0.1428 * column_b))
repeated by the number of times in column_b
, you could use generate_series
to genera the increments to the amount in column_b
before summing the result of this calculation eg.
Schema (PostgreSQL v13)
create table sample (
column_a char(1),
column_b int
);
insert into sample values ('a',3), ('b',2);
Using generate_series to generate the possible numbers
select
column_a,
column_b,
array(
select b_val
from generate_series(1,column_b) b_val
) as computed_column
from sample;
column_a | column_b | computed_column |
---|---|---|
a | 3 | 1,2,3 |
b | 2 | 1,2 |
Finding the sum of the repeated calculation
select
column_a,
column_b,
(
select sum(0.75 * power(0.93, (0.1428 * b_val)))
from generate_series(1,column_b) b_val
) as computed_column
from sample;
column_a | column_b | computed_column |
---|---|---|
a | 3 | 2.203924916826077250 |
b | 2 | 1.476883153703056500 |