2013-05-21 62 views
0

我不是一個SQL專家,但我有此查詢複雜的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

嘗試像這樣...

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 
+0

艾米特嗨,我剛剛運行的查詢,並沒有返回任何結果。 –

+0

@BhaskarMishra現在嘗試我更新我的答案! –

+0

沒有行被返回。 –