简体   繁体   English

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

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

I am trying to create a scalar function that will return an employee number if a position ( jobcode here) is filled, or a NULL value if it's vacant ( A ctive or T erminated status).我正在尝试创建一个标量 function 如果填充了position (此处为工作代码),或者NULL值(如果终止状态)将返回员工编号。

数据预览

I happen to know that the position called manager (10061) is vacant at store 517. The same position is filled at 536. I'm accomplishing this with a CTE, which is preventing me from using WHERE EXISTS , my first attempt at solving this.我碰巧知道名为 manager (10061) 的 position 在商店 517 处空置。相同的 position 在 536 处填充。我正在使用 CTE 完成此操作,这使我无法使用WHERE EXISTS ,这是我第一次尝试解决此问题.

The closest I'm getting is this:我得到的最接近的是:

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

And here is some sample data if you want to play along at home:如果您想在家玩,这里有一些示例数据:

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); 

Ideally the function's output would be理想情况下,函数的 output 将是

Store   |   Jobcode |   EmpNo
 536        10061       12558412
 517        10061       NULL

Any help is so greatly appreciated.非常感谢任何帮助。

To get the information that you look for, you just need to look at the record with the most recent LastHireDate for each store and jobcode .要获取您要查找的信息,您只需查看每个storejobcode的最新LastHireDate的记录。 If this record has status 'A' , the position is staffed, else it is vacant.如果此记录的状态为'A' ,则 position 有人值班,否则它是空的。

I would suggest to use a correlated subquery for filtering instead of row_number() .我建议使用相关子查询而不是row_number()进行过滤。 In this situation this can be an efficicent solution, especially with an index on (store, jobcode, LastHireDate) .在这种情况下,这可能是一个有效的解决方案,尤其是在(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 : DB Fiddle 上的演示

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

Here is a scalar function to do that:这是一个标量 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