Update multiple values in a single statement
Posted
by Kluge
on Stack Overflow
See other posts from Stack Overflow
or by Kluge
Published on 2008-11-14T00:35:27Z
Indexed on
2010/05/04
21:18 UTC
Read the original article
Hit count: 119
sql
|advantage-database-server
I have a master / detail table and want to update some summary values in the master table against the detail table. I know I can update them like this:
update MasterTbl set TotalX = (select sum(X) from DetailTbl where DetailTbl.MasterID = MasterTbl.ID)
update MasterTbl set TotalY = (select sum(Y) from DetailTbl where DetailTbl.MasterID = MasterTbl.ID)
update MasterTbl set TotalZ = (select sum(Z) from DetailTbl where DetailTbl.MasterID = MasterTbl.ID)
But, I'd like to do it in a single statement, something like this:
update MasterTbl set TotalX = sum(DetailTbl.X), TotalY = sum(DetailTbl.Y), TotalZ = sum(DetailTbl.Z)
from DetailTbl
where DetailTbl.MasterID = MasterTbl.ID group by MasterID
but that doesn't work. I've also tried versions that omit the "group by" clause. I'm not sure whether I'm bumping up against the limits of my particular database (Advantage), or the limits of my SQL. Probably the latter. Can anyone help?
© Stack Overflow or respective owner