Speed up CSV generation by making single query for download stats

I've compared the CSVs generated for a number of different queries and the md5sums are the same.

Git blame points the finger at @rossjones so maybe he should review? ;)