How to write a sql query to get a employees total experience [Previous companies exp. and current company experience]. I have only two columns in sql tables. Joining date and leaving dates.
Plzz solve my query as per as soon possible.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajesh PathakotiPosted Jul 19, 2014, 1:22 AM
For ex:
EmpPresentCompDtls- EmpID,EmpName, JoiningDate,LeavingDate
EmpPrevoisCompDtls-EmpID,PreviousComp, JoiningDate,LeavingDate
select EmpId,EmpName,(case when e1.EmpId =e2.EmpId then (SELECT DATEDIFF(YY,e2.JoiningDate,e2.LeavingDate)+DATEDIFF(YY,e1.JoiningDate,e1.LeavingDate)) else (SELECT DATEDIFF(YY,e1.JoiningDate,e1.LeavingDate)) end) as TotalExp from EmpPresentCompDtls e1,EmpPrevoisCompDtls e2 where e1.EmpId=e2.EmpId
May be Sytax errors check once, but the logic is correct
Ashu DhaundiyalPosted Jul 18, 2014, 7:23 AM
Let me explain all the query...
I have a table that contain employee information such as EmpID,EmpName, JoiningDate, etc....
I have one another table that contain employee previous exprience details such as Emp_previous_Company,Joining_Date,Leaving_Date,Last_CTC etc...
I have a grid view like that....
-----------------------------------------------------------------------------------------------------
EmpID EmpName Desig Deptt DOJ Currt_Exp Total_Exp
-----------------------------------------------------------------------------------------------------
101 Ramesh Developer IT 13 Jan 2013 01 Yr 6 M Blank
101 Sureh Developer IT 01 Nov 2012 01 Yr 8 M 5 Yr
102 Mohan Programmer IT 01 Nov 2011 02 Yr 8 M Blank
103 Sohan Web Design IT 01 Nov 2010 03 Yr 8 M 5 Yr
104 Mukesh Network IT 01 Nov 2009 04 Yr 8 M 5 Yr
-----------------------------------------------------------------------------------------------------
If Total_Exp coloum is Blank than it mean that employee have not any previous companies work experience.
My question is if Total_Exp column is Blank than total_Exp value is current_exp.......
How to write a query.......
Plz plz solve it.......
Rajesh PathakotiPosted Jul 18, 2014, 6:19 AM
SELECT DATEDIFF(YY,JoiningDate,LeavingDate) then it will return in Years