Question : SQL to find the previous dept of an employee

I have a requirement where I have to get the Previous department of an employee over the course of dates. I have attached an image which shows what I am exactly trying to achieve. I have also attached sql for the dummy data in the image. Please suggest me on how to achieve this.

Thanks in advance




1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
---SQL for Dummy data in the image

select person_id,dt,version_nbr,dept,pd
from
(
select 2345 person_id,trunc(sysdate)-30 dt,0 version_nbr,'HR' dept, 'PD1' PD from dual
union all
select 2345 person_id,trunc(sysdate)-6,1,'Finance', 'PD2' from dual
union all
select 2345 person_id,trunc(sysdate)-3,2,'Finance', 'PD3' from dual
union all
select 2345 person_id,trunc(last_day(sysdate) ),3,'Sales', 'PD3' from dual
union all

select 1264 person_id,trunc(sysdate)-30 ,0 ,'Global HR' dept, 'PD1' PD from dual
union all
select 1264 person_id,trunc(sysdate)-6,1,'Global Finance', 'PD2' from dual
union all
select 1264 person_id,trunc(sysdate)-3,2,'Global Finance', 'PD3' from dual
union all
select 1264 person_id,trunc(last_day(sysdate) ),3,'Global Sales', 'PD3' from dual

) a

order by 1,2
Attachments:
 
Required Prev Dept
Required Prev Dept
 

Answer : SQL to find the previous dept of an employee

I'd do something like:

    Do While InStr(str, "  ") > 0
        str = Replace(str, "  ", " ")
    Loop

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Set objFSO = CreateObject("Scripting.FileSystemObject") 
myFile = "c:\macros\aces\claim13.txt" 
Set objFile = objFSO.OpenTextFile(myFile,1) 
Do Until objFile.AtEndOfStream 
  line=objFile.ReadLine 
  WScript.Echo trimspace(line)
Loop 
Function trimspace(str As String) As String
    Do While InStr(str, "  ") > 0
        str = Replace(str, "  ", " ")
    Loop
    trimspace = str
End Function
Random Solutions  
 
programming4us programming4us