<pre>
I have 3 tables and one table valued function: EmpHistory,EmpRank,Emp and fnEmpRank.
The sample data are given as follows:
EmpHistory
EmpHistID EmpID RankID MonitorDate RankName
1 aba JPR 2008-10-06 Junior Prof
2 aba JPR 2009-11-07 Junior Prof
3 aba TERM 2012-2-08 Termoinated Prof
4 aba ASST 2012-6-22 lab Assistant
5 aba ASST 2012-7-2 Lab Assistant
6 aba TSST 2012-8-4 Terminated Assistant
EmpRank
RankID RankName
JPR Junior Professor
SPR Senior Professor
ASST Junior Assistant
SASST Senior Assistant
PL Principal
Employee
EmpID EmpStartDate
aba 2008-10-06
abc01 2007-09-23
sdh 2009-7-26
sbs 2012-2-8
The fnEmpRank function takes the emproleID and gives the employee history same as the empHistory Table. There is also empoyeerole table which has employeeroleid column.
Now my problem is: I want the second last professor rank of the employee i.e in this case I want Junior Professor row(i.e) 2nd row from emphistory table). Currntly my code is using emphistory table but now intead of that table I want to use fnEmpRank as it gives the same data. I am also giving the sample code.
select
a.EmpID,
a.StartDate,
J.RankID,
c.MonitorDate,
from dbo.vwEmployee A(nolock)
INNER join dbo.EmpHistory c(nolock) on c.Empid = a.EmpID
and c.EmpHistoryID = (select max(c1.EmpHistoryID)
from dbo.EmpHistory c1(nolock)
where c1.Empid = c.EmpID
and c1.MonitorDate =
(
我有3个表和一个表值函数:EmpHistory,EmpRank,Emp和fnEmpRank。 样本数据如下:
创建表EmpHistory( EmpHistID int, EmpID varchar, RankID varchar, Monitordate日期, Rankname varchar)
插入EmpHistory 选择1,'aba','JPR','2008-10-6','初级教授'
插入EmpHistory 选择2,'aba','JPR','2009-11-7','初级教授'
插入EmpHistory 选择3,'aba','TERM','2012-2-8','终止教授'
插入EmpHistory 选择4,'aba','ASST','2012-6-22','Lab Assistant'
插入EmpHistory 选择5,'aba','ASST','2012-7-2','Lab Assistant'
插入EmpHistory 选择1,'aba','JPR','2012-8-4','终止助理'
创建表EmpRank( RankID varchar, RankName varchar )
插入EmpRank 选择'JPR','初级教授'
插入EmpRank 选择'SPR','高级教授'
插入EmpRank 选择'ASST','初级助理'
插入EmpRank 选择'SASST','高级助理'
插入EmpRank 选择'PL','Principal'
创建表员工( EmpID varchar, EmpStartDate日期 )
插入员工 选择'aba','2008-10-06'
插入员工 选择'abc01','2007-9-23'
插入员工 选择'sdh','2009-7-26'
插入员工 选择'sbs','2012-2-8'
The fnEmpRank function takes the emproleID and gives the employee history same as the empHistory Table. There is also empoyeerole table which has employeeroleid column.
Now my problem is: I want the second last professor rank of the employee i.e in this case I want Junior Professor row(i.e) 2nd row from emphistory table). Currntly my code is using emphistory table but now intead of that table I want to use fnEmpRank as it gives the same data. I am also giving the sample code.
select
a.EmpID,
a.StartDate,
J.RankID,
c.MonitorDate,
from dbo.vwEmployee A(nolock)
INNER join dbo.EmpHistory c(nolock) on c.Empid = a.EmpID
and c.EmpHistoryID = (select max(c1.EmpHistoryID)
from dbo.EmpHistory c1(nolock)
where c1.Empid = c.EmpID
and c1.MonitorDate =
(
SELECT MAX(C2.MonitorDate)
FROM dbo.EmpHistory C2
WHERE C2.EmpID = C1.EmpID
)
)
join dbo.EmpRank d(nolock) on d.RankID = a.RankID
left join dbo.EmpHistory f(nolock) on f.EmpID = a.EmpID
and f.EmpHistoryID = (select max(g.EmpHistoryID)
from dbo.EmpHistory g(nolock)
where g.EmpID = a.EmpID
AND G.RankID not like 'T%'
and g.EmpHistoryID < c.EmpHistoryID)
left join dbo.EmpRank h(nolock) on h.RankID = f.RankID
LEFT JOIN dbo.EmpHistory J(NOLOCK) ON J.EmpID = A.EmpID
AND J.EmpHistoryID = (
SELECT max(K.EmpHistoryID )
FROM dbo.EmpHistory K(NOLOCK)
WHERE K.EmpID = J.EmpID
AND K.AgentRankID NOT LIKE 'T%'
AND K.MonitorDate = (
SELECT max(M.MonitorDate )
FROM dbo.EmpHistory M(NOLOCK)
WHERE M.EmpID = J.EmpID
AND M.RankID NOT LIKE 'T%'
)
)
where
A.Prof=1
c.RankID like 'T%'
AND c.RankID <>'TSST'
AND A.StartDate is not null
Here there is one more problem: Even if the Employee is terminated from professor to Assitant, A.Prof values is still 1 and basically Assistant dont have the start dates but when professor are transformed to Assitant, they still contain the start date. How can I handle this in the code. Basically this code assumes that that if emp has the start date then he is the professor. Can any one help me?
SELECT MAX(C2.MonitorDate)
FROM dbo.EmpHistory C2
WHERE C2.EmpID = C1.EmpID
)
)
join dbo.EmpRank d(nolock) on d.RankID = a.RankID
left join dbo.EmpHistory f(nolock) on f.EmpID = a.EmpID
and f.EmpHistoryID = (select max(g.EmpHistoryID)
from dbo.EmpHistory g(nolock)
where g.EmpID = a.EmpID
AND G.RankID not like 'T%'
and g.EmpHistoryID < c.EmpHistoryID)
left join dbo.EmpRank h(nolock) on h.RankID = f.RankID
LEFT JOIN dbo.EmpHistory J(NOLOCK) ON J.EmpID = A.EmpID
AND J.EmpHistoryID = (
SELECT max(K.EmpHistoryID )
FROM dbo.EmpHistory K(NOLOCK)
WHERE K.EmpID = J.EmpID
AND K.AgentRankID NOT LIKE 'T%'
AND K.MonitorDate = (
SELECT max(M.MonitorDate )
FROM dbo.EmpHistory M(NOLOCK)
WHERE M.EmpID = J.EmpID
AND M.RankID NOT LIKE 'T%'
)
)
where
A.Prof=1
c.RankID like 'T%'
AND c.RankID <>'TSST'
AND A.StartDate is not null
Here there is one more problem: Even if the Employee is terminated from professor to Assitant, A.Prof values is still 1 and basically Assistant dont have the start dates but when professor are transformed to Assitant, they still contain the start date. How can I handle this in the code. Basically this code assumes that that if emp has the start date then he is the professor. Can any one help me?
</pre>
答案 0 :(得分:0)
create fnemprank(@inputrankid int)
returns @result table(
EmpHistID int,
EmpID nvarchar,
RankID nvarchar,
MonitorDate date,
RankName nvarchar)
as
begin
insert into @result (EmpHistID,EmpID,RankID,MonitorDate,RankName)
select top 1 * from emphistory eh where eh.rankid=@inputrankid
order by eh.monitordate desc
return
end