1.基础语法:case...when与java中的if...else非常相似
case when 条件 then 结果1 else(可没有) 结果2
when 条件 then 结果1 else(可没有) 结果2
when 条件 then 结果1 else(可没有) 结果2
......end
2.解析:case when经常和聚合函数配合使用
考察点:count(),sum();
1):sum()配合使用,满足条件返回1,表示进行+1 else 0表示+0
2):count()配合使用,返回的值只要不是null,就进行一次计数,所以使用count()必须注意:若满足条件想进行一次计数,不满足条件不计数
3.本题解法:
elect match_date '比赛日期',
sum(case when result='胜' then 1 else 0 end)'胜',
sum(case when result='负' then 1 else 0 end)'负'
from match_record group by match_date
或者:
elect match_date '比赛日期',
count(case when result='胜' then 1 end)'胜',
count(case when result='负' then 1 end)'负'
from match_record group by match_date