在连接的选择中返回唯一的结果

发布于 2024-10-16 13:55:10 字数 996 浏览 6 评论 0原文

我需要查询方面的帮助来检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-23 13:55:10
with cte
     AS (SELECT
     Convert(varchar(30), SERVERPROPERTY('ServerName')) AS ServerName,
     a.run_status,
     b.run_requested_date,
     c.name,
     CASE c.enabled
       WHEN 1 THEN 'Enabled'
       Else 'Disabled'
     END
        AS Enabled,
     CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))+(a.run_duration
        * 9 +
     a.run_duration % 10000 * 6 + a.run_duration % 100 * 10) / 216e4, 108)
        AS run_duration,
     b.next_scheduled_run_date,
     ROW_NUMBER() over (partition by b.job_id ORDER BY b.run_requested_date
        DESC) AS RN
         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)
SELECT *
FROM   cte
WHERE  RN = 1
ORDER  BY name  
with cte
     AS (SELECT
     Convert(varchar(30), SERVERPROPERTY('ServerName')) AS ServerName,
     a.run_status,
     b.run_requested_date,
     c.name,
     CASE c.enabled
       WHEN 1 THEN 'Enabled'
       Else 'Disabled'
     END
        AS Enabled,
     CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))+(a.run_duration
        * 9 +
     a.run_duration % 10000 * 6 + a.run_duration % 100 * 10) / 216e4, 108)
        AS run_duration,
     b.next_scheduled_run_date,
     ROW_NUMBER() over (partition by b.job_id ORDER BY b.run_requested_date
        DESC) AS RN
         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)
SELECT *
FROM   cte
WHERE  RN = 1
ORDER  BY name  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文