Question

May I ask some little help on how can I get certain result format using my table design.

I have T_EmployeeDTRLogs table where the current design is the following:

| edl_EmployeeID |  edl_LogDate  |    edl_LogTime    |  edl_LogType  |
|     KMP0047    |   2014-05-05  |  07:35:12.1200000 |      IN       |
|     KMP0047    |   2014-05-05  |  15:06:23.3470000 |      OUT      |

Using the table design and given records I want to generate report or result into the following format:

| edl_EmployeeID |  edl_LogDate  |   In    |    Out   | 
|    KMP0047     |   2014-05-05  | 7:35:12 |  3:06:23 |

I am having problem on how can I generate this kind of result.

Can I have some your advises and help on how to can I generate this report.

Thank you very much in advance.

Was it helpful?

Solution

Try this;

select ein.edl_EmployeeID, ein.edl_LogDate, ein.edl_LogTime as [Out], eout.edl_LogTime [Out]
from T_EmployeeDTRLogs ein
inner join T_EmployeeDTRLogs eout on ein.edl_EmployeeID = eout.edl_EmployeeID
and ein.edl_LogDate = eout.edl_LogDate
where ein.edl_LogType = 'IN'
and eout.edl_LogType = 'OUT'

OTHER TIPS

A simple group by can give you the results you want without self join:

select  g.edl_EmployeeId, g.edl_LogDate, 
        min( when g.edl_LogType = 'IN' then g.edl_LogTime end ) as in, 
        min( when g.edl_LogType = 'OUT' then g.edl_LogTime end ) as out
from dbo.T_EmployeeDTRLogs g
group by g.edl_EmployeeId, g.edl_LogDate
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top