ericarivera411
IS-IT--Management
Hey guys,
I am supposed to write a query that will return the results of my Agent Jobs status, run duration, next run date, if the job failed or not for a certain job category, TPA Conversion. My problem is that I have the following SQL statement, it runs, but it returns to many results. They want only one result per job. Anyone have any great ideas? Any help is appreciated. Thanks!
select Jobs.name as 'Job Name',
syscat.name as Category,
CASE sysjobschedules.freq_type
WHEN 1 THEN 'Once'
WHEN 4 THEN 'Daily'
WHEN 8 THEN 'Weekly'
WHEN 16 THEN 'Monthly'
end as 'Job Frequency',
MAX(run_date) AS 'Last Execution Date',
/* run_time, is really the time the job completed */
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),1,2) + ':' +
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),3,2) + ':' +
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),5,2) as Completion_Time,
run_duration as "Duration(HHMMSS)",
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),1,3) * 3600) + /*convert hrs to seconds */
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),4,2) * 60) + /*convert mins to seconds */
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),6,2)) AS Run_Seconds, /*convert seconds to number */
Case run_status
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
end as Outcome
FROM SQL1.msdb.dbo.sysjobschedules, SQL1.msdb.dbo.sysjobhistory HIST
INNER JOIN SQL1.msdb.dbo.sysjobs jobs on hist.job_id=jobs.job_id
INNER JOIN SQL1.msdb.dbo.syscategories syscat on jobs.category_id = syscat.category_id
WHERE syscat.name = 'TPA Conversion'
GROUP BY jobs.name, syscat.name, sysjobschedules.freq_type, hist.run_date, Hist.run_time, Hist.run_duration, Hist.run_status
Results:
BMA INFORMATION TPA Conversion Daily 20061214 22:00:00 4121 2481 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:00:00 14708 6428 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:41:22 400 240 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:45:23 5911 3551 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:44:35 19 19 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:44:54 206 126 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:47:01 2 2 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:47:03 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:00:00 4223 2543 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:00:00 14130 6090 Failed
BMA INFORMATION TPA Conversion Daily 20061215 22:42:24 446 286 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:47:10 5419 3259 Failed
BMA INFORMATION TPA Conversion Daily 20061217 12:19:27 111 71 Canceled
BMA INFORMATION TPA Conversion Daily 20061217 12:19:27 113 73 Canceled
BMA INFORMATION TPA Conversion Daily 20061217 22:00:00 14709 6429 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:00:01 4217 2537 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:42:19 452 292 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:47:11 5727 3447 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:44:40 17 17 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:44:57 207 127 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:47:04 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:47:09 0 0 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:00:00 14556 6356 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:00:01 4146 2506 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:41:48 445 285 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:46:34 5650 3410 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:43:25 18 18 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:43:43 205 125 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:45:48 2 2 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:45:50 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:00:00 4557 2757 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:00:00 15002 6602 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:45:58 428 268 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:50:28 5700 3420 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:47:29 20 20 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:47:49 205 125 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:49:54 4 4 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:49:58 4 4 Succeeded
BMA INFORMATION TPA Conversion Weekly 20061214 22:00:00 4121 2481 Succeeded
BMA INFORMATION TPA Conversion Weekly 20061214 22:00:00 14708 6428 Succeeded
etc...
I am supposed to write a query that will return the results of my Agent Jobs status, run duration, next run date, if the job failed or not for a certain job category, TPA Conversion. My problem is that I have the following SQL statement, it runs, but it returns to many results. They want only one result per job. Anyone have any great ideas? Any help is appreciated. Thanks!
select Jobs.name as 'Job Name',
syscat.name as Category,
CASE sysjobschedules.freq_type
WHEN 1 THEN 'Once'
WHEN 4 THEN 'Daily'
WHEN 8 THEN 'Weekly'
WHEN 16 THEN 'Monthly'
end as 'Job Frequency',
MAX(run_date) AS 'Last Execution Date',
/* run_time, is really the time the job completed */
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),1,2) + ':' +
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),3,2) + ':' +
SUBSTRING(REPLICATE('0',6 - DATALENGTH(ltrim(CAST(run_time AS varchar(6))))) + ltrim(CAST(run_Time AS varchar(6))),5,2) as Completion_Time,
run_duration as "Duration(HHMMSS)",
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),1,3) * 3600) + /*convert hrs to seconds */
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),4,2) * 60) + /*convert mins to seconds */
(SUBSTRING(REPLICATE('0',7 - DATALENGTH(ltrim(CAST(run_duration AS varchar(7))))) + ltrim(CAST(run_duration AS varchar(7))),6,2)) AS Run_Seconds, /*convert seconds to number */
Case run_status
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
end as Outcome
FROM SQL1.msdb.dbo.sysjobschedules, SQL1.msdb.dbo.sysjobhistory HIST
INNER JOIN SQL1.msdb.dbo.sysjobs jobs on hist.job_id=jobs.job_id
INNER JOIN SQL1.msdb.dbo.syscategories syscat on jobs.category_id = syscat.category_id
WHERE syscat.name = 'TPA Conversion'
GROUP BY jobs.name, syscat.name, sysjobschedules.freq_type, hist.run_date, Hist.run_time, Hist.run_duration, Hist.run_status
Results:
BMA INFORMATION TPA Conversion Daily 20061214 22:00:00 4121 2481 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:00:00 14708 6428 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:41:22 400 240 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 22:45:23 5911 3551 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:44:35 19 19 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:44:54 206 126 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:47:01 2 2 Succeeded
BMA INFORMATION TPA Conversion Daily 20061214 23:47:03 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:00:00 4223 2543 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:00:00 14130 6090 Failed
BMA INFORMATION TPA Conversion Daily 20061215 22:42:24 446 286 Succeeded
BMA INFORMATION TPA Conversion Daily 20061215 22:47:10 5419 3259 Failed
BMA INFORMATION TPA Conversion Daily 20061217 12:19:27 111 71 Canceled
BMA INFORMATION TPA Conversion Daily 20061217 12:19:27 113 73 Canceled
BMA INFORMATION TPA Conversion Daily 20061217 22:00:00 14709 6429 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:00:01 4217 2537 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:42:19 452 292 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 22:47:11 5727 3447 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:44:40 17 17 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:44:57 207 127 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:47:04 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061217 23:47:09 0 0 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:00:00 14556 6356 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:00:01 4146 2506 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:41:48 445 285 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 22:46:34 5650 3410 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:43:25 18 18 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:43:43 205 125 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:45:48 2 2 Succeeded
BMA INFORMATION TPA Conversion Daily 20061218 23:45:50 5 5 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:00:00 4557 2757 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:00:00 15002 6602 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:45:58 428 268 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 22:50:28 5700 3420 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:47:29 20 20 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:47:49 205 125 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:49:54 4 4 Succeeded
BMA INFORMATION TPA Conversion Daily 20061219 23:49:58 4 4 Succeeded
BMA INFORMATION TPA Conversion Weekly 20061214 22:00:00 4121 2481 Succeeded
BMA INFORMATION TPA Conversion Weekly 20061214 22:00:00 14708 6428 Succeeded
etc...