SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Employee transfer details
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vipinjha123
Starting Member

India
45 Posts

Posted - 05/28/2012 :  02:39:30  Show Profile  Reply with Quote
Dear All,
I am looking for a query where i can find employee transfer details
like 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

India
47189 Posts

Posted - 05/28/2012 :  02:43:41  Show Profile  Reply with Quote

select t.empid,
t1.project as from_project,
t.project as to_project
from transfer t
outer 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


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vipinjha123
Starting Member

India
45 Posts

Posted - 05/28/2012 :  02:50:41  Show Profile  Reply with Quote
Hi Visakh thankx for your reply.
my query is

SELECT DISTINCT
A.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_STAFFID
inner join ERM_LOCATION_MASTER C ON C.LOCATION_ID=A.EMP_LOCATION_ID
INNER JOIN ERM_OU_MASTER D ON D.OU_ID=B.OU_ID
INNER JOIN SHIFT_MASTER E ON E.SHIFT_CODE=A.EMP_SHIFTCODE
LEFT JOIN ERM_GRADE_MASTER G ON A.EMP_GRADE_CODE=G.GRADE_CODE
WHERE 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 from

regards,
vipin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 05/28/2012 :  03:00:26  Show Profile  Reply with Quote

;With Emp_Projects
AS
(
SELECT DISTINCT
A.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_STAFFID
inner join ERM_LOCATION_MASTER C ON C.LOCATION_ID=A.EMP_LOCATION_ID
INNER JOIN ERM_OU_MASTER D ON D.OU_ID=B.OU_ID
INNER JOIN SHIFT_MASTER E ON E.SHIFT_CODE=A.EMP_SHIFTCODE
LEFT JOIN ERM_GRADE_MASTER G ON A.EMP_GRADE_CODE=G.GRADE_CODE
WHERE 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 t
outer 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) t1
ORDER BY EMP_EFFECTIVE_FROM ASC,EMP_STAFFID 



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vipinjha123
Starting Member

India
45 Posts

Posted - 05/28/2012 :  03:37:33  Show Profile  Reply with Quote
thank you very much friend,
its really a very useful,
but i dont know ahy all first from clumn showing null.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 05/28/2012 :  13:51:18  Show Profile  Reply with Quote
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 guess

post some sample data for us to see why

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000