我有一個查詢獲取有關特定作業的一些信息。顯示前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日期時間的結果
我不明白你的複製作業在哪裏 –
從group by中刪除hist.run_time,在select中將其更改爲max(或min?)(hist.run_time)。那工作? –
@RyanGadsdon查看結果中的第一條記錄和最後一條記錄。他們是一樣的工作 – Bodz