【问题标题】:how to get current employees status,resigned status,newly joined status in particular period?如何获取当前员工状态,离职状态,特定时期的新入职状态?
【发布时间】:2017-07-07 22:55:23
【问题描述】:

我在此表中有员工主表,其中包含 empcode、empname、joindate、resigndate,但未提及任何输入主表的日期。 如何获取当前员工、离职员工人数、特定日期期间入职员工人数

例如:

current employees  -200
resigned employees -25
joined employees   -10
current status     -200+10=210-25=185 

这是我想要的表格格式

【问题讨论】:

  • COUNT( CASE WHEN joindate BETWEEN ... )的变体
  • 展示您尝试过的内容,我们可以从那里帮助您,但 SO 不是代码编写服务。还要确定您使用的是 Oracle 还是 SQL Server。这些是完全不同的产品。
  • 您的架构在哪里?我们如何知道它们之间的关系?而且您的问题过于具体,对未来的访问者没有帮助。

标签: sql sql-server oracle11g


【解决方案1】:

如果我们有日历表或日期维度、查询、视图...等,这会变得简单得多。

/* example Calendar, just Months */
create table dbo.Calendar(
    MonthStart date primary key
  , MonthEnd   date
);

declare @FromDate date = '20000101';
declare @ThruDate date = '20301201';

with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, d as (
  select DateValue=convert(date,dateadd(month
      , row_number() over (order by (select 1)) -1, @fromdate))
    from         n as deka
      cross join n as hecto
      cross join n as kilo
)
insert into dbo.Calendar
  select top (datediff(month, @FromDate, @ThruDate)+1)
      MonthStart    = dateadd(month, datediff(month, 0, DateValue), 0)
    , MonthEnd      = dateadd(day,-1, dateadd(month, datediff(month, 0, DateValue)+1, 0))
    from d
    order by DateValue;

使用common table expressionsum(case when.. 1 else 0 end) 进行查询

;with cte as (
select
    c.MonthStart
  , c.MonthEnd
  , e.EmpCode
  , e.JoinDate
  , e.ResignDate
from dbo.Calendar as c
  inner join dbo.EmpMaster as e
    on  c.MonthEnd   >= e.JoinDate
    and c.MonthStart <= e.ResignDate
)
select
    MonthStart
  , MonthEnd
  , CurrentEmployees = sum(
          case when JoinDate   <  MonthStart
               then 1 else 0 end )
  , Joined           = sum(
          case when JoinDate   >= MonthStart and JoinDate   <= MonthEnd
               then 1 else 0 end)
  , Resigned         = sum(
          case when ResignDate >= MonthStart and ResignDate <= MonthEnd
               then 1 else 0 end)
  , CurrentStatus    = sum(
          case when ResignDate > MonthEnd
               then 1 else 0 end)
from cte
group by
    cte.MonthStart
  , cte.MonthEnd
order by cte.MonthStart;

没有 cte,而是使用 count(case when ... then column else null end)

select
    MonthStart
  , MonthEnd
  , CurrentEmployees = count (
      case when JoinDate   <  MonthStart
           then EmpCode else null end )
  , Joined           = count (
      case when JoinDate   >= MonthStart and JoinDate   <= MonthEnd
           then EmpCode else null end )
  , Resigned         = count (
      case when ResignDate >= MonthStart and ResignDate <= MonthEnd
           then EmpCode else null end )
  , CurrentStatus    = count (
      case when ResignDate > MonthEnd
           then EmpCode else null end )
from (
    select
    c.MonthStart
  , c.MonthEnd
  , e.EmpCode
  , e.JoinDate
  , e.ResignDate
from dbo.Calendar as c
  inner join dbo.EmpMaster as e
    on  c.MonthEnd   >= e.JoinDate
    and c.MonthStart <= e.ResignDate
    ) as cte
group by cte.MonthStart, cte.MonthEnd
order by cte.MonthStart

每个都返回相同的结果:http://rextester.com/AEZTL76069

+------------+------------+------------------+--------+----------+---------------+
| MonthStart |  MonthEnd  | CurrentEmployees | Joined | Resigned | CurrentStatus |
+------------+------------+------------------+--------+----------+---------------+
| 2010-01-01 | 2010-01-31 |                0 |      3 |        0 |             3 |
| 2010-02-01 | 2010-02-28 |                3 |      1 |        0 |             4 |
| 2010-03-01 | 2010-03-31 |                4 |      2 |        0 |             6 |
| 2010-04-01 | 2010-04-30 |                6 |      1 |        0 |             7 |
| 2010-05-01 | 2010-05-31 |                7 |      2 |        0 |             9 |
| 2010-06-01 | 2010-06-30 |                9 |      1 |        0 |            10 |
| 2010-07-01 | 2010-07-31 |               10 |      1 |        0 |            11 |
| 2010-08-01 | 2010-08-31 |               11 |      2 |        0 |            13 |
| 2010-09-01 | 2010-09-30 |               13 |      6 |        0 |            19 |
| 2010-10-01 | 2010-10-31 |               19 |      2 |        0 |            21 |
| 2010-11-01 | 2010-11-30 |               21 |      1 |        0 |            22 |
| 2010-12-01 | 2010-12-31 |               22 |      0 |        0 |            22 |
+------------+------------+------------------+--------+----------+---------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多