SELECT
SessionID = req.session_id
, BatchStartTime = req.start_time
, BatchMilliseconds = req.total_elapsed_time
, StatementStartTime =
CASE WHEN stat.last_execution_time IS NOT NULL
THEN stat.last_execution_time
ELSE req.start_time
END
, StatementMilliseconds =
DATEDIFF(MILLISECOND,
CASE WHEN stat.last_execution_time IS NOT NULL
THEN stat.last_execution_time
ELSE req.start_time
END, GETDATE()
)
, LoginName = des.login_name
, SQLCommand= req.command