0
<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 = 
        (

저는 EmpHistory, EmpRank, Emp 및 fnEmpRank라는 세 개의 테이블과 테이블 값 함수가 있습니다. 다음 샘플 데이터가 주어진다 :테이블 값 함수 사용 코드

하는 테이블 EmpHistory 생성 ( EmpHistID의 INT, 다는 EmpID VARCHAR, RankID의 VARCHAR, Monitordate 날짜 Rankname의 VARCHAR)

EmpHistory에

인서트 1 선택 'ABA ','JPR ','2008년 10월 6일 ','주니어 교수 ABA 'EmpHistory에

삽입 2를 선택 ' ','JPR ','2009년 11월 7일 ','주니어 교수 '

EmpHistory에 5,

삽입 'ABA', 'TERM', '2012년 2월 8일', '종료 교수'

EmpHistory에

인서트 4 선택 3 선택 'ABA', 'ASST', '2012 -6-22 ','연구소 지원 EmpHistory 선택으로 ','2012년 7월 2일 ','연구소 도우미 '

삽입 ABA', 'ASST'EmpHistory에

삽입 5를 선택 ' 1, 'aba', 'JPR', '2012-8-4', 'Terminated Assistant'

테이블 만들기 EmpRank ( RankID의 VARCHAR, RankName의 VARCHAR ) EmpRank EmpRank으로 선택 'JPR', '주니어 교수는'EmpRank 에

삽입 선택 'SPR', '수석 교수'

삽입에

삽입 'ASST'

012 EmpRank 선택 'SASST', '수석 도우미'로, '주니어 도우미'

삽입을 선택 EmpRank에 3,516,

삽입 'PL', '교장'를 선택

테이블 직원 ( 다는 EmpID VARCHAR, EmpStartDate 일) 직원에

삽입 'ABA'를 선택 을 작성, '2008-10-06 SDH ',' '직원에

삽입 선택 ' ','2007년 9월 23일을 abc01 '직원에

삽입 선택 '2 009-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 &lt; 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

을'직원에

삽입을 선택 '. 귀하의 논평은 여러 개의 'xRoleID' 열을 나타내지 만 귀하의 견본에는 존재하지 않습니다.내가 짐작할 수 있겠지만, 당신이 이것을 알기는 쉽다 * 당신이 의미하는 바를 (잘만하면) –

답변

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 [email protected] 
    order by eh.monitordate desc 
return 
end 
+0

유감스럽게도 자료형을 준다. – SRIRAM

+0

나는 또한 함수를 가지고있다. ... 나는 함수를 사용하고 싶다. 쿼리 – user1599392

+0

그냥 쿼리에서 함수를 호출하십시오. 당신의 질문을 보여 주어 내가 시도해 볼 수도 있도록 – SRIRAM