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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help on updating a column

Author  Topic 

wmr36104
Starting Member

3 Posts

Posted - 2011-09-20 : 00:09:21

I am very new to SQL and need help updating a table with data from another table. The table is an MS SQL Server 2005 type.
Here is my scenario:

I have table "aa" with say 100 records in it along with a unique customer id number.

I have table "bb" with varying quantities of records for each unique id number in table "aa".

Table "bb" has three fields that I want to sum into table "aa" for each customer id in table "bb". I have added the fields sum_a, sum_b and sum_c to table "aa". For simplicy, the same tables names exist in table "bb".

It is probably apparent that I don't know enough about SQL to identify the complete circumstance, so please let me know what I left out.

My capabilities right now are limited to using the query / execute section in SQL Server.

Any help would be very appreciated.

Thanks,


Walt

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 2011-09-20 : 02:23:42
Assuming you are "Updating" AA with data from BB and not inserting rows into AA, I think you could do something like this. Since you didn't define your table structure, I'm just guessing:

[CODE]
update #AA
set SumA=B2.SourceA, SumB=B2.SourceB, SumC=B2.SourceC
from (Select customerid, SUM(sourceA) as SourceA, SUM(SourceB) as SourceB, SUM(SourceC) as SourceC
From #BB
Group by customerid) B2

where b2.customerid=#AA.customerid

select *
from #AA
[/CODE]
Go to Top of Page

wmr36104
Starting Member

3 Posts

Posted - 2011-09-20 : 12:38:48
The table structure would be the same for both table "aa" and table "bb"

customerid int
sum_a int
sum_b int
sum_c int
Go to Top of Page

wmr36104
Starting Member

3 Posts

Posted - 2011-09-20 : 21:06:18
flamblaster - your posting worked perfectly. Thank you very much.
Go to Top of Page
   

- Advertisement -