需要帮助以获取最大时间

时间:2018-09-24 16:12:03

标签: sql

SELECT
    jobname, stepname, ExecutionStatus, runtime 
FROM 
    (SELECT DISTINCT
         j.name JobName, h.step_name StepName, 
         CONVERT CHAR(10), CAST(STR(h.run_date, 8, 0) AS dateTIME), 101)  RunDate, 
         STUFF(STUFF(RIGHT('000000' + CAST(h.run_time AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') RunTime,
         CASE h.run_status 
            WHEN 0 THEN 'FAILED'
            WHEN 1 THEN 'Succeeded' 
            WHEN 2 THEN 'Retry' 
            WHEN 3 THEN 'Cancelled' 
            WHEN 4 THEN 'In Progress' 
         END AS ExecutionStatus 
     FROM 
         msdb.dbo.sysjobhistory h 
     INNER JOIN 
         [msdb].[dbo].[sysjobs] j ON j.job_id = h.job_id
     WHERE 
         j.name NOT LIKE '%PDM %' 
         AND j.name NOT LIKE '%PDM_Push%' 
         AND j.name NOT LIKE '%&PDM_%' 
         AND j.name NOT LIKE '%CDM%') AS IJ
WHERE 
    RunDate >=CAST(GETDATE() AS DATE)
    AND ExecutionStatus = 'FAILED' 
    AND stepname NOT LIKE '(job outcome)'
    AND RunTime > CONVERT(VARCHAR(8), DATEADD (HOUR , -4 , GETDATE() ), 108)
GROUP BY
    ij.jobname, IJ.StepName, ij.RunDate, IJ.RunTime, IJ.ExecutionStatus
ORDER BY
    rundate DESC

我有这个查询,结果给出多个结果,如下所示:

jobname stepname    ExecutionStatus (No column name)
sp_who2 logging sp_whoisactive  FAILED  13:08:30
sp_who2 logging sp_whoisactive  FAILED  13:11:30
sp_who2 logging sp_whoisactive  FAILED  13:20:30
sp_who2 logging sp_whoisactive  FAILED  13:23:30
sp_who2 logging sp_whoisactive  FAILED  13:26:30
sp_who2 logging sp_whoisactive  FAILED  13:29:31
sp_who2 logging sp_whoisactive  FAILED  13:38:30
sp_who2 logging sp_whoisactive  FAILED  13:41:30
sp_who2 logging sp_whoisactive  FAILED  13:44:31
sp_who2 logging sp_whoisactive  FAILED  13:53:30
sp_who2 logging sp_whoisactive  FAILED  13:56:30
sp_who2 logging sp_whoisactive  FAILED  13:59:31
sp_who2 logging sp_whoisactive  FAILED  14:08:30
sp_who2 logging sp_whoisactive  FAILED  14:29:30
sp_who2 logging sp_whoisactive  FAILED  14:32:31
sp_who2 logging sp_whoisactive  FAILED  14:35:30
sp_who2 logging sp_whoisactive  FAILED  14:38:30
sp_who2 logging sp_whoisactive  FAILED  14:41:30
sp_who2 logging sp_whoisactive  FAILED  14:44:31
sp_who2 logging sp_whoisactive  FAILED  14:47:30
sp_who2 logging sp_whoisactive  FAILED  14:50:30
sp_who2 logging sp_whoisactive  FAILED  14:53:30
sp_who2 logging sp_whoisactive  FAILED  14:56:31
sp_who2 logging sp_whoisactive  FAILED  14:59:30
sp_who2 logging sp_whoisactive  FAILED  15:02:30
sp_who2 logging sp_whoisactive  FAILED  15:05:31
sp_who2 logging sp_whoisactive  FAILED  15:08:30
sp_who2 logging sp_whoisactive  FAILED  15:11:30
sp_who2 logging sp_whoisactive  FAILED  15:14:30
sp_who2 logging sp_whoisactive  FAILED  15:17:30
sp_who2 logging sp_whoisactive  FAILED  15:20:30
sp_who2 logging sp_whoisactive  FAILED  15:23:30
sp_who2 logging sp_whoisactive  FAILED  15:26:30
sp_who2 logging sp_whoisactive  FAILED  15:29:30
sp_who2 logging sp_whoisactive  FAILED  15:32:30
sp_who2 logging sp_whoisactive  FAILED  15:35:30
sp_who2 logging sp_whoisactive  FAILED  15:38:30
sp_who2 logging sp_whoisactive  FAILED  15:41:31
sp_who2 logging sp_whoisactive  FAILED  15:44:31
sp_who2 logging sp_whoisactive  FAILED  15:47:30
sp_who2 logging sp_whoisactive  FAILED  15:50:30
sp_who2 logging sp_whoisactive  FAILED  15:53:31
sp_who2 logging sp_whoisactive  FAILED  15:56:30
sp_who2 logging sp_whoisactive  FAILED  15:59:30
sp_who2 logging sp_whoisactive  FAILED  16:02:30
sp_who2 logging sp_whoisactive  FAILED  16:05:30
sp_who2 logging sp_whoisactive  FAILED  16:08:30
sp_who2 logging sp_whoisactive  FAILED  16:11:31
sp_who2 logging sp_whoisactive  FAILED  16:14:30
sp_who2 logging sp_whoisactive  FAILED  16:17:30
sp_who2 logging sp_whoisactive  FAILED  16:20:30
sp_who2 logging sp_whoisactive  FAILED  16:23:30
sp_who2 logging sp_whoisactive  FAILED  16:26:31
sp_who2 logging sp_whoisactive  FAILED  16:29:30
sp_who2 logging sp_whoisactive  FAILED  16:32:30
sp_who2 logging sp_whoisactive  FAILED  16:35:30
sp_who2 logging sp_whoisactive  FAILED  16:38:30
sp_who2 logging sp_whoisactive  FAILED  16:41:30
sp_who2 logging sp_whoisactive  FAILED  16:44:31
sp_who2 logging sp_whoisactive  FAILED  16:47:30
sp_who2 logging sp_whoisactive  FAILED  16:50:30
sp_who2 logging sp_whoisactive  FAILED  16:53:30
sp_who2 logging sp_whoisactive  FAILED  16:56:30
sp_who2 logging sp_whoisactive  FAILED  16:59:30

我只想要工作名称的最长时间,该怎么办?

1 个答案:

答案 0 :(得分:0)

此查询显示每个作业的最新状态。

SELECT 
    NOME AS NAME,
    STEP,
    STATUS,
    DT_INICIO AS DATE,
    DURACAO AS DURATION,
    SERVIDOR
FROM (SELECT 
    B.NAME AS NOME,
    C.STEP_NAME AS STEP,
    CASE 
WHEN RUN_STATUS = 0 THEN 'FALHA'
WHEN RUN_STATUS = 1 THEN 'SUCEDIDO'
WHEN RUN_STATUS = 3 THEN 'CANCELADA'
WHEN RUN_STATUS = 4 THEN 'EXECUTANDO' ELSE '' END [STATUS],
    CONVERT(DATETIME,CONVERT(VARCHAR,CONVERT(DATETIME,CONVERT(VARCHAR,RUN_DATE)),103) + ' ' +
CASE LEN(H.RUN_TIME)
WHEN 1 THEN CAST('00:00:0' + CAST(H.RUN_TIME AS CHAR) AS CHAR (8))
WHEN 2 THEN CAST('00:00:' + CAST(H.RUN_TIME AS CHAR) AS CHAR (8))
WHEN 3 THEN CAST('00:0' + LEFT(RIGHT(H.RUN_TIME,3),1) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 4 THEN CAST('00:' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 5 THEN CAST('0' + LEFT(RIGHT(H.RUN_TIME,5),1) +':' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 6 THEN CAST(LEFT(RIGHT(H.RUN_TIME,6),2) +':' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8)) END,103) AS DT_INICIO,

CONVERT(VARCHAR(10),CONVERT(DATETIME,CONVERT(VARCHAR,CONVERT(DATETIME,CONVERT(VARCHAR,RUN_DATE)),103) + ' ' +
CASE LEN(H.RUN_DURATION)
WHEN 1 THEN CAST('00:00:0' + CAST(H.RUN_DURATION AS CHAR) AS CHAR (8))
WHEN 2 THEN CAST('00:00:' + CAST(H.RUN_DURATION AS CHAR) AS CHAR (8))
WHEN 3 THEN CAST('00:0' + LEFT(RIGHT(H.RUN_DURATION,3),1) +':' + RIGHT(H.RUN_DURATION,2) AS CHAR (8))
WHEN 4 THEN CAST('00:' + LEFT(RIGHT(H.RUN_DURATION,4),2) +':' + RIGHT(H.RUN_DURATION,2) AS CHAR (8))
WHEN 5 THEN CAST('0' + LEFT(RIGHT(H.RUN_DURATION,5),1) +':' + LEFT(RIGHT(H.RUN_DURATION,4),2) +':' + RIGHT(H.RUN_DURATION,2) AS CHAR (8))
WHEN 6 THEN CAST(LEFT(RIGHT(H.RUN_DURATION,6),2) +':' + LEFT(RIGHT(H.RUN_DURATION,4),2) +':' + RIGHT(H.RUN_DURATION,2) AS CHAR (8)) END,103),108) AS DURACAO,
(SELECT CONVERT(VARCHAR,RB) + CASE WHEN (SELECT CONVERT(VARCHAR,RA)) IS NULL THEN '' ELSE '\'+ (SELECT CONVERT(VARCHAR,RA)) END 
FROM (SELECT SERVERPROPERTY('InstanceName') AS RA, ConnectionProperty('local_net_address') AS RB) AS R
) SERVIDOR,
ROW_NUMBER() OVER(PARTITION BY B.NAME,C.STEP_NAME ORDER BY 
    CONVERT(DATETIME,CONVERT(VARCHAR,CONVERT(DATETIME,CONVERT(VARCHAR,RUN_DATE)),103) + ' ' +
CASE LEN(H.RUN_TIME)
WHEN 1 THEN CAST('00:00:0' + CAST(H.RUN_TIME AS CHAR) AS CHAR (8))
WHEN 2 THEN CAST('00:00:' + CAST(H.RUN_TIME AS CHAR) AS CHAR (8))
WHEN 3 THEN CAST('00:0' + LEFT(RIGHT(H.RUN_TIME,3),1) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 4 THEN CAST('00:' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 5 THEN CAST('0' + LEFT(RIGHT(H.RUN_TIME,5),1) +':' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8))
WHEN 6 THEN CAST(LEFT(RIGHT(H.RUN_TIME,6),2) +':' + LEFT(RIGHT(H.RUN_TIME,4),2) +':' + RIGHT(H.RUN_TIME,2) AS CHAR (8)) END,103) DESC) AS D

FROM MSDB.DBO.SYSJOBHISTORY H
JOIN MSDB.DBO.SYSJOBS B
ON H.JOB_ID = B.JOB_ID
JOIN MSDB.DBO.SYSJOBSTEPS C
ON H.JOB_ID = C.JOB_ID
AND H.STEP_ID = C.STEP_ID
WHERE RUN_DATE = CONVERT(VARCHAR(8),GETDATE(),112) ) AS SRC
WHERE D = 1