Answers:
很难说出你想要的输出是什么,但我认为这会对你有所帮助:
DECLARE @Count INT
DECLARE @MaxRunDate DateTime -- I'm guessing run_date is a datetime...
SELECT @Count = COUNT(*)
FROM sysjobhistory WHERE run_status <> 1 AND step_id = 0;
SELECT @MaxRunDate = MAX(run_date)
FROM sysjobhistory WHERE run_status = 1 AND step_id = 0;
SELECT @Count, @MaxRunDate
还有其他方法可以做到这一点,包括使用JOIN,但我相信上面的内容更容易理解,特别是初学者。
如果要联合,请为列命名:
SELECT COUNT(*) AS 'Column' FROM sysjobhistory WHERE run_status <> 1 AND step_id = 0
UNION
SELECT MAX (run_date) AS 'Column' FROM sysjobhistory WHERE run_status = 1 AND step_id = 0
如果您想在同一查询中使用两列,则可以使用子查询:
SELECT (SELECT COUNT(*) FROM sysjobhistory WHERE run_status <> 1 AND step_id = 0) AS 'Count', (SELECT MAX (run_date) AS 'Column' FROM sysjobhistory WHERE run_status = 1 AND step_id = 0) AS 'Max'