如何计算每个月现有员工的数量?

时间:2014-10-06 13:03:14

标签: php sql postgresql postgis postgresql-9.2

我正在寻找SQL来计算那个特定年份每个月工作的员工数量。意味着结果应该是2013年QA部门的结果:

dept_name   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sept   Oct   Nov   Dec
QA          ( **count of working employees in each month** )

表员工:

id    dept_id    emp_name    date_started     date_terminated
1     3          Amstrong    01-Apr-13        null
2     4          Blowing     03-Jun-13        null
3     3          Jerry       09-Sept-13       01-May-14
4     3          Smack       04-Jun-12        null
5     4          Zuckus      13-Aug-12        13-Dec-13
6     4          Kolman      19-Nov-12        null
7     3          Qual        15-Oct-12        02-Nov-13
8     4          John        11-Jul-11        06-Apr-13
9     4          Delta       28-Jan-14        null
10    3          Panther     17-Jul-14        null
11    3          Amay        01-Apr-13        null
12    4          Blue        03-Jun-13        null
13    3          Jack        09-Sept-13       01-May-14
14    3          Stack       04-Jun-12        null
15    4          Jasus       13-Aug-12        13-Dec-13

表部门:

id      dept_name
1       IT
2       Support
3       QA
4       Development
5       Admin

查询我试图获得结果:

SELECT
    d.dept_name,
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 1 THEN e.id END) as "Jan",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 2 THEN e.id END) as "Feb",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 3 THEN e.id END) as "Mar",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 4 THEN e.id END) as "Apr",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 5 THEN e.id END) as "May",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 6 THEN e.id END) as "Jun",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 7 THEN e.id END) as "Jul",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 8 THEN e.id END) as "Aug",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 8 THEN e.id END) as "Sep",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 10 THEN e.id END) as "Oct",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 11 THEN e.id END) as "Nov",
    COUNT (CASE WHEN EXTRACT(MONTH FROM e.date_started) = 12 THEN e.id END) as "Dec"
FROM department d
    JOIN employee e ON (d.id = e.dept_id)
WHERE EXTRACT(month FROM e.date_started) BETWEEN 01 AND 12
    AND EXTRACT(year FROM e.date_started) < 2014
    AND (e.date_terminated IS NULL OR EXTRACT(month FROM e.date_terminated) > EXTRACT(month FROM NOW())) 
    AND (e.date_terminated IS NULL OR EXTRACT(year FROM e.date_terminated) >= EXTRACT(year FROM NOW()))
GROUP BY d.name
ORDER BY d.name ASC 

但它只考虑那个开始在那个月工作的员工

3 个答案:

答案 0 :(得分:1)

你的桌子上应该有一个像LOG一样的列然后你可以真正检查一个月内谁在工作,目前无法检查谁在哪个月工作你只能检查有多少雇员开始工作特定月份或在一个月内有多少员工结束工作。

但那就是查询应该只在WHERE中查看,你必须添加一个新员工列来存储当前员工的工作日志......

SELECT

COUNT(employee.id) as 'Number of EMployees',
department.dept_name as 'Department name'

FROM employee

LEFT JOIN department on employee.dept_id=department.id

WHERE 

employee.dateLog > 2013-01-01
employee.dateLog < 2013-02-01

答案 1 :(得分:1)

您可以在每个月生成一个日期,并查找在该日期工作的员工数量。格式每个部门每月有一行。在单独的操作中透视结果,可能最好在应用层完成:

with months as (
      select generate_series('2013-01-01'::timestamp, '2013-12-01'::timestamp, '1 month') as m
     ),
select d.dept_name, m.m, count(e.dept_id) as numemployees
from department d cross join
     months m left join
     employee e
     on e.dept_id = d.id and e.date_started <= m.m and
        (e.date_terminated is null or e.date_terminated >= m.m)
group by d.dept_name, m.m
order by d.dept_name, m.m;

答案 2 :(得分:1)

这是另一种方法,这段代码将为2013年的每个部门提供Jan和feb员工人数。这里是sql fiddle http://sqlfiddle.com/#!2/4a861f/14

 SELECT a.dept_name, coalesce(Jan.total,0) as Jan, coalesce(Feb.total,0) as Feb
FROM department as a
LEFT JOIN 
(SELECT dept_id, COUNT(emp_name) as total from employee
where date_started <= '2013-01-31 00:00:00'
and (date_terminated >= '2013-01-01 00:00:00' 
  or date_terminated is null) group by dept_id) as Jan
ON Jan.dept_id = a.id   
LEFT JOIN 
(SELECT dept_id, COUNT(emp_name) as total from employee
where date_started <= '2013-02-31 00:00:00'
and (date_terminated >= '2013-02-01 00:00:00' 
  or date_terminated is null)  group by dept_id) as Feb
ON Feb.dept_id = a.id