温馨提示×

sql collapse函数的高级用法有哪些

sql
小樊
82
2024-07-26 21:18:10
栏目: 云计算

  1. 使用聚合函数进行分组
SELECT name, SUM(score) as total_score
FROM students
GROUP BY name
  1. 使用条件进行分组
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
  1. 使用聚合函数进行排序
SELECT name, SUM(score) as total_score
FROM students
GROUP BY name
ORDER BY total_score DESC
  1. 使用聚合函数进行筛选
SELECT name, SUM(score) as total_score
FROM students
GROUP BY name
HAVING total_score > 200
  1. 使用窗口函数
SELECT name, score, SUM(score) OVER (PARTITION BY name) as total_score
FROM students

0