數(shù)據(jù)庫(kù)分組排序的方法有兩種:按字段分組排序和按計(jì)算表達(dá)式分組排序。
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;
無(wú)論使用哪種方法,分組排序都可以通過(guò)GROUP BY子句對(duì)結(jié)果進(jìn)行分組,再通過(guò)ORDER BY子句對(duì)每個(gè)分組進(jìn)行排序,從而得到按指定字段或計(jì)算表達(dá)式分組排序的結(jié)果。