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 |
Mahavirjadhav
Starting Member
18 Posts |
Posted - 2012-11-19 : 01:23:29
|
Dear All, I am having tables 1. EmployeeInfo(emp_id, empname, DOB,DOJ)2. EmployeeWorkHistory(id,empId, deptid, fromdate,todate) EmployeeInfo:emp_id empname DOB DOJ1001 abc 1/1/1985 1/1/20051002 xyz 12/12/1986 1/2/2006EmployeeWorkHistory:id empid deptid fromdate todate1 1001 1 1/1/2005 31/12/20052 1001 2 1/1/2006 31/12/20063 1001 3 1/1/2007 31/12/20074 1002 2 1/2/2006 31/12/20065 1002 3 1/1/2007 31/12/20076 1002 4 1/1/2008 31/12/20087 1002 1 1/1/2009 31/12/20098 1001 4 1/1/2007 31/12/20079 1001 2 1/1/2008 31/12/201110 1002 3 1/1/2010 31/12/2011I want following resultempname deptid fromdate todateabc 4 1/1/2007 31/12/2007xyz 1 1/1/2009 31/12/2009Kindly help me.Thanks in advance.Mahavir |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2012-11-19 : 01:26:20
|
It seems to be your homework. Study about joins in BOL..Senthil Kumar C------------------------------------------------------MCITP - Database Administration SQL SERVER 2008MCTS - Database Development SQL SERVER 2008 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-11-19 : 02:22:08
|
looks like thisSELECT ei.empname,ewh.deptid,ewh.fromdate,ewh.todateFROM EmployeeInfo eiINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY fromdate DESC,todate DESC) AS Seq,* FROM EmployeeWorkHistory )ewhON ewh.empid = ei.Emp_idAND ewh.Seq=2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Mahavirjadhav
Starting Member
18 Posts |
Posted - 2012-11-19 : 02:42:55
|
Thanks dude. It works. Thanks again.quote: Originally posted by visakh16 looks like thisSELECT ei.empname,ewh.deptid,ewh.fromdate,ewh.todateFROM EmployeeInfo eiINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY fromdate DESC,todate DESC) AS Seq,* FROM EmployeeWorkHistory )ewhON ewh.empid = ei.Emp_idAND ewh.Seq=2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Mahavir |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-11-19 : 02:46:54
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|