Question : 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.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
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

Answer : How do I perform a calculation in an update query?

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
Random Solutions  
 
programming4us programming4us