Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Ingres Topics / September 2006

Tip: Looking for answers? Try searching our database.

SQL help nedded......

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manny - 21 Sep 2006 18:46 GMT
Hello all,

Is it possible to do a sum in a sub-query which is grouped and then use
that to update a value in the main query?
Not sure if I have explained myself very well so.....

The table below has the column 'amount' which I want to update
(where dept = 'A101' and level = 0 and code = code from table
sub_start):

select * from tot_amount
+------+------+--------+-------+
|Dept  |Code  |level   |amount |
+------+------+--------+-------+
|A101  |AA1234|       0|      0|
|A101  |AA1234|       1|      0|
|B303  |AA1234|       0|      0|
|A101  |BB1234|       0|      0|
|A101  |BB1234|       1|      0|
+------+------+--------+-------+

The table below has the values which I need to group and use to update
the amount in the above table:

select * from sub_start
+------+------+---------+
|Dept  |Code  |qty      |
+------+------+---------+
|A101  |AA1234|       20|
|B303  |AA1234|       10|
|A101  |AA1234|       10|
|A101  |AA1234|       10|
|A101  |BB1234|       10|
|B303  |BB1234|        2|
|A101  |BB1234|       16|
+------+------+---------+

e.g. the amount value for:
row 1 should be updated to 40 (20 + 10 + 10)
row 2 should be 0
row 3 should be 0
row 4 should be 26 (10 + 16)
row 5 should be 0

Can this be done in one query?

Thanks in advance,

Manny
Karl & Betty Schendel - 21 Sep 2006 19:14 GMT
>Hello all,
>
>Is it possible to do a sum in a sub-query which is grouped and then use
>that to update a value in the main query?

No, alas.  Scalar subqueries (which is what you describe) are not
yet allowed on the right-hand side of an update SET assignment.

Maybe someday...until then, either use a view, or declare a global
temporary table containing the intermediate sums.

Karl
Manny - 21 Sep 2006 21:06 GMT
Ok....thanks for clarifying that for me.

Manny.

> >Hello all,
> >
[quoted text clipped - 8 lines]
>
> Karl
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.