为每个特定的sql作业选择前3的平均值

时间:2016-04-21 20:51:13

标签: sql sql-server tsql



SELECT  jobs.Job_ID AS JobID ,
                    jobs.NAME AS JobName ,
                    act.start_execution_date AS StartExecutionDate ,
                    AVG(FLOOR(run_duration / 100)) AS AvgDurationMin ,
                    CASE 
		--If job average less than 5 minutes then limit is avg+10 minutes
                         WHEN AVG(FLOOR(run_duration / 100)) <= 5
                         THEN ( AVG(FLOOR(run_duration / 100)) ) + 2
		--If job average greater than 5 minutes then limit is avg*limit percentage
                         ELSE ( AVG(FLOOR(run_duration / 100))
                                )
                    END AS DurationLimit ,
                    DATEDIFF(MI, act.start_execution_date, GETDATE()) AS [CurrentDuration]
            FROM    @currently_running_jobs crj
                    INNER JOIN msdb..sysjobs AS jobs ON crj.job_id = jobs.job_id
                    INNER JOIN msdb..sysjobactivity AS act ON act.job_id = crj.job_id
                                                              AND act.stop_execution_date IS NULL
                                                              AND act.start_execution_date IS NOT NULL
                    INNER JOIN msdb..sysjobhistory AS hist ON hist.job_id = crj.job_id
                                                              AND hist.step_id = 0
            WHERE   crj.job_state = 1 --and jobs.NAME = 'long_running_testing'
            GROUP BY jobs.job_ID ,
                    jobs.NAME ,
                    act.start_execution_date ,
                    DATEDIFF(MI, act.start_execution_date, GETDATE())
					HAVING  CASE WHEN AVG(FLOOR(run_duration / 100)) <= 5
							  THEN (AVG(FLOOR(run_duration / 100))) + 2
                            --THEN ( AVG(FLOOR(run_duration / 100)) ) + 1  
                         ELSE ( AVG(FLOOR(run_duration / 100))
                                )
                    END < DATEDIFF(MI, act.start_execution_date, GETDATE())
&#13;
&#13;
&#13;

有人可以帮助我提取最近三个由job_name分区的作业的平均run_duration吗?

请找到我用于提取信息的以下脚本    最近三个run_duration由job_name分区,但它    计算所有run_duration的平均值。

这里我只需要最近三次run_duration平均计数。

a boring table with guids and datetimes and sooper seecrit JobNames, carefully redacted

SELECT top 1 with  ties  jobs.Job_ID AS JobID , AVG(FLOOR(run_duration/100%100 )),
                    jobs.NAME AS JobName ,
                    act.start_execution_date AS StartExecutionDate 
 from msdb..sysjobs AS jobs
inner join msdb..sysjobactivity AS act ON act.job_id = jobs.job_id
INNER JOIN msdb..sysjobhistory AS hist ON hist.job_id = jobs.job_id AND hist.step_id = 0

 GROUP BY jobs.job_ID ,
                    jobs.NAME 
            ,      act.start_execution_date ,hist.run_duration,
                   DATEDIFF(MI, act.start_execution_date, GETDATE())
order by  case when row_number() over (partition by jobs.NAME order by hist.run_duration desc) <= 3 then 0 else 1 end;

1 个答案:

答案 0 :(得分:1)

也许CTE会简化它。

with SeqByJob_cte as (  SELECT  jobs.NAME AS JobName
                            ,hist.run_duration
                            ,row_number() over(partition by jobs.NAME order by act.start_execution_date desc) as seq 
                    from    msdb..sysjobs AS jobs
                            inner join msdb..sysjobactivity AS act ON act.job_id = jobs.job_id
                            INNER JOIN msdb..sysjobhistory AS hist ON hist.job_id = jobs.job_id AND hist.step_id = 0)
select  avg(run_duration) as AvgOf3MostRecentRuns
        ,CASE 
    --If job average less than 5 minutes then limit is avg+10 minutes
                     WHEN AVG(FLOOR(run_duration / 100)) <= 5
                     THEN ( AVG(FLOOR(run_duration / 100)) ) + 2
    --If job average greater than 5 minutes then limit is avg*limit percentage
                     ELSE ( AVG(FLOOR(run_duration / 100))
                            )
                END AS DurationLimit
        ,JobName
from    SeqByJob_cte
where   seq <= 3
group by JobName