I have a table A
with n
rows (200+) and different numeric columns.
I have a table B
with m
rows (100K+) and a column called multipliers
, which is of type array (REAL[]
). For every row in B
, this array's length is n
, ie. a multiplier for every numeric variable in A
. The array is sorted to match the alphabetical order of the id
field in A
Table A
id | values_1 | values_2
---|----------|-------------
1 | 11.2 | 10.2
2 | 21.9 | 12.5
3 | 30.0 | 26.0
4 | 98.0 | 11.8
Table B
id | multipliers
--------|-------------
dafcec | {2,3,4,9}
nkjhbn | {0,0,1,5}
ccseff | {1,2,0,5}
ddeecc | {0,0,0,1}
I need a query that returns the SUM( multipliers * values_1 )
.
Like this:
b.id | sum(b.multipliers*a.value_1)
--------|----------------------------------
dafcec | 2*11.2 + 3*21.9 + 4*30.0 + 9*98.0
nkjhbn | 0*11.2 + 0*21.9 + 1*30.0 + 5*98.0
ccseff | 1*11.2 + 2*21.9 + 0*30.0 + 5*98.0
ddeecc | 0*11.2 + 0*21.9 + 0*30.0 + 1*98.0
I have tried with different subquerys, LATERAL
joins and UNNEST
, but I can't get a working result.
Any hints? Thanks!