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

avatar
作者
筋斗云
阅读量:0

  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 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!