title: Query to find long running jobs in SQL server
body: <pre><code>select
  j.name as 'JobName',
  run_date,
  run_time,
  msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime',
  h.run_duration,
  ((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60) 
        as 'RunDurationMinutes'
  From msdb.dbo.sysjobs j 
  INNER JOIN msdb.dbo.sysjobhistory h 
  ON j.job_id = h.job_id 
  where j.enabled = 1  
  AND 
  ((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60) &gt; 1 
  </code></pre>
  
  <p>The above SQL query will fetch list of a all jobs that takes more then a minute.
  But it give a huge list, i dont want that all.
  I just want last 2 run of every jobs.
  I tried using top 2 and order by desc but it does not list all the jobs in the list.
  I just want last 2 run of every job.</p>
  
  <p>Any suggestions.?</p>