Solved

SQL Update with nested select and aggregate function

Posted on 2008-06-12
3
4,159 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now