Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi all!!

I have a datatable which has this data


EMPID      || DateOFJoining   || Designation             || Section     ||  EmpName


   1       ||    25 Oct 2007  ||Private Secretary, Gr II||   Establishment || Ajay
   2       ||    02 Aug 2006  || Section Officer        ||   Establishment ||Raj
   1       ||    02 Aug 2006  || Private Secretary, Gr I||   Accounts      ||Ajay
   3       ||   13 Oct 2010   || Reviewing Officer      ||   Establishment || Kiran
   1       ||   01 Jan 2002   || Accountant             ||   Accounts      || Ajay 



now what i need is the lowest DateOfJoining for each employee so it should look like this

EMPID      || DateOFJoining   || Designation             || Section     ||  EmpName


  
   2       ||    02 Aug 2006  || Section Officer        ||   Establishment ||Raj
   3       ||   13 Oct 2010   || Reviewing Officer      ||   Establishment || Kiran
   1       ||   01 Jan 2002   || Accountant             ||   Accounts      || Ajay 


please tell me how can i do this !!!
Posted
Updated 20-Jun-12 23:36pm
v3
Comments
preet88 21-Jun-12 5:01am    
use ORDER BY in your query
like select * from tablename ORDERBY attributename
ujjwal uniyal 21-Jun-12 5:05am    
i don't have to sort data instead i have to filter data for each epmloyee where doteofjoining is lowest
ssd_coolguy 21-Jun-12 5:10am    
hey what do you mean "each epmloyee where doteofjoining is lowest"?
is there multiple records for employee...?
can you explain more...
ujjwal uniyal 21-Jun-12 5:12am    
yeah there are multiple records for each employee. Like in the table above there are 3 records for employee with eid=1. similarly there may or may not be multiple records for employees.

try below query:

SQL
SELECT a.*
FROM employee A,
(SELECT EMPID ,MIN(DateOFJoining) DateOFJoining FROM employee
GROUP BY EMPID) B
WHERE b.EMPID=a.EMPID
AND b.DateOFJoining=a.DateOFJoining
 
Share this answer
 
Comments
Prosan 21-Jun-12 5:26am    
good query
Vani Kulkarni 21-Jun-12 5:38am    
Good One
ssd_coolguy 21-Jun-12 6:30am    
thank you... :)
by this query u can get lowest date of joing of each employee but with name and doj.

SQL
select distinct EmpName, MIN(doj) from TblEmp group by EmpName
 
Share this answer
 
Comments
Vani Kulkarni 21-Jun-12 5:39am    
Good Query!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900