我不是SQL专家,但我有这个查询
USE msdb
DECLARE
@JobStepCount int,
@JobName sysname,
@JobID uniqueidentifier
DECLARE @JobStatus TABLE (JobStatusID int, JobStatus varchar(20))
INSERT INTO @JobStatus (JobStatusID, JobStatus)
VALUES (0, 'Failed'),
(1, 'Succeeded'),
(2, 'Retry'),
(3, 'Cancelled')
SET @JobName = 'Expedient BookingRefresh'
SELECT
@JobID = job_id
FROM
sysjobs j
where
j.name = @JobName
SELECT
@JobStepCount = COUNT(*)
FROM
dbo.sysjobsteps
WHERE
job_id = @JobID
SELECT
RunDate,
RunTime,
RunDuration,
JobStatus
FROM
( SELECT
jstart.instance_id,
RunDate,
RunTime,
RunDuration,
MIN(h.run_status) as run_status
FROM
sysjobhistory h
inner join
(
SELECT
instance_id,
jstart.instance_id - @JobStepCount AS instance_range,
jstart.run_date AS RunDate,
CONVERT(time, LEFT(jstart.run_time, 2) + ':' + SUBSTRING(jstart.run_time, 3, 2) + ':' + RIGHT(jstart.run_time, 2)) AS RunTime,
CONVERT(time, LEFT(jstart.run_duration, 2) + ':' + SUBSTRING(jstart.run_duration, 3, 2) + ':' + RIGHT(jstart.run_duration, 2)) AS RunDuration
FROM
( SELECT
instance_id,
CONVERT(date, CONVERT(VARCHAR(8), run_date), 112) AS run_date,
RIGHT('000000' + CONVERT(VARCHAR(6), run_time), 6) AS run_time,
RIGHT('000000' + CONVERT(VARCHAR(6), run_duration), 6) AS run_duration
FROM
sysjobhistory hstart
WHERE
step_id = 0
AND
job_id = @JobID
) jstart
) jstart
on
h.instance_id between jstart.instance_range and jstart.instance_id
AND
h.job_id = @JobID
GROUP BY
jstart.instance_id,
RunDate,
RunTime,
RunDuration
) a
INNER JOIN
@JobStatus js
ON
js.JobStatusID = a.run_status
ORDER BY
RunDate, RunTime
我得到的结果如下:
2013-05-09 02:15:44.0000000 00:14:46.0000000 Succeeded
2013-05-09 02:56:17.0000000 23:18:25.0000000 Succeeded
2013-05-10 06:00:00.0000000 01:56:18.0000000 Cancelled
我正在寻找这样的结果:
Date Number of runs Success Count Failure/Cancel Count
2013-05-09 2 2 0
2013-05-10 1 0 1
我尝试了一些组合来总结日期,并希望能够以所需的格式获得结果。有人可以帮助我吗?
感谢。
答案 0 :(得分:0)
尝试这样......
SELECT
cAST(RunDate AS dATE),
Count(*) as Number_of_Run,
Sum(Case When JobStatus='Succeeded' then 1 else 0 end) as Success_Count,
Sum(Case When JobStatus='Cancelled' or JobStatus='Failure' then 1 else 0 end) as FailureCancelCount
FROM --I M USING THE tABLE wHICH you mentioned in your question......
(SELECT
RunDate,
RunTime,
RunDuration,
JobStatus
FROM
( SELECT
jstart.instance_id,
RunDate,
RunTime,
RunDuration,
MIN(h.run_status) as run_status
FROM
sysjobhistory h
inner join
(
SELECT
instance_id,
jstart.instance_id - @JobStepCount AS instance_range,
jstart.run_date AS RunDate,
CONVERT(time, LEFT(jstart.run_time, 2) + ':' + SUBSTRING(jstart.run_time, 3, 2) + ':' + RIGHT(jstart.run_time, 2)) AS RunTime,
CONVERT(time, LEFT(jstart.run_duration, 2) + ':' + SUBSTRING(jstart.run_duration, 3, 2) + ':' + RIGHT(jstart.run_duration, 2)) AS RunDuration
FROM
( SELECT
instance_id,
CONVERT(date, CONVERT(VARCHAR(8), run_date), 112) AS run_date,
RIGHT('000000' + CONVERT(VARCHAR(6), run_time), 6) AS run_time,
RIGHT('000000' + CONVERT(VARCHAR(6), run_duration), 6) AS run_duration
FROM
sysjobhistory hstart
WHERE
step_id = 0
AND
job_id = @JobID
) jstart
) jstart
on
h.instance_id between jstart.instance_range and jstart.instance_id
AND
h.job_id = @JobID
GROUP BY
jstart.instance_id,
RunDate,
RunTime,
RunDuration
) a
INNER JOIN
@JobStatus js
ON
js.JobStatusID = a.run_status
ORDER BY
RunDate, RunTime) t
group by RunDate
ORDER BY RunDate