zoukankan      html  css  js  c++  java
  • 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;
    
  • 相关阅读:
    【NOIP2016】换教室
    【NOIP模拟赛】总结
    【Codeforces Round 418】An impassioned circulation of affection DP
    DP测试总结
    【NOIP2012】疫情控制
    【HNOI2016】序列 莫队+单调栈+RMQ
    【Luogu P2709 小B的询问】莫队
    【HNOI2017】影魔
    【HNOI2017】大佬
    阿里云MaxCompute 2019-7月刊
  • 原文地址:https://www.cnblogs.com/zhouyongyin/p/13543737.html
Copyright © 2011-2022 走看看