Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the below table with columns.
aDate aTime EmpCode EmpName aDateTime
27-Dec-11 9:31 32 Amarnath KP 12/27/11 9:31
27-Dec-11 9:42 32 Amarnath KP 12/27/11 9:42
27-Dec-11 10:01 32 Amarnath KP 12/27/11 10:01
27-Dec-11 14:10 32 Amarnath KP 12/27/11 14:10
27-Dec-11 14:11 32 Amarnath KP 12/27/11 14:11
27-Dec-11 14:13 32 Amarnath KP 12/27/11 14:13
27-Dec-11 14:46 32 Amarnath KP 12/27/11 14:46
27-Dec-11 20:46 32 Amarnath KP 12/27/11 20:46




I want to display the records like below(Parameters From Date and To date)

EmpCode FirstIN LastOUT

32 9.31 20.46

Thanks
Posted

Hi,

You need to use self join with this table as below:

SELECT EmpIN.EmpCode,
       EmpIn.EmpName,
       EmpIn.FirstIN,
       EmpOut.LastOut
FROM   (SELECT   EmpCode,
                 EmpName,
                 aDate,
                 MIN(aTime) AS [FirstIN]
        FROM     tblINOUT
        GROUP BY EmpCode,EmpName, aDate) AS EmpIN
       LEFT OUTER JOIN
       (SELECT   EmpCode,
                 EmpName,
                 aDate,
                 MAX(aTime) AS [LastOut]
        FROM     tblINOUT
        GROUP BY EmpCode,,EmpName, aDate) AS EmpOUT
       ON EmpIN.EmpCode = EmpOUT.EmpCode
          AND EmpIN.aDate = EmpOUT.aDate;
 
Share this answer
 
Comments
v2vinoth 19-Nov-14 2:11am    
Hi Dusara,

Thank you so much. Its working...
v2vinoth 19-Nov-14 2:19am    
Hi Dusara,

In above query, where do I pass parameter as (Empcode and month) or (From date to date). Please help this.
Dusara Maulik 19-Nov-14 5:47am    
You have to create Stored Procedure.
Divyam Sharma 19-Nov-14 2:48am    
Good one..
It is Not Possible in one Query..

our have to take two query for getting your output..

In a first query just sort FirstIN Column in ascending order ...

In Second query just sort LastOUT Column in Descending order....

For Example.

SQL
Select EmpCode,FirstIN from tblName order by FirstIN asc
Select EmpCode,LastOUT from tblName order by LastOUT desc



Thanks
AARIF SHAIKH
 
Share this answer
 
Comments
v2vinoth 19-Nov-14 1:16am    
I want to display the same in Grid. If I split this how can I show in front end.

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