sub calculating_pop_summations_A { my $sthZK_B = $dbh->prepare(" SELECT DISTINCT Aggregated_area FROM Geo_postaggregated_pop_count; ") or die "Couldn't prepare query: ".$dbh->errstr; $sthZK_B->execute() or die "Couldn't execute query: ".$sthZK_B->errstr; my @sthZK_B_A_array; while ( my @columns_B = $sthZK_B->fetchrow() ) { my $Aggregated_area = $columns_B[0]; # The /3 is required because the my $sthZK_B_A = " UPDATE Geo_postaggregated_pop_count SET Sum_male_count = (SELECT SUM(G.Person_count) FROM Geo_postaggregated_pop_count G WHERE Sex = 1 AND Aggregated_area LIKE '".$Aggregated_area."') FROM Geo_postaggregated_pop_count WHERE Aggregated_area LIKE '".$Aggregated_area."'; UPDATE Geo_postaggregated_pop_count SET Sum_female_count = (SELECT SUM(G.Person_count) FROM Geo_postaggregated_pop_count G WHERE Sex = 2 AND Aggregated_area LIKE '".$Aggregated_area."') FROM Geo_postaggregated_pop_count WHERE Aggregated_area LIKE '".$Aggregated_area."'; "; push (@sthZK_B_A_array, $sthZK_B_A); } foreach (@sthZK_B_A_array){ my $sthZK_C = $dbh->prepare(" ".$_." ") or die "Couldn't prepare query: ".$dbh->errstr; $sthZK_C->execute() or die "Couldn't execute query: ".$sthZK_C->errstr; } }