Solved

Stored Procedure in SQL Server 2008

Posted on 2011-03-22
3
292 Views
Last Modified: 2012-05-11
Hello Experts,

I'm having trouble in writing a procedure to calculate "Work Time" for each ticket that is resolved in Help Desk environment. This work time should not calculate the time when a ticket is in "Pending" status.

This is the table structure with the data

TicketNo    Status             Datetime
TK123       TicketOpen      03/22/2011  7:00AM
TK123       To - Pending   03/22/2011 8:00AM
TK123       To - Open        03/22/2011 9:30AM
TK123       To-Pending        03/22/2011 10:00AM
TK123       To-Open           03/22/2011  10:45AM
TK123      To-Closure        03/22/2011   11:30AM

So actual work time should be 2 hours and 15 Minutes excluding Pending Hours ( Time when the ticket went to "To-Pending" and came out to "To-Open" status) which is 1 Hour and 15 Minutes.

How can I write a Procedure to do this calculate? Please assist.

Thank you very much
0
Comment
Question by:angel7170
3 Comments
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 35188802
-- i get 180 total minutes.  I'm summarizing time differences when the status is not "To- Pending"

declare @myTable table (myTicket nvarchar(10), myStatus nvarchar(15), myDate smalldatetime)

insert into @myTable values ('TK123', 'TicketOpen', '03/22/2011  7:00AM')
insert into @myTable values ('TK123', 'To - Pending',  '03/22/2011 8:00AM')
insert into @myTable values ('TK123', 'To - Open', '03/22/2011 9:30AM')
insert into @myTable values ('TK123', 'To-Pending', '03/22/2011 10:00AM')
insert into @myTable values ('TK123', 'To-Open', '03/22/2011 10:45AM')
insert into @myTable values ('TK123', 'To-Closure', '03/22/2011 11:30AM');

--below is code

with MyTimeOnTicket as
(
      select
            ROW_NUMBER() over (Partition By myTicket Order by myDate Asc) as Instance_Counter
      ,      myTicket
      ,      myStatus
      ,      myDate
      
      from @myTable      
)

select      MyTicket, SUM(TimeSpanInMinutes) as TotalTicketMinutes
from
(
      select      
            cur.myTicket            as      myTicket
      ,      cur.myStatus            as      myStatus
      ,      cur.myDate                  as      CurrentTaskDateTime
      ,      prev.myDate                  as      PreviousTaskDateTime
      
      ,      case
                  when prev.myDate is not null and replace(cur.myStatus,' ','') <> 'To-Pending'
                  then datediff(minute, prev.myDate,cur.myDate)
                  else null
            end                              as      TimeSpanInMinutes

      from      myTimeOnTicket cur

      left outer join MyTimeOnTicket prev on
                  prev.myTicket = cur.myTicket
            and (prev.Instance_Counter + 1) = cur.Instance_Counter

) TicketDetailWithTimeSpan
      
 
 group by myTicket
 
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 35188885
This should do and use it in a procedure if required:
select TicketNo, cast(cast(135/60 as varchar(10)) + ':' + cast(135%60 as varchar(10)) as time)
FROM 
(
select t1.TicketNo, t1.Status old_status, t2.Status new_status, DATEDIFF(mi, t1.Datetime, t2.Datetime) time_value
from ur_table_name t1
left join ur_table_name t2 on t1.TicketNo = t2.TicketNo and t1.rnum = t2.rnum - 1
Where t1.Status <> 'Pending' ) temp
GROUP BY TicketNo

-- Tested with your sample input

;with cte as (
SELECT TicketNo, Status, Datetime, ROW_NUMBER() over ( PARTITION BY TicketNo order by Datetime) rnum
FROM (
SELECT 'TK123' TicketNo, 'Open' Status,  cast('2011-03-22 7:00:00' as datetime) Datetime
union all
SELECT 'TK123', 'Pending',  cast('2011-03-22 8:00:00' as datetime)
union all
SELECT 'TK123', 'Open',  cast('2011-03-22 9:30:00' as datetime)
union all
SELECT 'TK123', 'Pending',  cast('2011-03-22 10:00:00' as datetime)
union all
SELECT 'TK123', 'Open',  cast('2011-03-22 10:45:00' as datetime)
union all
SELECT 'TK123', 'Closure',  cast('2011-03-22 11:30:00' as datetime)
)temp
)
select TicketNo, cast(cast(135/60 as varchar(10)) + ':' + cast(135%60 as varchar(10)) as time)
FROM 
(
select t1.TicketNo, t1.Status old_status, t2.Status new_status, DATEDIFF(mi, t1.Datetime, t2.Datetime) time_value
from cte t1
left join cte t2 on t1.TicketNo = t2.TicketNo and t1.rnum = t2.rnum - 1
Where t1.Status <> 'Pending' ) temp
GROUP BY TicketNo

Open in new window

0
 

Author Comment

by:angel7170
ID: 35265251
Thank you both.

I tried gohord solution and it works as expected. Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl Server 2012 r2 - Why temp table shrinks from large query 4 40
MS SQL Delete Query 9 37
SSRS 2013 - Overlapping reports 2 32
SQL Recursion schedule 13 18
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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