SELECT 
    DataSourceName, 
    t.Name as TaskName, 
strftime('%Y-%m-%d %H:%M:%S',StartTime/10000000 - 62135596800,'unixepoch') LastRun,
strftime('%H:%M:%S',(EndTime-StartTime)/10000000 - 62135596800,'unixepoch') Duration,
e.ID as TaskRunID,
CASE e.State
    WHEN 4 Then 'Completed Successfully'
    WHEN 6 Then 'Completed with Errors'  
    WHEN 7 Then 'Failed'
    ELSE e.State
    END        
as Status,
CASE  
    WHEN l.Message IS NULL THEN '  ----  '
    ELSE l.Message
    END
AS [Task Detail (Preview)],
CASE e.isScheduled
    WHEN 0 THEN e.UserName
    WHEN 1 THEN 'Schedule'
    END
AS StartedBy

FROM TaskVersions t

Left Join Tasks t2
ON hex(t.id) = hex(t2.ID)

Left Join 
    (SELECT hex(ds.id) as DataSourceID, ds.Name as DataSourceName, ds.isTombStoned, ds.Settings
    FROM DataSourceVersions ds
    WHERE ds.VersionNumber = 
      (SELECT max(VersionNumber) FROM DataSourceVersions b WHERE b.id = ds.id)) as ds
ON ds.DataSourceID=hex(t2.DataSourceID)

Left Join
    (Select Max(Id) as LastExecutionID, hex(taskid) as taskid
    From [ExecutionTasks]
    Group By hex(taskid)) e1
    ON hex(t.id) = e1.taskid

Left Join [ExecutionTasks] e
ON hex(t.id) = hex(e.taskid)
AND e1.LastExecutionID = e.Id

Left Join
    ( SELECT l.Id, l.ExecutionTaskId, l.Severity, l.message
        FROM ExecutionLogs l
        WHERE l.Id = 
          (SELECT min(Id) FROM ExecutionLogs l1 WHERE l.ExecutionTaskId = l1.ExecutionTaskId)
        AND l.Severity > 1
    ) l
ON e.Id = l.ExecutionTaskId

WHERE t.VersionNumber = 
  (SELECT max(VersionNumber) FROM TaskVersions b WHERE b.id = t.id)
  AND t.IsTombstoned <>1

Order By DataSourceName, TaskName