create proc calculating_pop_summations_A as SELECT Sum_count = sum(Person_count), Sex, Aggregated_area INTO #tmp FROM Geo_postaggregated_pop_count GROUP BY Sex, Aggregated_area update Geo_postaggregated_pop_count SET Sum_female_count = t.Sum_count FROM Geo_postaggregated_pop_count G , #tmp t WHERE G.Aggegated_area = t.Aggregated_area AND G.Sex = t.Sex AND G.Sex = 2 update Geo_postaggregated_pop_count SET Sum_male_count = t.Sum_count FROM Geo_postaggregated_pop_count G , #tmp t WHERE G.Aggegated_area = t.Aggregated_area AND G.Sex = t.Sex AND G.Sex = 1