Iam having a table with following details
ID statusID logTime
100238 1 2011-07-07 03:48:43.000
100238 2 2011-07-07 03:48:46.000
100238 1 2011-07-07 09:07:57.000
100238 2 2011-07-07 16:12:28.000
100238 1 2011-07-08 02:59:57.000
100238 2 2011-07-08 03:00:00.000
100238 1 2011-07-08 09:26:37.000
100238 2 2011-07-08 14:03:05.000
and the required output should be like
repID ClockIn ClockOut
100238 2011-07-07 03:48:43.000 2011-07-07 03:48:46.000
100238 2011-07-07 09:07:57.000 2011-07-07 16:12:28.000
100238 2011-07-08 02:59:57.000 2011-07-08 03:00:00.000
100238 2011-07-08 09:26:37.000 2011-07-08 14:03:05.000
ie.. if the statusID is 1 then logtime has to be in ClockIn column and if the statusID is 2, logtime has to be in ClockOut column
I have used the query like
SELECT repID,
ClockIn,
ClockOut from
(SELECT a.eventID,
a.repID,
a.logTime,
CASE WHEN a.statusID = 1 THEN a.logTime ELSE NULL END AS ClockIn,
CASE WHEN b.statusID = 2 THEN b.logTime ELSE NULL END AS ClockOut
FROM tbl_ets_reptimelog a
LEFT JOIN tbl_ets_reptimelog b ON a.repID = b.repID)c
and it results like
repID ClockIn ClockOut
100238 2011-07-07 03:48:43.000 NULL
100238 2011-07-07 03:48:43.000 2011-07-07 03:48:46.000
100238 2011-07-07 09:07:57.000 NULL
100238 2011-07-07 09:07:57.000 2011-07-07 16:12:28.000
100238 2011-07-08 02:59:57.000 NULL
100238 2011-07-08 02:59:57.000 2011-07-08 03:00:00.000
100238 2011-07-08 09:26:37.000 NULL
100238 2011-07-08 09:26:37.000 2011-07-08 14:03:05.000
How to remove the extra row that appears with the NULL valu ein the ClockOut Column
Please help to get out of this problem......
Just add a WHERE clause in outer query
select repID,ClockIn,ClockOut from(select a.eventID, a.repID, a.logTime,
case when a.statusID = 1 then a.logTime else null end as ClockIn,
case when b.statusID = 2 then b.logTime else null end as ClockOut from tbl_ets_reptimelog a
left join tbl_ets_reptimelog b on a.repID = b.repID)c
WHERE ClockOut IS NOT NULL
This query should return the result that you need:
SELECT
t1.ID,
t1.logTime check_in,
CASE WHEN MIN(t2.logTime) < MIN(t3.logTime) OR MIN(t3.logTime) IS NULL
THEN MIN(t2.logTime) END check_out
FROM
Logs t1 LEFT JOIN Logs t2
ON t1.ID = t2.ID
AND t2.statusID=2
AND t1.logTime<t2.logTime
LEFT JOIN Logs t3
ON t2.ID = t3.ID
AND t3.statusID=1
AND t1.logTime<t3.logTime
WHERE
t1.statusID=1
GROUP BY
t1.ID, t1.logTime
Please see fiddle here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.