Use built-in functions and GROUP BY in Transact-SQL