SQL Server函数,如果没有结果,则返回值或NULL

问题描述 投票:0回答:1

我正在尝试创建一个标量函数,如果该职位(此处为jobcode)已满,将返回员工编号;如果该职位为空(A ctive或T,则返回NULL值)终结状态)。

Data Preview

我碰巧知道在商店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); 

非常感谢您的帮助。

sql sql-server common-table-expression
1个回答
0
投票

要获得所需的信息,您只需查看每个LastHireDatestore都具有最新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

Demo on DB Fiddle

商店|工作代码|员工姓名----:| ------:| :-----------119 | 10721 | null517 | 10061 | null536 | 10061 |约翰·S
© www.soinside.com 2019 - 2024. All rights reserved.