"Invalid aggregate reference." when doing an update with a SUM

56 Views Asked by At

Good morning, I want to update my table, I want to add the net cost of my product and this net cost is the sum of my unit column + the freight column.

I'm doing the UPDATE like this:;

update com_entradas_produtos ep;
set ep.custo_item = sum(ep.unitario + ep.frete);
where ep.empresa = :empresa and ep.codigo = :codigo and ep.produto = :produto;

It is returning me the following error:

SQL error code = -104. Invalid aggregate reference.

Could you help me how should I do this update.

1

There are 1 best solutions below

0
3N1GM4 On

As Mark outlines in his comment above, aggregate functions like SUM() are used to aggregate across rows, but it sounds like you want to just sum multiple columns on each row, so you want something like:

update com_entradas_produtos
set ep.custo_item = ep.unitario + ep.frete
where [whatever logic you want]