Refactoring 5: Extract temp to query (total_amount)