WHEN'日本'THEN'亚洲' WHEN'美国'THEN'北美洲' WHEN'加拿大'THEN'北美洲' WHEN'墨西哥'THEN'北美洲' ELSE'其他'END FROM Table_A GROUP BY CASE country WHEN'中国'THEN'亚洲' WHEN'印度'THEN'亚洲' WHEN'日本'THEN'亚洲' WHEN'美国'THEN'北美洲' WHEN'加拿大'THEN'北美洲' WHEN'墨西哥'THEN'北美洲' ...
GROUP BY CASE c.country WHEN '中国' THEN '亚洲' WHEN '印度' THEN '亚洲' WHEN '日本' THEN '亚洲' WHEN '美国' THEN '北美洲' WHEN '加拿大' THEN '北美洲' WHEN '墨西哥' THEN '北美洲' ELSE '其他' END; 运行结果:
else null end asGrade fromPayrollgroup by case when salary>=0 and salary<200 then '等级1' when salary>=200 and salary<400 then '等级2' when salary>=400 and salary<600 then '等级3' when salary>=600 and salary<=800 then '等级4' else null end; 二,用一个sql语句完成不同条件的分组 ...
WHEN s.s_sex = '1' THEN '男'WHEN s.s_sex = '2' THEN '女'ELSE '其他'END as sex,s.s_age,s.class_id FROM t_b_student s WHERE 1 = 1 2、第二种写法 SELECT s.s_id,s.s_name,s.s_sex,CASE s.s_sex WHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他'END ...
WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END as sex, s.s_age, s.class_id FROM student s 也可以这样写,CASE后不加字段,在 WHEN的时候指定字段名! SELECT s.s_id, s.s_name, s.s_sex, CASE WHEN s.s_sex = '1' THEN '男' ...
then 1 else 0 end) as "fsalary<9999_fage>35", sum(case when fsalary < 9999 and fage < 35 then 1 else 0 end) as "fsalary<9999_fage<35" from empinfo; 解释: SELECT case ---如果 when sex='1' then '男' ---sex='1',则返回值'男' when sex='2' then '女' --...
CASE WHEN status = '已完成' THEN 'Completed' WHEN status = '已提交' THEN 'Submitted' ELSE 'Pending' END AS order_status FROM orders; ``` 上述语句将返回一个包含订单状态和对应描述的结果集。 有时需要根据更复杂的条件进行判断,这时就需要使用嵌套的 CASE WHEN THEN ELSE END 语句。嵌套的 CASE ...
通过观察我们知道,case when语句是将原来在where后的条件移到了case when后面,在逻辑上,都是满足score>=80 AND score<90这个条件的人才会被统计为B等级。 既然我们知道等级为B的肯定score<90,因此我们再来看以下这个sql: SELECT COUNT(CASE WHEN score>=80 THEN `name` ELSE NULL END) AS B ...
select sum(case when fsalary > 9999 and fage > 35 then 1 else 0end) as "fsalary>9999_fage>35", sum(case when fsalary > 9999 and fage < 35 then 1 else 0 end) as "fsalary>9999_fage<35", sum(case when fsalary < 9999 and fage > 35 ...