如何通过HireDate和TerminationDate获得公司最大员工?

时间:2018-02-05 02:05:40

标签: sql postgresql

我是新来的。所以我需要一个帮助我的PostgreSQL查询。我有一个名为Employee的表,如:

ID | FirstName | LastName | HireDate   | TerminationDate | Salary
---+-----------+----------+------------+-----------------+-------
1  | Peter     | Jones    | 2010-06-20 | 2016-01-01      | 1000
2  | Eva       | Johnson  | 2010-02-12 | NULL            | 2000
3  | Ryan      | Kent     | 2012-03-14 | 2014-01-01      | 2000
4  | Jack      | River    | 2013-09-10 | NULL            | 3000
5  | Thomas    | Lex      | 2013-09-10 | NULL            | 2000
6  | Gerald    | Smith    | 2015-04-10 | 2015-05-01      | 4000

如何获取员工的所有信息,我必须添加另一列,显示公司在服务期间雇用的最大员工人数,以及获得最大员工人数的日期?

表格应如下所示

ID | FirstName | LastName | HireDate   | TerminationDate | Salary | MaxEmp | DateAcq
---+-----------+----------+------------+-----------------+--------+--------+-----------
1  | Peter     | Jones    | 2010-06-20 | 2016-01-01      | 1000   | 5      | 2015-04-10
2  | Eva       | Johnson  | 2010-02-12 | NULL            | 2000   | 5      | 2015-04-10
3  | Ryan      | Kent     | 2012-03-14 | 2014-01-01      | 2000   | 5      | 2013-09-10
4  | Jack      | River    | 2013-09-10 | NULL            | 3000   | 5      | 2015-04-10
5  | Thomas    | Lex      | 2013-09-10 | NULL            | 2000   | 5      | 2015-04-10
6  | Gerald    | Smith    | 2015-04-10 | 2015-05-01      | 4000   | 5      | 2015-04-10

如果MaxEmp是公司在其服务期间雇用的最大员工数,则DateAcq是获取最大数量的日期,而TerminationDate中的NULL记录显示该员工仍然在该公司工作。 / p>

1 个答案:

答案 0 :(得分:0)

此查询显示了一段时间内就业的变化:

select sum(count) over (order by dateaq) as maxemp, dateaq
from (
    select hiredate as dateaq, 1 as count
    from employee
    union all
    select terminationdate, -1
    from employee
    where terminationdate is not null
    ) s;

 maxemp |   dateaq   
--------+------------
      1 | 2010-02-12
      2 | 2010-06-20
      3 | 2012-03-14
      5 | 2013-09-10
      5 | 2013-09-10
      4 | 2014-01-01
      5 | 2015-04-10
      4 | 2015-05-01
      3 | 2016-01-01
(9 rows)

如您所见,最大就业人数为5人,因为Ryan和Gerald没有同时就业。

将这些结果与表格结合起来,并为每位员工选择最大值:

select distinct on (id) *
from employee e
join (
    select sum(count) over (order by dateaq) as maxemp, dateaq
    from (
        select hiredate as dateaq, 1 as count
        from employee
        union all
        select terminationdate, -1
        from employee
        where terminationdate is not null
        ) s
    ) s on dateaq between hiredate and coalesce(terminationdate, 'infinity')
order by id, maxemp desc, dateaq desc;

 id | firstname | lastname |  hiredate  | terminationdate | salary | maxemp |   dateaq   
----+-----------+----------+------------+-----------------+--------+--------+------------
  1 | Peter     | Jones    | 2010-06-20 | 2016-01-01      |   1000 |      5 | 2015-04-10
  2 | Eva       | Johnson  | 2010-02-12 |                 |   2000 |      5 | 2015-04-10
  3 | Ryan      | Kent     | 2012-03-14 | 2014-01-01      |   2000 |      5 | 2013-09-10
  4 | Jack      | River    | 2013-09-10 |                 |   3000 |      5 | 2015-04-10
  5 | Thomas    | Lex      | 2013-09-10 |                 |   2000 |      5 | 2015-04-10
  6 | Gerald    | Smith    | 2015-04-10 | 2015-05-01      |   4000 |      5 | 2015-04-10
(6 rows)