2017-10-20 122 views
0

我有一個查詢獲取有關特定作業的一些信息。顯示前1最大列

但是,我努力獲得每項工作的最高記錄(基於最新的'LastRun'字段)。

任何幫助我如何實現這一目標?

我的查詢是:

SELECT 
job.Name, 
CAST(CAST(hist.run_date AS CHAR(8)) 
+ ' ' 
+ STUFF(
STUFF(RIGHT('000000' + CAST(hist.run_time AS VARCHAR(6)), 6) 
, 3, 0, ':') 
, 6, 0, ':') 
AS DATETIME) AS LastRun, 
CASE hist.run_status 
    WHEN 0 THEN 'Failed' 
    WHEN 1 THEN 'Success' 
    WHEN 2 THEN 'Retry' 
    WHEN 3 THEN 'Canceled' 
    WHEN 4 THEN 'In progress' 
END AS Status, 
CASE job.enabled 
WHEN 0 THEN 'False' 
    WHEN 1 THEN 'True' 
END AS Enabled 
FROM dbo.sysjobhistory hist 
JOIN dbo.sysjobs_view job ON job.job_id=hist.job_id      
WHERE job.[name] = '[Parallel] Copy DBOutboxMaster' 
OR job.[name] = '[Parallel] Copy filestore to PPE' 
OR job.[name] = '[Parallel] copyLogShippingForECW' 
OR job.[name] = '[Parallel] copyLogShippingForNLASQL' 
GROUP BY job.name,hist.run_date,hist.run_time,hist.run_status,job.enabled 
ORDER BY lastrun DESC 

我目前得到的結果:

[Parallel] Copy DBOutboxMaster 2017-10-20 09:32:00.000 Success True 
[Parallel] Copy filestore to PPE 2017-10-20 09:32:00.000 Success True 
[Parallel] copyLogShippingForECW 2017-10-20 09:32:00.000 Success True 
[Parallel] copyLogShippingForNLASQL 2017-10-20 09:31:11.000 Success True 
[Parallel] Copy DBOutboxMaster 2017-10-20 09:31:00.000 Success True 

正如你可以看到有每個作業重複字段。我只想每個工作的最高記錄,所以總共應該只有4個最新LastRun日期時間的結果

+0

我不明白你的複製作業在哪裏 –

+0

從group by中刪除hist.run_time,在select中將其更改爲max(或min?)(hist.run_time)。那工作? –

+0

@RyanGadsdon查看結果中的第一條記錄和最後一條記錄。他們是一樣的工作 – Bodz

回答

0

感謝George Menoutis,對此的回答在以下查詢中,通過將運行日期和狀態欄:

SELECT 
job.Name, 
MAX(CAST(CAST(hist.run_date AS CHAR(8)) 
+ ' ' 
+ STUFF(
STUFF(RIGHT('000000' + CAST(hist.run_time AS VARCHAR(6)), 6) 
, 3, 0, ':') 
, 6, 0, ':') 
AS DATETIME)) AS LastRun, 
MAX(CASE hist.run_status 
    WHEN 0 THEN 'Failed' 
    WHEN 1 THEN 'Success' 
    WHEN 2 THEN 'Retry' 
    WHEN 3 THEN 'Canceled' 
    WHEN 4 THEN 'In progress' 
END) AS STATUS, 
CASE job.enabled 
WHEN 0 THEN 'False' 
    WHEN 1 THEN 'True' 
END AS Enabled 
FROM dbo.sysjobhistory hist 
JOIN dbo.sysjobs_view job ON job.job_id=hist.job_id      
WHERE job.[name] = '[Parallel] Copy DBOutboxMaster' 
OR job.[name] = '[Parallel] Copy filestore to PPE' 
OR job.[name] = '[Parallel] copyLogShippingForECW' 
OR job.[name] = '[Parallel] copyLogShippingForNLASQL' 
GROUP BY job.name,job.enabled 
ORDER BY lastrun DESC 

結果:

[Parallel] Copy DBOutboxMaster 2017-10-20 09:43:00.000 Success True 
[Parallel] Copy filestore to PPE 2017-10-20 09:43:00.000 Success True 
[Parallel] copyLogShippingForECW 2017-10-20 09:32:00.000 Success True 
[Parallel] copyLogShippingForNLASQL 2017-10-20 09:31:11.000 Success True 
0

使用TOP 1 WITH TIES + ROW_NUMBER

SELECT TOP 1 WITH TIES * 
FROM (
    SELECT job.[Name], 
      CAST(CAST(hist.run_date AS CHAR(8)) 
      + ' ' 
      + STUFF(
      STUFF(RIGHT('000000' + CAST(hist.run_time AS VARCHAR(6)), 6) 
      , 3, 0, ':') 
      , 6, 0, ':') 
      AS DATETIME) AS LastRun, 
      CASE hist.run_status 
       WHEN 0 THEN 'Failed' 
       WHEN 1 THEN 'Success' 
       WHEN 2 THEN 'Retry' 
       WHEN 3 THEN 'Canceled' 
       WHEN 4 THEN 'In progress' 
      END AS Status, 
      CASE job.enabled 
      WHEN 0 THEN 'False' 
       WHEN 1 THEN 'True' 
      END AS Enabled 
    FROM dbo.sysjobhistory hist 
    JOIN dbo.sysjobs_view job ON job.job_id=hist.job_id      
    WHERE job.[name] = '[Parallel] Copy DBOutboxMaster' 
     OR job.[name] = '[Parallel] Copy filestore to PPE' 
     OR job.[name] = '[Parallel] copyLogShippingForECW' 
     OR job.[name] = '[Parallel] copyLogShippingForNLASQL' 
    GROUP BY job.name,hist.run_date,hist.run_time,hist.run_status,job.enabled 
) as t 
ORDER BY ROW_NUMBER() OVER (PARTITION BY [Name] ORDER BY lastrun DESC) 
0

CROSS APPLY是 「每行頂部(n)的」 有用要求

SELECT 
     job.Name 
     , ca.LastRun 
     , ca.Status 
     , CASE job.enabled 
       WHEN 0 THEN 'False' 
       WHEN 1 THEN 'True' 
     END AS Enabled 
FROM dbo.sysjobs_view job 
CROSS APPLY (
     SELECT TOP(1) 
       dateadd(day,datediff(day,0,run_date),cast(run_time as datetime)) AS LastRun 
       , CASE hist.run_status 
         WHEN 0 THEN 'Failed' 
         WHEN 1 THEN 'Success' 
         WHEN 2 THEN 'Retry' 
         WHEN 3 THEN 'Canceled' 
         WHEN 4 THEN 'In progress' 
       END AS Status 
     FROM dbo.sysjobhistory hist 
     ORDER BY dateadd(day,datediff(day,0,run_date),cast(run_time as datetime)) DESC 
     WHERE hist.job_id = job.job_id 
     ) AS ca 
WHERE job.[name] = '[Parallel] Copy DBOutboxMaster' 
OR job.[name] = '[Parallel] Copy filestore to PPE' 
OR job.[name] = '[Parallel] copyLogShippingForECW' 
OR job.[name] = '[Parallel] copyLogShippingForNLASQL' 
ORDER BY ca.LastRun DESC 
; 

EDIT

要添加單獨的列的類型日期&時間在一起,一個簡單的/快速的方法是計算從零日期開始的天數,然後在將時間值轉換爲datetime之後將其添加到時間。

dateadd(day, datediff(day,0,run_date) ,cast(run_time as datetime)) 

使用基於數字的日期函數意味着你不依賴於服務器的設置來解釋字符串轉換爲日期,和他們比轉換/從字符串更快。