Link to home
Start Free TrialLog in
Avatar of nisupport
nisupport

asked on

How do I perform a calculation in an update query?

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

Avatar of HainKurt
HainKurt
Flag of Canada image

can you please give some sample data before and after the query...

I could not understand the logic here...
Avatar of Reza Rad
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

ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial