Query to find the change in Dueby time when the Site is changed (MSSQL)

Query to find the change in Dueby time when the Site is changed (MSSQL)

Tested in build MSSQL (14306)

SELECT wo.WORKORDERID AS "Request ID", "rtdef"."NAME" AS "Request Type", "std"."STATUSNAME" AS "Request Status",
LONGTODATE(cast(cast(wohd1.prev_value as varchar) as bigint)) AS "Previous Due Date",
LONGTODATE(cast(cast(wohd1.current_value as varchar) as bigint)) AS "Current Due Date",longtodate("wo"."CREATEDTIME") AS "Created Time",
longtodate("wo"."COMPLETEDTIME") AS "Completed Time",
longtodate("wo"."RESOLVEDTIME") AS "Resolved Time",
sd1.name as "Site change from" ,
sd2.name as"Site Changed to" FROM WorkOrder wo
Left Join Workorderstates wos on wo.workorderid=wos.workorderid
LEFT JOIN "RequestTypeDefinition" "rtdef" ON "wos"."REQUESTTYPEID"="rtdef"."REQUESTTYPEID" LEFT JOIN "StatusDefinition" "std" ON "wos"."STATUSID"="std"."STATUSID"
Left Join WorkorderHistory woh ON wo.WorkorderID=woh.WorkorderID
Left Join WorkorderHistoryDiff wohd ON woh.HistoryId=wohd.HistoryId
LEFT JOIN SDOrganization sd1 ON cast(cast(wohd.prev_value as varchar) as varchar)=sd1.org_id
LEFT JOIN SDOrganization sd2 ON cast(cast(wohd.current_value as varchar) as varchar)=sd2. org_id  
Left Join WorkorderHistory woh1 ON wo.WorkorderID=woh1.WorkorderID
Left Join WorkorderHistoryDiff wohd1 ON woh1.HistoryId=wohd1.HistoryId
WHERE (wo.ISPARENT='1') and wohd1.ColumnName  in ('DUEBYTIME') and wohd.ColumnName in ('SITEID') and wohd.historyid=wohd1.historyid

                  New to ADSelfService Plus?