Case Aging Reports

Case Aging Reports

I am trying to write two new query reports used for case aging.  We need to divide the cases into the following buckets:  less than 7 days, 7 to 30 days, 30 to 60 days, 60 to 90 days, and greater than 90 days.  I am trying to write one report for closed cases in the last month and all open cases.  The sql below is for open cases and it works fine outside of Service Desk.  When I try to add it to service desk, it will not run.  Anyone have a suggestion?
 

select ag.Location,
 sum( OneWeek ) as "< 7 Days",
 sum( OneMonth ) as "7 to 30 Days",
 sum( TwoMonths ) as "30 to 60 Days",
 sum( ThreeMonths ) as "60 to 90 Days",
 sum( ThreeMonthsPlus ) as "> 90 Days"
from ( select isnull(qd.QUEUENAME, sdo.NAME ) as Location,
 case when ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) < 7 ) then 1 else 0 end  as OneWeek,
 case when ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) >= 7 ) and ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) < 30 ) then 1 else 0 end  as OneMonth,
 case when ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) >= 30 ) and ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) < 60 ) then 1 else 0 end  as TwoMonths,
 case when ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) >= 60 ) and ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) < 90 ) then 1 else 0 end  as ThreeMonths,
 case when ( datediff(d, dateadd(s, (wo.CreatedTime / 1000), '1970-01-01 00:00:00' ), getutcdate() ) >= 90 ) then 1 else 0 end  as ThreeMonthsPlus
FROM WorkOrder wo
 LEFT OUTER JOIN SiteDefinition sd ON wo.SITEID = sd.SITEID
 LEFT OUTER JOIN SDOrganization sdo ON sd.SITEID = sdo.ORG_ID
 LEFT OUTER JOIN WorkOrder_Queue wo_queue ON wo.WORKORDERID=wo_queue.WORKORDERID
 LEFT OUTER JOIN QueueDefinition qd ON wo_queue.QUEUEID=qd.QUEUEID
 LEFT OUTER JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
 LEFT OUTER JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
WHERE wo.IsParent = 1
 and sdo.Name not in ( 'US Facilities', 'US HR' )
 and std.IsPending = 1
) as Ag
group by ag.Location
























  


I also have a report for closed cases that is similar to the one above that has a problem with the where clause.  I have a work around, but it would be nice to get it to work 100%.  The line that uses <from_lastmonth> and <to_lastmonth> fails if I only use wo.CompletedTime instead of a sub-select that queries the WorkOrder table for the wo.Completed Time.  I found this because I initially was querying the WorkOrderHistory table before I saw the CompletedTime on the WorkOrder table.


 

select ag.Location,
 sum( OneWeek ) as "< 7 Days",
 sum( OneMonth ) as "7 to 30 Days",
 sum( TwoMonths ) as "30 to 60 Days",
 sum( ThreeMonths ) as "60 to 90 Days",
 sum( ThreeMonthsPlus ) as "> 90 Days"
from ( select isnull(qd.QUEUENAME, sdo.NAME ) as Location,
 case when ( ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) < 7 ) then 1 else 0 end  as OneWeek,
 case when ( ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) >= 7 and ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) < 30 ) then 1 else 0 end  as OneMonth,
 case when ( ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) >= 30 and ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) < 60 ) then 1 else 0 end  as TwoMonths,
 case when ( ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) >= 60 and ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) < 90 ) then 1 else 0 end  as ThreeMonths,
 case when ( ( ( wo.CompletedTime - wo.CreatedTime ) / 86400000.0 ) >= 90 ) then 1 else 0 end  as ThreeMonthsPlus
FROM WorkOrder wo
 LEFT OUTER JOIN WorkOrder_Fields wof on wo.WorkOrderID = wof.WorkOrderID
 LEFT OUTER JOIN SiteDefinition sd ON wo.SITEID = sd.SITEID
 LEFT OUTER JOIN SDOrganization sdo ON sd.SITEID = sdo.ORG_ID
 LEFT OUTER JOIN WorkOrder_Queue wo_queue ON wo.WORKORDERID=wo_queue.WORKORDERID
 LEFT OUTER JOIN QueueDefinition qd ON wo_queue.QUEUEID=qd.QUEUEID
 LEFT OUTER JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
 LEFT OUTER JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
 LEFT OUTER JOIN WorkOrderHistory woh ON ( woh.WorkOrderID = wo.WorkOrderID  and woh.Operation in ( 'CLOSE', 'COMPLETED' ) )
WHERE wo.IsParent = 1
 and sdo.Name not in ( 'US Facilities', 'US HR' )
 and std.IsPending = 0
 and ( select wo2.CompletedTime from WorkOrder wo2 WHERE ( wo2.WorkOrderID = wo.WorkOrderID ) ) >= <from_lastmonth>
 and ( select wo2.CompletedTime from WorkOrder wo2 WHERE ( wo2.WorkOrderID = wo.WorkOrderID ) ) <= <to_lastmonth>
) as Ag
group by ag.Location



























Any suggestions on fixing these reports would be appreciated.


Danny Smith

                  New to ADSelfService Plus?