人事系统要查询某个时刻的部门人员数,有员工表,离职表,调动表请帅哥美女给个大概的解决方案某刻,SQL

解决方案 »

  1.   

    调动有调离,调入之分么?
    select count(*) from 员工表
    where id not in
    (
    select id from 离职表 where 离职时间 > 给定时间
    union all
    select id from 调动表 where 调动时间 > 给定时间
    )
    and 入职时间 <= 给定时间
    --如果算上调入
    (
    or id in (select id from 调动表 where 调入部门 = 部门 and 调入时间  < 给定时间)
    )
      

  2.   

    这个SQL的写法跟表的设计结构有直接关系,我一般这样设计,员工表是一个总表,离职员工只是它的状态为已离职,调动表话可以通过当前部门来限定就Ok了。下面是我的SQL:--
    select count(*) from 员工表
    where 入职时间<=‘入职时间’ and 部门=‘部门名’ and state='在职' ;如果非要按三张表来做,可以尝试一下:
    select count(employeeid) from (
    select employeeid from 员工表
    where 部门=‘部门名’ and 入职时间<=‘给定时间’
    minus
    select employeeid from 离职表
    where 部门=‘部门名’and 离职时间<=‘给定时间’
    union
    select employeeid from 调动表
    where 调入部门=‘部门名’ and 调动时间<=‘给定时间’
    minux 
    select employeeid from 调动表
    where 调出部门=‘部门名’ and 调动时间<=‘给定时间’
    );
    希望能对你有所帮助~