| Author |
Topic |
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2011-10-23 : 17:39:58
|
| Hi AllI have requirement where I need to find out the diff of the time_taken between to date for a particular job .Please find my below query which I have written to find the time taken by the jobs for every day . Now I want to get that how many minutes differ the job from previous to next date ..I hope I clear with my question :Here is the Query :DECLARE @COUNTER INT,@MAX INT SET @COUNTER = 0SET @MAX = 3 CREATE TABLE #TEMP(Source VARCHAR(30),RunDate varchar(20), TimeTakenInMinutes INT)WHILE (@COUNTER <= @MAX)BEGIN INSERT INTO #TEMP(Source,RunDate,TimeTakenInMinutes) select 'EnterpriseJPFactEnroll' AS SOURCE , convert (varchar(20),GETDATE()-@COUNTER,101) AS dATE, datediff(MINUTE, (select LogDateTime from SSIS..LogDetail where MessageID ='JEDI-0201' and DATEDIFF(DD,LogDateTime,GETDATE())=@COUNTER ),(select LogDateTime from SSIS..LogDetail where MessageID ='JEDI-0232' and DATEDIFF(DD,LogDateTime,GETDATE())=@COUNTER ))AS MINUTE SET @COUNTER = @COUNTER + 1END SELECT * FROM #TEMPDROP TABLE #TEMPMy OutPut:Source RunDate TimeTakenInMinutes EnterpriseJPFactEnroll 10/24/2011 NULLEnterpriseJPFactEnroll 10/23/2011 88EnterpriseJPFactEnroll 10/22/2011 68EnterpriseJPFactEnroll 10/21/2011 74I want extra colum added Diff and output mentioned in that.I just manully calulated the diff and populated the columns..Source RunDate TimeTakenInMinutes DiffEnterpriseJPFactEnroll 10/24/2011 NULL NullEnterpriseJPFactEnroll 10/23/2011 88 20EnterpriseJPFactEnroll 10/22/2011 68 -6EnterpriseJPFactEnroll 10/21/2011 74 0Suggestion and Solutions Both are welcomeThanksVIjay Sahu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-24 : 04:14:26
|
just add below after insertALTER TABLE #TEMP ADD DIff Int NULLUPDATE tSET t.DIff =t1.TimeTakenInMinutes FROM #Temp tOUTER APPLY (SELECT TOP 1 TimeTakenInMinutes FROM #Temp WHERE Source = t.Source AND RunDate > t.RunDate ORDER BY RunDate ASC) t1SELECT * FROM #TEMPDROP TABLE #TEMP ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2011-10-24 : 04:42:44
|
Try this:WITH CTE AS( SELECT T.Source, T.RunDate, T.TimeTakenInMinutes, ROW_NUMBER() OVER(PARTITION BY T.Source ORDER BY CONVERT(DATE, T.RunDate, 101)) AS row_num FROM #TEMP AS T)SELECT T.Source, T.RunDate, T.TimeTakenInMinutes, T.TimeTakenInMinutes - ISNULL(P.TimeTakenInMinutes, T.TimeTakenInMinutes) AS Diff FROM CTE AS T LEFT OUTER JOIN CTE AS P ON P.Source = T.Source AND P.row_num = T.row_num - 1 And it is better to change RunDate datatype from VARCHAR(20) to DATE. This way you will eliminate the chance of bad data, and get better performance. |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2011-10-24 : 16:56:17
|
quote: Originally posted by visakh16 just add below after insertALTER TABLE #TEMP ADD DIff Int NULLUPDATE tSET t.DIff =t1.TimeTakenInMinutes FROM #Temp tOUTER APPLY (SELECT TOP 1 TimeTakenInMinutes FROM #Temp WHERE Source = t.Source AND RunDate > t.RunDate ORDER BY RunDate ASC) t1SELECT * FROM #TEMPDROP TABLE #TEMP ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hi VikasThanks for your effort on my problem ..But It does not give output What i need . After your query Output is coming like this Source RunDate TimeTakenInMinutes DIffEnterpriseJPFactEnroll 10/25/2011 NULL NULLEnterpriseJPFactEnroll 10/24/2011 71 NULLEnterpriseJPFactEnroll 10/23/2011 88 71EnterpriseJPFactEnroll 10/22/2011 68 88and I want ouput like below Source RunDate TimeTakenInMinutes DIffEnterpriseJPFactEnroll 10/25/2011 NULL NULLEnterpriseJPFactEnroll 10/24/2011 71 -17EnterpriseJPFactEnroll 10/23/2011 88 20EnterpriseJPFactEnroll 10/22/2011 68 0ANyways I got the reply from malpashaa on this and he has used CTE to implement this and desired output came.Can you please try to modify your query for the desire output so that I may get different ideas on this problem Thanks For your support.. |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2011-10-24 : 16:57:54
|
quote: Originally posted by malpashaa Try this:WITH CTE AS( SELECT T.Source, T.RunDate, T.TimeTakenInMinutes, ROW_NUMBER() OVER(PARTITION BY T.Source ORDER BY CONVERT(DATE, T.RunDate, 101)) AS row_num FROM #TEMP AS T)SELECT T.Source, T.RunDate, T.TimeTakenInMinutes, T.TimeTakenInMinutes - ISNULL(P.TimeTakenInMinutes, T.TimeTakenInMinutes) AS Diff FROM CTE AS T LEFT OUTER JOIN CTE AS P ON P.Source = T.Source AND P.row_num = T.row_num - 1 And it is better to change RunDate datatype from VARCHAR(20) to DATE. This way you will eliminate the chance of bad data, and get better performance.
Thanks malpashaa your query works exactlty what I needed.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-25 : 00:32:20
|
sorry it should have beenALTER TABLE #TEMP ADD DIff Int NULLUPDATE tSET t.DIff =t.TimeTakenInMinutes -t1.TimeTakenInMinutes FROM #Temp tOUTER APPLY (SELECT TOP 1 TimeTakenInMinutes FROM #Temp WHERE Source = t.Source AND RunDate > t.RunDate ORDER BY RunDate ASC) t1SELECT * FROM #TEMPDROP TABLE #TEMP ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2011-10-25 : 19:51:07
|
| Thanks Vikas it works... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-26 : 00:42:53
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|