Solved

SQL Update with nested select and aggregate function

Posted on 2008-06-12
3
4,165 Views
Last Modified: 2008-06-13
I'm trying to update one column of a table with the following query:

UPDATE Utable
Set Amt =
   (SELECT
        Sum(Amt)
    FROM Ptable
    WHERE ID IN
        (SELECT ID FROM UTable)
    GROUP BY
        ID,
        Col1,
        Col2,
        Col3
    )

I'd like the sum of AMT from Ptable to be placed in Utable  but I keep getting various error messages -- can I do this in one update statement -- How would I do it?
   
0
Comment
Question by:dkcoop03
3 Comments
 
LVL 8

Accepted Solution

by:
i2mental earned 250 total points
ID: 21772287
I'm going to assume that Ptable has multiple rows to each Utable ID.

UPDATE Utable
set utable.amt = (select sum(amt) from Ptable where ptable.id = utable.id)
0
 

Author Comment

by:dkcoop03
ID: 21778165
I have one question regarding this solution -- why doesn't  the nested select with the Sum(amt) require a group by clause?
0
 

Expert Comment

by:crobledo
ID: 31817188
MS SQL:

Can I work a query as such

Max(Count(field1), Count(field2), Count(field3))  AS thisField

In other words can I easily nest aggregate function
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question