我正在尝试创建一个包含多个“With”语句的函数。
我原来的查询是:
WITH EmpCount as
(
SELECT job.EmployeeID, assign.PropertyID FROM EmployeeJobstatus job
JOIN Assignment assign ON job.JobID = assign.ID GROUP by job.EmployeeID,
assign.PropertyID
),
NoDup as
(
SELECT EmployeeID, Count(employeeID) as NO from EmpCount
Group by EmployeeID HAVING count(Employeeid) > 1
)
SELECT prop.Name, job.EmployeeID, Emp.EmpID, Emp.Name
from EmployeeJobStatus job
JOIN Assignment assign ON assign.id = job.jobid
JOIN Property prop ON prop.ID = assign.PropertyID
JOIN Employee emp on emp.ID = job.EmployeeID
WHERE EmployeeID IN (SELECT EmployeeID from NoDup)
GROUP By prop.Name, EmployeeID, emp.EmpID,Emp.Name
Order BY EmployeeID
这将返回:
Name EmployeeID EmpID Name
Property 1 23 1286333 LastNameRemoved1, Rachel A
Property 2 23 1286333 LastNameRemoved1, Rachel A
Property 2 76 1268329 LastNameRemoved2, Tamer A
Property 1 76 1268329 LastNameRemoved2, Tamer A
Property 3 135 1411933 LastNameRemoved3, Sarah E
Property 1 135 1411933 LastNameRemoved2, Sarah E
我的函数需要使用 EmployeeJobStatus StartDate 和 EndDate 中的字段,根据它们是否是交叉属性来返回“Y”或“N”。
我才刚刚开始我的功能,因为我不知道下一步该做什么。
CREATE function dbo.IsEmployeeCrossPropertyOnDate
(@EmpID int, @AsOfDate datetime)
RETURNS INT AS
任何帮助,或朝着正确方向轻推将不胜感激。谢谢你。