记一道群友的数据分析面试真题,你会吗?

 近期在群里面看到了如下这样一个面试题目,这个题目其实难度不大,但是你是否能够很快写出这个答案来呢?

记一道群友的数据分析面试真题,你会吗?_第1张图片

本文解答方案由群友老黄提供

老黄博客:
https://blog.csdn.net/weixin_41261833/article/details/105739549?utm_source=app

建表语句

create table student (
      id varchar(20),
      name varchar(20),
      gender char(1),
      birth varchar(20),
      department varchar(20),
      address varchar(20)
) charset = utf8;

插入数据

insert into student values
("201901","张大佬","男","1985","计算机系","北京市海淀区"),
("201902","郭大侠","男","1986","中文系","北京市昌平区"),
("201903","张三","女","1990","中文系","湖南省永州市"),
("201904","李四","男","1990","英语系","辽宁市阜新市"),
("201905","王五","女","1991","英语系","福建省厦门市"),
("201906","王六","男","1988","计算机系","湖南省衡阳市");

结果如下

记一道群友的数据分析面试真题,你会吗?_第2张图片

第一步

select 
    department 院系,
    case gender when "男" then 1 else 0 end 男,
    case gender when "女" then 1 else 0 end 女
from student;

结果如下

记一道群友的数据分析面试真题,你会吗?_第3张图片

第二步

select 
      院系,
      sum(男) 男,
      sum(女) 女,
      sum(男) + sum(女) as 总计
from
(
    select department 院系,
    case gender when "男" then 1 else 0 end 男,
    case gender when "女" then 1 else 0 end 女
    from student
) a
group by 院系;

结果如下

记一道群友的数据分析面试真题,你会吗?_第4张图片

怎么样,学会了吗?欢迎大家加群交流数分~

日常答疑|Python实现分类频数统计

日常答疑|Python处理时间格式并计算时间差值

你可能感兴趣的:(记一道群友的数据分析面试真题,你会吗?)