SELECT wof.UDF_CHAR7 'Business Unit',wo.WORKORDERID 'Request ID',mdd.MODENAME 'Request Mode'
,wof.UDF_CHAR7 'Business Unit',pd.PRIORITYNAME 'Priority'
,aau.FIRST_NAME 'Contact',wo.TITLE 'Subject',std.STATUSNAME ,
DATEADD(s,DATEDIFF(s,GETUTCDATE(),GETDATE()) + (wo.CREATEDTIME/1000),'1970-01-01 00:00:00')
'Created Time'
,DATEADD(s,DATEDIFF(s,GETUTCDATE(),GETDATE()) + (wo.COMPLETEDTIME/1000),'1970-01-01 00:00:00')'Completed Time'
,
DATEDIFF(day,DATEADD(s,DATEDIFF(s,GETUTCDATE(),GETDATE()) + (wo.CREATEDTIME/1000),'1970-01-01 00:00:00')
,(DATEADD(s,DATEDIFF(s,GETUTCDATE(),GETDATE()) + (wo.COMPLETEDTIME/1000),'1970-01-01 00:00:00'))) AS 'Turnaround Time TAT(Days)'
,std.STATUSNAME 'Request Status',ti.FIRST_NAME 'Support Rep',
rcontact.LANDLINE 'Contact Phone',rcontact.EMAILID 'Contact Email'
FROM WorkOrder_Threaded wot
INNER JOIN WorkOrder wo ON wot.WORKORDERID=wo.WORKORDERID
LEFT JOIN ModeDefinition mdd ON wo.MODEID=mdd.MODEID
LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID
LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID
LEFT JOIN AaaUserContactInfo user_contact ON aau.USER_ID=user_contact.USER_ID
LEFT JOIN AaaContactInfo rcontact ON user_contact.CONTACTINFO_ID=rcontact.CONTACTINFO_ID
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
LEFT JOIN PriorityDefinition pd ON wos.PRIORITYID=pd.PRIORITYID
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
LEFT JOIN WorkOrder_Fields wof ON wo.WORKORDERID=wof.WORKORDERID
WHERE (wo.DEPARTMENTID = 1) AND wot.THD_WOID=wot.WORKORDERID AND
std.STATUSNAME ='Closed'
ORDER BY 1, 9, cd.CATEGORYNAME, 5
DATEDIFF((longtodate(wo.COMPLETEDTIME) ),(longtodate(wo.CREATEDTIME) ))