我正在尝试创建一个标量函数,如果填补了职位(此处为 jobcode ),则将返回员工编号;如果职位为空( A ,则返回NULL值) ctive或 T 终止状态)。
我碰巧知道在商店517中称为经理(10061)的职位空缺。在536中填补了同一职位。我正在通过CTE完成此操作,这阻止了我使用WHERE EXISTS
,我第一次尝试解决这个问题。
我最近得到的是:
with cte as (
select store, empno, name, jobcode, jobtitle, status, lasthiredate, terminationdate, row_number() over (partition by jobcode, store order by terminationdate desc) as rn
from #temp
)
Select distinct a.store, b.empno, a.name, a.jobcode, a.jobtitle, a.status, a.rn
from cte a
left outer join cte b on a.jobcode = b.jobcode and a.store = b.store and a.status <> b.status
where a.jobcode = 10061
order by a.store, a.rn
如果您想在家玩,这里是一些示例数据:
create table #temp ( [Store] int, [EmpNo] int, [Name] varchar(11), [Jobcode] int, [Dept] int, [JobTitle] varchar(8), [LastHireDate] date, [Status] varchar(1), [TerminationDate] date);
Insert into #temp values ( 119, 5042105, 'Gary D.', 10721, 10, 'Director', '7/7/2003', 'T', '1/18/2015');
Insert into #temp values ( 119, 5391105, 'Sonia H.', 10721, 10, 'Director', '12/19/2008', 'A', NULL);
Insert into #temp values ( 119, 8155608, 'Paul W.', 10721, 10, 'Director', '3/20/2017', 'T', '11/30/2017');
Insert into #temp values ( 119, 11952311, 'LARRY B.', 10721, 11, 'Director', '4/15/2010', 'T', '3/14/2012');
Insert into #temp values ( 119, 19065019, 'Gary D.', 10721, 10, 'Director', '7/7/2003', 'T', '3/24/2017');
Insert into #temp values ( 119, 19073019, 'Timothy P.', 10721, 10, 'Director', '4/30/2013', 'T', '12/5/2017');
Insert into #temp values ( 119, 27230127, 'Jeffery F.', 10721, 10, 'Director', '1/17/2010', 'T', '12/21/2015');
Insert into #temp values ( 119, 89113289, 'Timothy S.', 10721, 10, 'Director', '8/3/2015', 'T', '5/14/2019');
Insert into #temp values ( 119, 89209289, 'Michael B.', 10721, 10, 'Director', '12/17/2015', 'A', NULL);
Insert into #temp values ( 119, 89453589, 'Harold H.', 10721, 10, 'Director', '2/21/2018', 'T', '5/7/2019');
Insert into #temp values ( 119, 89604489, 'Jason B.', 10721, 10, 'Director', '5/17/2017', 'A', NULL);
Insert into #temp values ( 119, 89931089, 'Jeffery F.', 10721, 10, 'Director', '1/17/2010', 'A', NULL);
Insert into #temp values ( 119, 99371499, 'William A.', 10721, 10, 'Director', '11/2/1998', 'A', NULL);
Insert into #temp values ( 119, 99728099, 'K. Renee H.', 10721, 10, 'Director', '9/11/1989', 'T', '3/24/2017');
Insert into #temp values ( 517, 11263511, 'Michael D.', 10061, 3, 'Manager', '1/19/2015', 'T', '7/27/2015');
Insert into #temp values ( 517, 11544211, 'Richard L.', 10061, 3, 'Manager', '10/10/2005', 'T', '12/14/2014');
Insert into #temp values ( 536, 3507003, 'Jeffrey S.', 10061, 3, 'Manager', '2/18/2002', 'T', '6/8/2012');
Insert into #temp values ( 536, 12558412, 'John S.', 10061, 3, 'Manager', '9/27/2010', 'A', NULL);
理想情况下,函数的输出为
Store | Jobcode | EmpNo
536 10061 12558412
517 10061 NULL
非常感谢您的帮助。
答案 0 :(得分:1)
要获取您需要的信息,您只需要查看每个LastHireDate
和store
的最新jobcode
记录。如果此记录的状态为'A'
,则说明该职位已配备人员,否则职位空缺。
我建议使用相关子查询进行过滤,而不要使用row_number()
。在这种情况下,这可能是一种有效的解决方案,尤其是对于(store, jobcode, LastHireDate)
上的索引。
select store, jobcode, case when status = 'A' then name end employeeName
from #temp t
where t.LastHireDate = (
select max(t1.LastHireDate)
from #temp t1
where t1.store = t.store and t1.jobcode = t.jobcode
)
order by store, jobcode
store | jobcode | employeeName ----: | ------: | :----------- 119 | 10721 | null 517 | 10061 | null 536 | 10061 | John S.
答案 1 :(得分:-1)
这是一个执行此操作的标量函数:
CREATE FUNCTION dbo.CheckAvailability (@jobcode INT, @store INT)
RETURNS INT
AS
BEGIN
DECLARE @return int
SELECT @return = t.jobcode
FROM temp1 AS t
WHERE t.Jobcode = @jobcode
AND t.Store = @store
AND t.Status NOT IN ('A','T')
RETURN (@return)
END