Solved

How to calculate difference in Minutes/Seconds between two date in the same column

Posted on 2010-08-13
7
499 Views
Last Modified: 2012-05-10
Hi all

I have a table that looks like this:

TicketID              SetStatus                                      SetDate                                       Count
100601BG15      Acknowledged                      2010-06-01 14:02:45.330                   1
100601BG15      Waiting on Business      2010-06-01 14:07:28.820                   0
100601BG15      Pending                              2010-06-01 14:07:40.597                   1
100601BG15      Pending                              2010-06-01 14:34:43.737                   1
100601BG15      Pending                           2010-06-03 18:29:47.120                   1      
100601BG15      Post Dated                      2010-06-04 09:00:52.587                   0
100601BG15      Post Dated                      2010-06-07 12:42:37.960                   0
100601BG15      Fulfilled                              2010-06-07 14:38:29.677                   1


I also have a function  CalcMinutesSLA(Date1,Date2) that will calculate the difference between two dates excluding weekends and holidays.

Basically what i need is to calculate the difference in minutes between each date and the one before it and only the ones that have count set to 1.

So from the example above i would need the difference between Fulfilled and Post Date then from Post Dated to Pending and so on.
0
Comment
Question by:urir10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 250 total points
ID: 33430922
Please see the attached script with your sample data and final query

Raj
create table #table
(
	TicketID              varchar(20),
	SetStatus             varchar(20), 
	SetDate               datetime,                        
	Count				int
)

insert into #table
select '100601BG15',      'Acknowledged',                      '2010-06-01 14:02:45.330',                   1
union all
select '100601BG15',      'Waiting on Business',      '2010-06-01 14:07:28.820',                   0 
union all
select '100601BG15',      'Pending',                              '2010-06-01 14:07:40.597',                   1
union all
select '100601BG15',      'Pending',                              '2010-06-01 14:34:43.737',                   1
union all
select '100601BG15',      'Pending',                           '2010-06-03 18:29:47.120',                   1      
union all
select '100601BG15',      'Post Dated',                      '2010-06-04 09:00:52.587',                   0
union all
select '100601BG15',      'Post Dated',                      '2010-06-07 12:42:37.960',                   0 
union all
select '100601BG15',      'Fulfilled',                              '2010-06-07 14:38:29.677',                   1




with cte as
(
	select *, row_number() over (order by SetDate) row_no 
	from #table
) 
select *, datediff(mi, a.SetDate, b.SetDate) as DiffMinute from cte a
	inner join cte b
		on a.row_no = b.row_no - 1

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33430941
Oh! In my query, give the alias 'a'. Here is the modified query.

Raj
with cte as
(
	select *, row_number() over (order by SetDate) row_no 
	from #table
) 
select a.*, datediff(mi, a.SetDate, b.SetDate) as DiffMinute from cte a
	inner join cte b
		on a.row_no = b.row_no - 1

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 33431050
The "count" condition is not accounted for, and it is far from being clear what you mean. In the above CTE, would the added condition be
   a.count = 1 and b.count = 0 or a.count = 0 and b.count = 1
or
  a.count = 1 or b.count = 1
?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 7

Author Comment

by:urir10
ID: 33431208
The count condition only there to say whether the status assosiated to the row should be counted since when the ticket is set to 'Waiting on business' for example it should not count the time towards the SLA value.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 250 total points
ID: 33431230
;with cte as
  (select ticketid,setstatus,setdate
     .row_number() over (partition by (ticketid) order by setdate asc) as rn
   from yourtable
   where [count]=1
  )
select a.ticketid,dbo.calcminutessla(a.setdate,b.setdate)
  from cte as a
  inner join cte as b
    on a.ticketid=b.ticketid
   and a.rn=b.rn-1
 order by 1
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33431949
Check this new query. I verified and seems to be fine.

Raj
select b.*, datediff(mi, PreviousDate, Setdate) as DiffInMinutes
from
(
	select a.*, (Select max(SetDate) from #table where SetDate < a.SetDate and count = 1) as PreviousDate
	from #table a
) b

Open in new window

0
 
LVL 7

Author Closing Comment

by:urir10
ID: 33432481
Thanks alot guys! i used both of those solution to get what i needed.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question