Smart formatting of query results