繁体   English   中英

SQL 服务器 Function 返回一个值或 NULL 如果没有结果

[英]SQL Server Function to return a value or a NULL if no results

我正在尝试创建一个标量 function 如果填充了position (此处为工作代码),或者NULL值(如果终止状态)将返回员工编号。

数据预览

我碰巧知道名为 manager (10061) 的 position 在商店 517 处空置。相同的 position 在 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); 

理想情况下,函数的 output 将是

Store   |   Jobcode |   EmpNo
 536        10061       12558412
 517        10061       NULL

非常感谢任何帮助。

要获取您要查找的信息,您只需查看每个storejobcode的最新LastHireDate的记录。 如果此记录的状态为'A' ,则 position 有人值班,否则它是空的。

我建议使用相关子查询而不是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

DB Fiddle 上的演示

store | jobcode | employeeName
----: | ------: | :-----------
  119 |   10721 | null        
  517 |   10061 | null        
  536 |   10061 | John S.     

这是一个标量 function 来做到这一点:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM