Home > Software engineering >  Multiply values of same column with same id
Multiply values of same column with same id

Time:08-24

There is a table like this:

ID Value
1 0.2
2 0.55
1 0.4
3 0.6
1 0.4
... ...

I want to multiply Values where I have the same ID.

So that I get a table looking like this:

ID Value
1 0.032
2 0.55
1 0.032
3 0.6
1 0.032
... ...

Unfortunately I have no point where to start for this that makes sense...

CodePudding user response:

There's a trick to doing that using exp, sum, and log.

select ID,  exp(sum(log(Value))) as Value
from t
group by ID
ID Value
1 0.08
2 0.55
3 0.6

Fiddle

CodePudding user response:

Try this

SELECT id, (a.value * b.value) as sum
FROM tablename, tablename a
WHERE tablename.id = a.id
  •  Tags:  
  • sql
  • Related