L. De la Cruz
L. De la Cruz

Reputation: 3

Save query result into specific column

This might seem like a noob question, but here's the thing:

Here's my two tables

In the image, the first table name is facturaDetalle and the second one's facturamaster

I want to SUM all the total matching idfactura in facturadetalle and save them into the total column in the facturamaster table.

Something like this

I'm working on a master-detail form in ASP.NET

Upvotes: 0

Views: 60

Answers (2)

Bogdan Sahlean
Bogdan Sahlean

Reputation: 1

UPDATE m
SET total = (SELECT SUM(d.total) FROM dbo.facturadetalle d WHERE d.idfactura  = m.idfactura)
FROM dbo.facturamaster m
--WHERE m.total IS NULL

Upvotes: 1

Venkataraman R
Venkataraman R

Reputation: 12959

You can use UPDATE statement to do that.

UPDATE m
SET TOTAL = SUM(d.Total)
FROM  idfactura AS m
INNER JOIN idfacturaldetelle AS d
ON m.idfactura = d.idfactura

Upvotes: 1

Related Questions