- the move of get_result_xx() functions out of the main function is
more complicated; moved them back into the main function for now
- write current SQL output to temporary file now not after the first
aggregation loop but at the end together with the other temporary SQL
commands; to prevent half-written SQL statements from which to restart
the code is more difficult