The easiest way would appear to be by using a 'group by' clause in your SQL and wrapping a 'sum()' around each non-grouped column. Also, is there any particular reason that you're extracting the resultset to a flat file and re-parsing it rather than using the SQL output directly?
rdfield