sql面试 case /union all

1、sum(case when results='胜' then 1 else 0 end) as ‘胜’

要求查询出结果:

sql语句实现:

select date,
	sum(case when results='胜' then 1 else 0 end) AS '胜',
	sum(case when results='负' then 1 else 0 end) AS '负'
from game
group by date;

2、成绩分等级(case when 条件 then 结果 when 条件 then 结果 else 结果 end)

请写出生成如下图结果的sql语句( >=80 表示优秀, >=60 且 <80表示及格,<60表示不及格)?

效果图:

sql语句实现:

select 
	(case when `语文` >= 80 then '优秀' when `语文` >= 60 then '及格' else '不及格' end) AS '语文',
	(case when `数学` >= 80 then '优秀' when `数学` >= 60 then '及格' else '不及格' end) AS '数学',
  (case when `英语` >= 80 then '优秀' when `英语` >= 60 then '及格' else '不及格' end) AS '英语'
from score

3、合并字段显示(union all)

合并显示按投资额度倒序排序,结果如下:

sql语句实现

select 
	id AS '编号',
	'db_gain' AS '表名',
	'' as '连接',
	invest_money AS '投资额度',
	project_name as '项目名称',
	belongs_inductry as '产业领域',
	coperation as '合作方式'
from db_gain
UNION ALL
select 
	id AS '编号',
	'achievement' as '表名',
	url as '连接',
	price as '投资额度',
	name as '项目名称',
	category as '产业领域',
	tradeType as '合作方式'
from achievement
order by `投资额度` desc;
posted @ 2020-08-21 22:40  一座塔一盏灯  阅读(327)  评论(0编辑  收藏  举报