473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to update a table with SUM function?

Table test2 has multiple amounts for each account, I would like to sum
the amounts for the same account and use the result to update the
variable 'tot_amount' in table test1. But SQL does not allow me to use
sum function in update. Is there any other way to do this? Thanks.

update test1
set tot_amount=sum(b.amount)
from test1 as a
join test2 as b
on a.acc_no=b.acc_no

Jul 23 '05 #1
4 79414
On 4 Feb 2005 12:28:25 -0800, gooday wrote:
Table test2 has multiple amounts for each account, I would like to sum
the amounts for the same account and use the result to update the
variable 'tot_amount' in table test1. But SQL does not allow me to use
sum function in update. Is there any other way to do this? Thanks.


Hi gooday,

The best option is to not do this at all. Don't store values that you can
calculate, just calculate them when you retireve the data, as part of the
SELECT statement ((or put it in a view if you want it easy).

If you do store it, you'll soon find differences between the stored data
and the actual data and you'll find yourself plastering the database with
triggers to keep the calculated column current after each update.

Anyway, if you still feel that you have a valid reason for doing it like
this, here is how you could accomplish it:

UPDATE test1
SET tot_amount = (SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)

This will update all rows in test1; those without matching rows in test2
will have tot_amount set to NULL. If you prefer 0, use COALESCE:

UPDATE test1
SET tot_amount = COALESCE((SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no), 0)

And if you prefer not to update the tot_amount value for rows that have no
match in the test2 table (as your non-working query would do - but I don't
think you intended that!), then use

UPDATE test1
SET tot_amount = (SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)
WHERE EXISTS (SELECT *
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Thanks Hugo, that is a great help!

Yes, calculate up front in SELECT statement is a great idea, I also
found it cumbersome to keep updating the column. The only reason I
didn't do it in SELECT is because I selected more than 30 variables at
the same time (some of them are real variables and some of them are
created using other variables). If I use SUM function up front, I have
to use GROUP BY at the end and make sure all variables in SELECT are
included, which makes the query really long. Is there a better way to
avoid this?

I have another question... After creating a table and finishing 10
different UPDATE in a order, if I then found one update needs to be
fixed, do I need to re-run the whole table and all other 9 updates?
Thanks.

Hugo Kornelis wrote:
On 4 Feb 2005 12:28:25 -0800, gooday wrote:
Table test2 has multiple amounts for each account, I would like to sumthe amounts for the same account and use the result to update the
variable 'tot_amount' in table test1. But SQL does not allow me to usesum function in update. Is there any other way to do this? Thanks.
Hi gooday,

The best option is to not do this at all. Don't store values that you

can calculate, just calculate them when you retireve the data, as part of the SELECT statement ((or put it in a view if you want it easy).

If you do store it, you'll soon find differences between the stored data and the actual data and you'll find yourself plastering the database with triggers to keep the calculated column current after each update.

Anyway, if you still feel that you have a valid reason for doing it like this, here is how you could accomplish it:

UPDATE test1
SET tot_amount = (SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)

This will update all rows in test1; those without matching rows in test2 will have tot_amount set to NULL. If you prefer 0, use COALESCE:

UPDATE test1
SET tot_amount = COALESCE((SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no), 0)

And if you prefer not to update the tot_amount value for rows that have no match in the test2 table (as your non-working query would do - but I don't think you intended that!), then use

UPDATE test1
SET tot_amount = (SELECT SUM(b.amount)
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)
WHERE EXISTS (SELECT *
FROM test2 AS b
WHERE b.acc_no = test1.acc_no)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #3
On 4 Feb 2005 13:50:15 -0800, gooday wrote:
Thanks Hugo, that is a great help!

Yes, calculate up front in SELECT statement is a great idea, I also
found it cumbersome to keep updating the column. The only reason I
didn't do it in SELECT is because I selected more than 30 variables at
the same time (some of them are real variables and some of them are
created using other variables). If I use SUM function up front, I have
to use GROUP BY at the end and make sure all variables in SELECT are
included, which makes the query really long. Is there a better way to
avoid this?
Hi gooday,

I'm not sure what you mean. Could you post the table structure (as CREATE
TABLE statements, including constraints and properties but excluding
irrelevant columns), some sample data (as INSERT statement) and your
current query? See www.aspfaq.com/5006.

I have another question... After creating a table and finishing 10
different UPDATE in a order, if I then found one update needs to be
fixed, do I need to re-run the whole table and all other 9 updates?


If for example the fifth UPDATE is the one that went wrong, the sixth and
seventh don't use any data changed by the fifth, but the eighth does and
the ninth and tenth use columns affected by the eigtht, then you should
probably rerun the fifth (after correcting it!), eighth, ninth and tenth
only.

To be sure, you'd need to carefully inspect what each update statement
does and how each statement affects the statements that follow it.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4
gooday (ro******@gmail.com) writes:
Yes, calculate up front in SELECT statement is a great idea, I also
found it cumbersome to keep updating the column. The only reason I
didn't do it in SELECT is because I selected more than 30 variables at
the same time (some of them are real variables and some of them are
created using other variables). If I use SUM function up front, I have
to use GROUP BY at the end and make sure all variables in SELECT are
included, which makes the query really long. Is there a better way to
avoid this?
There might be, but not seeing your query it's a bit too much of a
dark room to tell where the light switch might be.
I have another question... After creating a table and finishing 10
different UPDATE in a order, if I then found one update needs to be
fixed, do I need to re-run the whole table and all other 9 updates?


Even more difficult to tell. If the the result of one UPDATE affects
the result of the next, yes. If not, no.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: amwi | last post by:
I have tried to solve this on my own for a long time now, so i really need some help here... I use Oracle 10.1 and SQL *plus 10.1. How do i update table a.fkid from table b.pkid with the...
0
by: Ferindo Middleton Jr | last post by:
I am trying to write a Perl Function for one of the databases I'm building a web application for. This function is triggered to occur BEFORE INSERT OR UPDATE. This function is complex in that it...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
6
by: dharmadam | last post by:
Is it possible to pass a column name or the order of the column name in the DB2 table table function. For example, I want to update the address of a person by passing one of the address column name...
4
by: peteh | last post by:
Hello All; The environment is DB2 AIX 8.1.5 (parallel edition) being accessed by a Windows 8.1.2 admin client via Quest. I'm trying to use the snapshot_lockwait table function and getting...
9
by: baonks | last post by:
hello all here is my problem: I have 2 table 1: K_POS SALDO_A_D SALDO_A_K 11100 105 5 11200 5 105
2
by: Ron | last post by:
Hello, I am trying to do a simple update on employee information. I am a novice at both aspx and SQLServer, so I hope you are not too offended by my code. The following is the update code: ...
2
by: Paul712 | last post by:
Recently, I have a table that I use to update a master table. When I run the same Update query that's been successful in the past, most all of the data in the fields in the update fields has been...
1
by: mrobinsc | last post by:
** This SQL statement returns 4 rows SELECT COUNT(*) G.ACTIVITY_ID G.RESOURCE_TYPE G.RESOURCE_CATEGORY G.RESOURCE_SUB_CAT G.ANALYSIS_TYPE G.PROJECT_ID
1
by: adithi | last post by:
My Table Structure is: Table A Table B Table C colA -PK Col B-PK Col C-PK Col B-FK ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.