Update table with sum

0

Good morning People, I need the column "Expenses" to be added as new records are being entered. Try to do it with an update, I work in mysql but doing it in sql server throws me an error. The sales id is with the number 3 because before loading it, load 2 records in the table.

The table as I would like it to remain would be like this:

+----------+-------+----------+
| IdVentas |   Mes |    Gasto |
+----------+-------+----------+
|        1 |     1 |       10 |
|        2 |     1 |       20 |
|        3 | Acum. |       30 |
+----------+-------+----------+

I await your comments. Greetings

UPDATE
    ventas v,
    (SELECT sum(gasto) as mysum
     FROM ventas
     where idventas <> 3
    ) as s
SET
    v.gasto = s.mysum
WHERE
    v.idventas = 3
    
asked by Guillermo Rubio 07.08.2018 в 14:22
source

1 answer

0

If I understand correctly, this is what you need ...

UPDATE  v set Gasto  = S.mysum 
from Ventas v
inner join 
(    select  sum(Gasto) as mysum
     FROM Ventas v
     where IdVentas <> 3
	
)s
on 1=1
where IdVentas = 3
    
answered by 07.08.2018 в 15:38