简体   繁体   English

根据当前时间点的部门对员工历史表进行不同计数

[英]Doing a distinct count on an employee history table, based on departments at a current point in time

So I have an employee table with data on all employee since the beginning.所以我有一个员工表,其中包含从一开始就所有员工的数据。 In the data I have all the data I should need.在数据中,我拥有我需要的所有数据。 I have the employee startdate, enddate (null if nothing), I have the name of the department, and if a department has changed, that specific employee has a new line, with a new department value, and two columns called "DepValidFrom" and "DepValidto", in date format that determines the time-period that the current employee was in that specific department.我有员工的开始日期、结束日期(如果没有则为 null),我有部门的名称,如果部门发生变化,该特定员工有一个新行,有一个新的部门值,以及两列名为“DepValidFrom”和“DepValidto”,采用日期格式,确定当前员工在该特定部门的时间段。

My goal is, to get into a matrix, a list of all the departments as rows, and with year and month as columns, and the number of employees in that department at that time as values.我的目标是,进入一个矩阵,所有部门的列表作为行,年和月作为列,以及当时该部门的员工人数作为值。 I have all the data, I just cannot find the exact way to write my PowerBI Measure or perhaps even SQL query.我拥有所有数据,只是找不到编写 PowerBI Measure 或什至 SQL 查询的确切方法。

So.... I am trying to pull this into Power BI, and I am getting an incomplete view.所以....我正在尝试将其放入 Power BI,但我得到的是一个不完整的视图。 I want my data to look like the following:我希望我的数据如下所示:

Department | Jan | Feb | Mar | Apr |
Dep1       |  3  |  5  |  6  |  4  |
Dep2       |  2  |  3  |  2  |  3  |
Dep3       |  1  |  1  |  2  |  3  |

Right now I am just using a very simple DISTINCTCOUNT(Emp_Table[EmployeeInitials]) which gives me an incomplete view, as it only counts on the specific date, and doesn't retain the number into a total, leaving a bunch of empty values.现在我只是使用一个非常简单的DISTINCTCOUNT(Emp_Table[EmployeeInitials]) ,它给了我一个不完整的视图,因为它只计算特定日期,并且不将数字保留为总数,留下一堆空值。

I hope someone can understand what I mean, and that someone can help!我希望有人能理解我的意思,并且有人可以提供帮助!

Thanks!谢谢!

You can start by unpivoting the dates and generating a query that gives the number of employee per department and date:您可以首先逆透视日期并生成一个查询,该查询给出每个部门和日期的员工人数:

select e.dept, x.dt, sum(cnt) over(partition by dept order by dt) cnt
from employees e
cross apply (values (startdate, 1), (enddate, -1)) as x(dt, cnt)
where dt is not null

Then, you can do conditional aggregation to pivot the results - this requires enumerating the dates though:然后,您可以对结果进行条件聚合 pivot - 这需要枚举日期:

select dept, 
    max(case when dt >= '20200101' and dt < '20200201' then cnt else 0 end) cnt_202001,
    max(case when dt >= '20200201' and dt < '20200301' then cnt else 0 end) cnt_202002,
    ...
from (
    select e.dept, x.dt, sum(cnt) over(partition by dept order by dt) cnt
    from employees e
    cross apply (values (startdate, 1), (enddate, -1)) as x(dt, cnt)
    where dt is not null
) t
group by dept

When an employee changes in the middle of the month, it is counted in both departments for that month.当一名员工在月中发生变动时,该月的两个部门都会对其进行计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我现在有一个包含不同部门的示例表,该场景需要在所有列的表中显示重复率最高的部门 - I have a sample table with distinct departments now the scenario is,need to display the highest repeated departments in a table with all columns 查询以获取不同部门的员工 - Query to get employee of different departments 未找到:在美国位置未找到表 project-id:employee_data.departments - Not found: Table project-id:employee_data.departments was not found in location US 如何创建视图表以显示同一员工的当前记录数和总数? 甲骨文 - How do i create a view table to show current number of record of the same employee and the total count? SQL ORACLE 如何计算当月的员工读数 - How to count employee readings in current month SQL:基于多对多表的条件下的不同用户数 - SQL: count of distinct users with conditions based on many to many table 根据从联接评估的条件对mysql表中的字段进行计数 - Doing a count on a field in a mysql table based on a condition evaluated from a join 如何获得所有带员工号的部门 - How to get all departments with Employee number 查询两个或多个部门的员工 - Query an employee from two or more departments 最高薪员工+各部门平均薪资 - Highest paid employee + average salary of the departments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM