Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2005-04-20 : 05:56:54
|
| UPDATE Target SET Target.Col1 = SUM(Source.Col1 - Source.Col2)FROM source S INNER JOIN target TON S.PK = T.PK I can do this with temp tables or cursors, but surely there must be a better/faster way? |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-04-20 : 06:03:10
|
| UPDATE Target SET Target.Col1 = Source.Col1 - Source.Col2FROM source S INNER JOIN target TON S.PK = T.PK You are misinterpreting the objectives of the SUM function. |
 |
|
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2005-04-20 : 06:25:01
|
| The target is a summary table and the source is a details table.How do do I store for each summary (Sale) the sum profit which is the sum of all the "saleprice - costprice" for each detail in a sale? |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2005-04-20 : 06:36:42
|
[code]update Targetset Target.Col1 = a.Totalfrom (select Total = sum(Col1)-sum(Col2), PK from Source group by PK) as awhere a.PK = Target.PKgo[/code]------------->>> BREAKING NEWS!!! <<<------------- Saddam Hussien has weapons of mass destruction |
 |
|
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2005-04-20 : 07:04:35
|
| You my friend, are a darn genious!that worked a treat! lightning fast! |
 |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2005-04-20 : 07:17:24
|
No problem ------------->>> BREAKING NEWS!!! <<<------------- Saddam Hussien has weapons of mass destruction |
 |
|
|
|
|
|