?
Solved

How do I perform a calculation in an update query?

Posted on 2010-01-09
3
Medium Priority
?
433 Views
Last Modified: 2012-05-08
I need to perform a query that first calculates the total weight for a category. There are not a set number of categories. Then I need to take the total weight for each category and update the weights withing that category by dividing the current value by the sum. Any help is appreciated on this.
UPDATE Testing SET weight = weight/b.weight
FROM Testing a
	inner join
	(
		SELECT SUM(weight) as 'weight' FROM Testing GROUP BY category

	) b on a.Category = b.Category

Open in new window

0
Comment
Question by:nisupport
[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 57

Expert Comment

by:HainKurt
ID: 26276201
can you please give some sample data before and after the query...

I could not understand the logic here...
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26276202
this query seems to be correct!
what error do you receive when run this statement?
try code below it's with alias names now

UPDATE Testing SET a.weight = a.weight/b.weight
FROM Testing a
        inner join
        (
                SELECT SUM(weight) as 'weight' FROM Testing GROUP BY category

        ) b on a.Category = b.Category

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 26276208
Hello nisupport,

UPDATE a
SET a.weight = a.weight/b.weight
FROM Testing a
inner join
(
        SELECT Category, SUM(weight) as 'weight' FROM Testing GROUP BY category
) b on a.Category = b.Category




Regards,

aneeshattingal
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

741 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