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;
    
  • 相关阅读:
    windows中dos命令指南
    HDU 2084 数塔 (dp)
    HDU 1176 免费馅饼 (dp)
    HDU 1004 Let the Balloon Rise (map)
    变态杀人狂 (数学)
    HDU 2717 Catch That Cow (深搜)
    HDU 1234 开门人和关门人 (模拟)
    HDU 1070 Milk (模拟)
    HDU 1175 连连看 (深搜+剪枝)
    HDU 1159 Common Subsequence (dp)
  • 原文地址:https://www.cnblogs.com/zhouyongyin/p/13543737.html
Copyright © 2011-2022 走看看