zoukankan      html  css  js  c++  java
  • 常见面试SQL问题

    一、表内容

    Tdate

    Tresulte
    2019/5/9
    2019/5/9
    2019/5/9
    2019/5/9
    2019/5/10
    2019/5/10
    2019/5/10

    结果要生成如下

    日期
    2019/5/9 2 2
    2019/5/10 1 2

    参考答案

    考虑的是行列转换的问题,使用case when解决

    create table tmp(Tdate varchar(10), Tresulte nchar(1));
    insert into tmp values('2019-05-09','胜');
    insert into tmp values('2019-05-09','胜');
    insert into tmp values('2019-05-09','负');
    insert into tmp values('2019-05-09','负');
    insert into tmp values('2019-05-10','胜');
    insert into tmp values('2019-05-10','负');
    insert into tmp values('2019-05-10','负');
    方法一
    select Tdate '日期',
    sum(case when Tresulte = '胜' then 1 else 0 end) '胜',
    sum(case when Tresulte = '负' then 1 else 0 end) '负',
    from tmp group by Tdate;
    方法二
    select N.Tdate '日期', N,胜, M,负
    from (
    select Tdate, count(*) '胜' from tmp where
    Tresulte = '胜' group by Tdate ) N
    inner join
    (select Tdate, count(*) '负' from tmp where
    Tresulte = '负' group by Tdate ) M
    on N.Tdate = M.Tdate;
    二、表中有A B C 三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列

    参考答案

    select
    (case when a>b then a else b end ),
    (case when b>c then b else c end)
    from table_name
    三、请取出tb_send表中日期(SendTime字段)为当天的所有记录。(SendTime字段为datetime型,包含日期和时间)

    参考答案
    select * from tb_send where datediff (dd,SendTime, getdate()) =0
    四、有一张表table,里面包含三个字段:语文、数学、英语。其中有三条记录

    语文 数学 英语
    70 80 58

    请用一条sql语句查询出这三条记录并按照以下条件显示出来:大于或等于80为优秀,大于或等于60为及格,小于60为不及格。

    显示格式

    语文 数学 英语
    及格 优秀 不及格

    参考答案
    select
    (case when '语文'>=80 then '优秀'
          when '语文'>=60 then '及格'
          else '不及格') as '语文',
    (case when '数学'>=80 then '优秀'
          when '数学'>=60 then '及格'
          else '不及格') as '数学',
    (case when '英语'>=80 then '优秀'
          when '英语'>=60 then '及格'
          else '不及格') as '英语',
    from table

    五、从table1,table2中取出如table3所列格式数据,其中table1只展示了部分数据,还有4到12月份的数据未完全展示,table3也只展示了部分数据,还有4到12月份的数据未完全展示。

    table1  

    月份mon 部门dep 业绩yj
    1 01 10
    1 02 10
    2 03 5
    2 02 8
    3 04 9
    3 03 8

    table2

    部门dep 部门名称dname
    01 国内业务一部
    02 国内业务二部
    03 国内业务三部
    04 国内业务部

    结果如下:

    table3

    部门dep 一月份 二月份 三月份
    01 10 null null
    02 10 8 null
    03 null 5 8
    04 null null 9

    参考答案

    select a.dep
    sum(case when b.mon=1 then b.yj else 0 end ) as '一月份',
    sum(case when b.mon=2 then b.yj else 0 end ) as '二月份',
    sum(case when b.mon=3 then b.yj else 0 end ) as '三月份',
    sum(case when b.mon=4 then b.yj else 0 end ) as '四月份',
    sum(case when b.mon=5 then b.yj else 0 end ) as '五月份',
    sum(case when b.mon=6 then b.yj else 0 end ) as '六月份',
    sum(case when b.mon=7 then b.yj else 0 end ) as '七月份',
    sum(case when b.mon=8 then b.yj else 0 end ) as '八月份',
    sum(case when b.mon=9 then b.yj else 0 end ) as '九月份',
    sum(case when b.mon=10 then b.yj else 0 end ) as '十月份',
    sum(case when b.mon=11 then b.yj else 0 end ) as '十一月份',
    sum(case when b.mon=12 then b.yj else 0 end ) as '十二月份'
    from table2 a left join table1 b on a.dep=b.dep

    六、一个表中的id有多少个记录,把所有这个id的记录查出来,并显示共有多少条记录数。

    参考答案:

    --方法一

    select id, count(*)
    from tb
    group by id
    having count(*)>1;
    --方法二:
    select *
      from (select count(ID) as count from table group by ID) T
     where T.count > 1

    七、表形式如下:

    Year Salary
    2010 1000
    2011 2000
    2012 3000
    2013 4000

    想得到如下形式的查询结果

    Year Salary
    2010 1000
    2011 3000
    2012 6000
    2013 10000

    参考答案:

    --连接查询
    select b.Tyear, sum(a.salary) salary
      from hello a, hello b
     where a.Year <= b.Year
     group by b.Year

    --子查询
    select year, (select sum(salary)
                  from hello as B
                  where B.year <= A.year)
      from hello as A

  • 相关阅读:
    201871010115 马北 《面向对象程序设计(java)》 第67周学习总结
    201871010115马北《面向对象程序设计(java)》第一周学习总结
    《面向对象程序设计(Java)》第四周学习总结
    201871010115——马北《面向对象程序设计JAVA》第二周学习总结
    第十一周作业
    《面向对象程序设计(java)》第十周学习总结
    201871010115 马北 第八周作业
    FPGA开发全攻略——FPGA发展以及赛灵思系列产品
    嵌入式系统词汇表
    FPGA开发全攻略——FPGA内部结构
  • 原文地址:https://www.cnblogs.com/john4415/p/11897433.html
Copyright © 2011-2022 走看看