Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Update with nested select and aggregate function

Posted on 2008-06-12
3
Medium Priority
?
4,173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Accepted Solution

by:
i2mental earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

604 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