我有一个具有以下6列的表:
员工,雇员,CurrentStatus,位置,Hiredate,TerminationDate
EmployeeId EmployeeName CurrentStatus, Location, HireDate, TerminationDate
2001 Peter Active London 1/1/2000
2002 Jim Terminated Manchester 2/1/2016 7/1/2019
我需要以以下格式使用SQL创建报告:
Location Week End Date No of Employees Terminated
London 1/12/2019 0
Manchester 1/12/2019 1
我还有一些要求:
- 自2016年3月1日起,我需要获取数据。
- 周末日期应该是星期六。
- 计数应该是该周终止的员工人数。
请提供有关如何完成此操作的指导。
hmmm。。。这可以做你想要的吗?
with weekends as (
select cast('2019-01-12' as date) weekenddate
)
select l.location, we.weekenddate,
count(t.employeeid) as num_terminated
from weekends we cross join
(select distinct location from t) l left join
t
on t.location = l.location and
t.TerminationDate <= we.weekenddate and
t.TerminationDate >= dateadd(day -6, we.weekenddate)
group by l.location, we.weekenddate
order by location, weekenddate;
这产生了位置和周末的所有组合。然后在每个日期对每个位置进行计数。只需填写您想要的几周。