- 使用聚合函数进行分组
SELECT name, SUM(score) as total_score FROM students GROUP BY name
- 使用条件进行分组
SELECT name, SUM(CASE WHEN subject='Math' THEN score ELSE 0 END) as math_score, SUM(CASE WHEN subject='English' THEN score ELSE 0 END) as english_score FROM students GROUP BY name
- 使用聚合函数进行排序
SELECT name, SUM(score) as total_score FROM students GROUP BY name ORDER BY total_score DESC
- 使用聚合函数进行筛选
SELECT name, SUM(score) as total_score FROM students GROUP BY name HAVING total_score > 200
- 使用窗口函数
SELECT name, score, SUM(score) OVER (PARTITION BY name) as total_score FROM students