查询人数最多的部门及各部门男女人数

首先是建表语句:

spool employee.log

prompt
prompt Creating table EMPLOYEE
prompt =======================
prompt
create table JCXT.EMPLOYEE
(
  id   VARCHAR2(20),
  name VARCHAR2(20),
  sex  VARCHAR2(20),
  dep  VARCHAR2(20)
)
tablespace ZXGL
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );


spool off

插入数据:

insert into employee values('1','李一','女','产品');
insert into employee values('2','李二','男','销售');
insert into employee values('3','李三','女','开发');
insert into employee values('4','李四','男','开发');
insert into employee values('5','李五','男','产品');
insert into employee values('6','李六','女','开发');
insert into employee values('7','李七','女','销售');

查询表:

select * from employee;

效果如下:

 查询人数最多的部门:

select * from (select dep,count(name) renshu from employee group by dep order by renshu desc) where rownum=1;

查询结果如下:

 查询各部门男女数:

select dep 部门,
       count(name) 总人数,
       sum(case
             when sex = '男' then
              1
             else
              0
           end) 男生人数,
       sum(case
             when sex = '女' then
              1
             else
              0
           end) 女生人数
  from employee
 group by dep

查询结果如下:

以上仅为本人工作中的总结,如有雷同,纯属巧合。如有写得不妥地方,欢迎指出

猜你喜欢

转载自blog.csdn.net/Java_15707951907/article/details/89236184