Author |
Topic |
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-02-13 : 10:58:17
|
Hi Everyone,I am trying to find the date difference on a column in one of my tables. The table has 6 coulmns ID, DateSubmitted, RequestID, RequestType, SubmittedBy and Status.I would like to calculate the time difference between two rows of the table by passing a requestID. Attaching the query that i tried,SELECT DATEDIFF(Minute, prev.DateSubmitted, curr.DateSubmitted) FROM Status_History curr INNER JOIN Status_History prev ON prev.ID = curr.ID - 1 WHERE prev.DateSubmitted = (SELECT MAX(maxtbl.DateSubmitted) FROM Status_History maxtbl WHERE maxtbl.ID = curr.ID - 1AND maxtbl.DateSubmitted < curr.DateSubmitted) and curr.RequestID='MR-201227135933' Thanking all in advance,Arun |
|
X002548
Not Just a Number
15586 Posts |
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-02-13 : 11:12:16
|
Sorry for not being clear. The DateSubmitted gets its value as the status changes,So when the Status of the ticket is open i'd have a datesubmitted value, then when the status changes to inprogress i'd have the datesubmitted field set to the time when the status was changed. I'd like to calculate the time taken for ticket to be closed.E.g.Date Submitted Status2012-02-07 14:36:56.920 Open2012-02-08 10:51:59.823 Inprogress2012-02-09 08:22:24.297 Build2012-02-09 08:22:34.813 ClosedI am not able to use the row number concept to get to the result. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-13 : 11:19:19
|
isnt this enough?SELECT RequestID,DATEDIFF(minute,MAX(CASE WHEN Status='Open' THEN DateSubmitted END),MAX(CASE WHEN Status='Closed' THEN DateSubmitted END)) AS CallDurationFROM tableGROUP BY RequestID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-02-13 : 11:28:54
|
Yes it does give me the entire time taken for the ticket. I also wanted to calculate the time difference between each status change (I missed out on mentioning this in my last post, my apologies). Like time taken from open to inprogress, inprogress to build and so on. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-13 : 11:56:08
|
quote: Originally posted by ArunPhilip Yes it does give me the entire time taken for the ticket. I also wanted to calculate the time difference between each status change (I missed out on mentioning this in my last post, my apologies). Like time taken from open to inprogress, inprogress to build and so on.
for that use likeSELECT t.*,DATEDIFF(minute,(SELECT TOP 1 DateSubmitted FROM table WHERE RequestID = t.RequestID AND DateSubmitted < t.DateSubmitted ORDER BY DateSubmitted DESC),t.DateSubmitted) AS TimeDiffFROM table t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ArunPhilip
Starting Member
21 Posts |
Posted - 2012-02-13 : 12:00:01
|
That is just perfect. Thanks everyone |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-13 : 12:11:45
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|