数据库分组排序的方法有两种:按字段分组排序和按计算表达式分组排序。
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department
ORDER BY max_salary DESC;
SELECT CASE WHEN (YEAR(hire_date) % 2) = 0 THEN 'Even' ELSE 'Odd' END AS hire_year_group,
COUNT(*) AS employee_count
FROM employees
GROUP BY hire_year_group
ORDER BY employee_count DESC;
无论使用哪种方法,分组排序都可以通过GROUP BY子句对结果进行分组,再通过ORDER BY子句对每个分组进行排序,从而得到按指定字段或计算表达式分组排序的结果。