在连接的选择中返回唯一的结果
我需要查询方面的帮助来检查 MSDB 数据库中的 SQL Server 代理作业结果。我的查询如下:
SELECT CONVERT(VARCHAR(30), Serverproperty('ServerName')),
a.run_status,
b.run_requested_date,
c.name,
CASE c.enabled
WHEN 1 THEN 'Enabled'
ELSE 'Disabled'
END,
CONVERT(VARCHAR(10), CONVERT(DATETIME, Rtrim(19000101))+(a.run_duration *
9 +
a.run_duration % 10000 * 6 + a.run_duration % 100 * 10) / 216e4, 108),
b.next_scheduled_run_date
FROM (msdb.dbo.sysjobhistory a
LEFT JOIN msdb.dbo.sysjobactivity b
ON b.job_history_id = a.instance_id)
JOIN msdb.dbo.sysjobs c
ON b.job_id = c.job_id
ORDER BY c.name
到目前为止一切顺利,但是运行它会返回相同作业的多个结果,具体取决于它们在查询之前运行的次数。这可不行。我只想要每项工作的一个结果,并且只需要最新的结果。
如果我添加字符串: WHERE b.session_id=(从 msdb.dbo.sysjobactivity 中选择 MAX(session_id)) 它的效果更好,但它只根据 session_id 参数列出最新的作业。这将排除已经有一段时间没有运行并且也不好的作业。
有人可以帮我解决这个问题吗? 我尝试过 DISTINCT 和/或 GROUP BY 但无法让它工作。
I need help with a query that check the MSDB-database for SQL Server Agent Job results. My query is as follows:
SELECT CONVERT(VARCHAR(30), Serverproperty('ServerName')),
a.run_status,
b.run_requested_date,
c.name,
CASE c.enabled
WHEN 1 THEN 'Enabled'
ELSE 'Disabled'
END,
CONVERT(VARCHAR(10), CONVERT(DATETIME, Rtrim(19000101))+(a.run_duration *
9 +
a.run_duration % 10000 * 6 + a.run_duration % 100 * 10) / 216e4, 108),
b.next_scheduled_run_date
FROM (msdb.dbo.sysjobhistory a
LEFT JOIN msdb.dbo.sysjobactivity b
ON b.job_history_id = a.instance_id)
JOIN msdb.dbo.sysjobs c
ON b.job_id = c.job_id
ORDER BY c.name
So far so good, but running it returns several results for the same jobs depending on how many times they have ran up until the query. This is no good. I want only one result per job, and only the latest.
If I add the the string:
WHERE b.session_id=(SELECT MAX(session_id) from msdb.dbo.sysjobactivity)
It works better, but then it only lists the latest jobs depending on the session_id parameter. This will exclude jobs that haven't run for a while and is not good either.
Can someone help me with this?
I have tried with DISTINCT and/or GROUP BY but can't get it to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)