Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
vipinjha123
Starting Member
45 Posts |
Posted - 2012-05-28 : 02:39:30
|
Dear All,I am looking for a query where i can find employee transfer detailslike employee a transfereed from locatioon a to b , employee project shifted from project C to project D.table name transfer(empid,location,project,transfer_effective_date)please suggest the best solution.regards,Vipin jha |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-28 : 02:43:41
|
[code]select t.empid,t1.project as from_project,t.project as to_projectfrom transfer touter apply (select top 1 project from transfer where empid = t.empid and transfer_effective_date < t.transfer_effective_date order by transfer_effective_date desc) t1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-05-28 : 02:50:41
|
Hi Visakh thankx for your reply.my query is SELECT DISTINCTA.EMP_STAFFID "Employee code",A.EMP_FIRSTNAME + ' '+ isnull(A.EMP_MIDDLENAME,'') + ' '+ isnull(A.EMP_LASTNAME,'') "Employee name",LOCATION_NAME,D.OU_NAME Project,G.GRADE_DESCRIPTION,E.SHIFT_NAME,B.EMP_EFFECTIVE_FROM-1 "LWD Before Transfer",B.EMP_EFFECTIVE_FROM "Transfer Effective Date"FROM ERM_EMPLOYEE_MASTER A inner join ERM_EMPLOYEE_MASTER_HISTORY B ON A.EMP_STAFFID=B.EMP_STAFFIDinner join ERM_LOCATION_MASTER C ON C.LOCATION_ID=A.EMP_LOCATION_IDINNER JOIN ERM_OU_MASTER D ON D.OU_ID=B.OU_IDINNER JOIN SHIFT_MASTER E ON E.SHIFT_CODE=A.EMP_SHIFTCODELEFT JOIN ERM_GRADE_MASTER G ON A.EMP_GRADE_CODE=G.GRADE_CODEWHERE A.EMP_ISACTIVE LIKE '%1%' AND A.EMP_STAFFID='A0007'ORDER BY B.EMP_EFFECTIVE_FROM ASC,A.EMP_STAFFID what modification required to get locationto_location from,projectto_project fromregards,vipin |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-28 : 03:00:26
|
[code];With Emp_ProjectsAS(SELECT DISTINCTA.EMP_STAFFID "Employee code",A.EMP_FIRSTNAME + ' '+ isnull(A.EMP_MIDDLENAME,'') + ' '+ isnull(A.EMP_LASTNAME,'') "Employee name",LOCATION_NAME,D.OU_NAME Project,G.GRADE_DESCRIPTION,E.SHIFT_NAME,B.EMP_EFFECTIVE_FROM-1 "LWD Before Transfer",B.EMP_EFFECTIVE_FROM "Transfer Effective Date"FROM ERM_EMPLOYEE_MASTER A inner join ERM_EMPLOYEE_MASTER_HISTORY B ON A.EMP_STAFFID=B.EMP_STAFFIDinner join ERM_LOCATION_MASTER C ON C.LOCATION_ID=A.EMP_LOCATION_IDINNER JOIN ERM_OU_MASTER D ON D.OU_ID=B.OU_IDINNER JOIN SHIFT_MASTER E ON E.SHIFT_CODE=A.EMP_SHIFTCODELEFT JOIN ERM_GRADE_MASTER G ON A.EMP_GRADE_CODE=G.GRADE_CODEWHERE A.EMP_ISACTIVE LIKE '%1%' AND A.EMP_STAFFID='A0007')SELECT t.empid,t1.project as from_project,t.project as to_project,other columns....FROM Emp_Projects touter apply (select top 1 project from Emp_Projects where empid = t.empid and transfer_effective_date < t.transfer_effective_date order by transfer_effective_date desc) t1ORDER BY EMP_EFFECTIVE_FROM ASC,EMP_STAFFID [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
vipinjha123
Starting Member
45 Posts |
Posted - 2012-05-28 : 03:37:33
|
thank you very much friend,its really a very useful,but i dont know ahy all first from clumn showing null. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-28 : 13:51:18
|
quote: Originally posted by vipinjha123 thank you very much friend,its really a very useful,but i dont know ahy all first from clumn showing null.
that means you dont have date values in correct sequence i guesspost some sample data for us to see why------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|