Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date diff between timein and timeout rows


Date diff between timein and timeout rows

Author
Message
Kasinathan
Kasinathan
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 59
Autoid   Empid   TimeIn    Timeout
1   1009   13-10-2012 22:10    13-10-2012 22:15
2   1009   13-10-2012 22:20    13-10-2012 22:32
3   1009   13-10-2012 22:34    13-10-2012 22:36



I need to find the idel time for this.. like First row out time and secount row in time diff as 5 min like wise each row

Please help me

thanks in advance
robert.gerald.taylor
robert.gerald.taylor
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1315 Visits: 1398
Kasinathan (10/13/2012)
Autoid   Empid   TimeIn    Timeout
1   1009   13-10-2012 22:10    13-10-2012 22:15
2   1009   13-10-2012 22:20    13-10-2012 22:32
3   1009   13-10-2012 22:34    13-10-2012 22:36

I need to find the idel time for this.. like First row out time and secount row in time diff as 5 min like wise each row

Are you looking for the time difference in each record? or for each employee? I'm not sure what you're saying about 5 minutes.

CREATE TABLE #Temp 
(
AutoId int,
EmpId int,
TimeIn datetime,
TimeOut datetime
)
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (1, 1009, '10-13-2012 22:10', '10-13-2012 22:15');
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (2, 1009, '10-13-2012 22:20', '10-13-2012 22:32');
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (3, 1009, '10-13-2012 22:34', '10-13-2012 22:36');

-- Time difference per row --
SELECT AutoId,
EmpId,
TimeIn,
TimeOut,
DATEDIFF(mi, TimeIn, TimeOut) AS MinutesDifference
FROM #Temp

-- Time difference per employee --
SELECT EmpId,
MIN(TimeIn) AS BeginTimeIn,
MAX(TimeOut) AS EndTimeOut,
DATEDIFF(mi, MIN(TimeIn), MAX(TimeOut)) AS MinutesDifference
FROM #Temp
GROUP BY EmpId



HTH,
Rob
sql-programmers
sql-programmers
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 277
Hi,


CREATE TABLE #Temp
(
AutoId int,
EmpId int,
TimeIn datetime,
TimeOut datetime
)
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (1, 1009, '10-13-2012 22:10', '10-13-2012 22:15');
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (2, 1009, '10-13-2012 22:20', '10-13-2012 22:32');
INSERT #Temp (AutoId, EmpId, TimeIn, TimeOut) VALUES (3, 1009, '10-13-2012 22:34', '10-13-2012 22:36');

SELECT * FROM #Temp

;WITH CTE AS
(
SELECT T.EmpId, T.TimeIn, T.TimeOut, ROW_NUMBER() OVER(ORDER BY T.TimeIn, T.TimeOut ) AS row_num
FROM #Temp AS T
)
SELECT N.EmpId, p.TimeIn, p.TIMEOUT, n.TimeIn, DATEDIFF(MI, p.TimeOut, n.TimeIn)
FROM CTE AS N
INNER JOIN
CTE AS P
ON P.row_num = N.row_num - 1;


DROP TABLE #Temp



SQL Sever Performance tuning
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search