Move aggregation into sqlite

Rather than doing aggregations in awk, error-prone and slower