我有一个包含以下6列的表格:
EmployeeId,EmployeeName,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
我还有其他一些要求:
请提供有关如何完成此操作的指南。
答案 0 :(得分:0)
嗯。 。 。这是您想要的吗?
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;
这将生成位置和周末的所有组合。然后在每个日期对每个位置进行计数。只需填写CTE所需的周数即可。