Click here to Skip to main content
16,020,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I was trying to find report in format

jobno | TimeDiff

for the following table

actionhistoryid	jobno	actiondate	        actionstatus
910891	        206448	2011-05-13 10:31:18.877	    33
910931	        206448	2011-05-13 10:38:43.673	     8


Where actionstatus value stands for 33(For-Marketing),8(Approve)
TimeDiff is time period, when the job was send For Marketing and when the job is Approved by marketing

can anyone tell me, how can i write query for the same?

Thanks in advance
Posted
Updated 12-May-11 20:17pm
v2

another way using inner join

select a.jobno, datediff(s, a.actiondate, b.actiondate)
from table a
inner join table b on a.jobno = b.jobno and a.actionhistoryid != b.actionhistoryid
and a.actionstatus = 33
where a.jobno = @jobno


Will give you the difference in seconds
 
Share this answer
 
v2
SQL
SELECT 
      jobno, 
      TimeDifference =  datediff(decimal(5,2), 0, (startTime - endTime))
FROM
(
      SELECT
            startTime = convert(datetime, select actiondate from table where ( jobno = jonId and actionstatus = 33 )),
            endTime   = convert(datetime, select actiondate from table where ( jobno = jobId and actionstatus = 8 ))
)


This should do the trick for you assuming you have a specific value for jobId.

Else you need to fine tune the code with small modification.

With datediff function you can get the time difference in Minutes, Seconds and Milliseconds.

This you can mention as the first parameter.

datediff ( format, startDate, endDate )

BR//
Harsha
 
Share this answer
 
v3
Comments
dhage.prashant01 13-May-11 6:27am    
I tried following

with CTE as (
select jobno,
(select actiondate from tbl_action_history A2 where A2.actionhistoryid=(A1.actionhistoryid-1)) [QC],
(select actiondate from tbl_action_history A3 where A3.actionhistoryid=A1.actionhistoryid) [ACC]
from tbl_action_history A1
where actionstatus = 8 )

SELECT C.jobno,C.QC,C.ACC,DATEDIFF(D,C.ACC,C.QC) FROM CTE C

C.QC values are coming wrong. As u see in my question the entries are back to back

any solution y C.QC values coming wrong?
use datdiff...

try this
 
Share this answer
 
I got following the result for my query

WITH CTE AS
(
	select 
		A1.jobno,
		CONVERT(date, A1.actiondate) [QC],
		(select CONVERT(date, MAX(actiondate)) from tbl_action_history A2 
		where A2.jobno=A1.jobno and A2.actionhistoryid > A1.actionhistoryid and A2.actionstatus = 8) [ACC],
		actionstatus,cycle
	from tbl_action_history A1
	where actionstatus = 33 and CONVERT(date,actiondate) between '05/12/2011' and '05/13/2011')

SELECT C.jobno,C.QC,C.ACC,DATEDIFF(DAY,C.QC, C.ACC) [DateDiff],C.actionstatus,c.cycle
FROM CTE C order by C.jobno desc


Hope it helps others, if they face the same situation like me.
 
Share this answer
 

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