Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to join to MSSQL queries, but I am not getting anywhere with this.

Both queries return a single value:

 SELECT COUNT(*) FROM sysjobhistory WHERE run_status <> 1 AND step_id = 0;

 SELECT MAX (run_date) FROM sysjobhistory WHERE run_status = 1 AND step_id = 0;

I am a total SQL noob so any help is appreciated

share|improve this question
    
Do you want both values returned in a single query from the sysjobhistory table? – Milica Medic Jun 9 '14 at 23:21
up vote 1 down vote accepted

It's hard to tell what you want the output to be, but I think this will help you:

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

There are other ways to do this, including using a JOIN, but I believe the above is easier to understand, particularly for beginners.

share|improve this answer
    
Thanks, this will do. All I needed was to have both values returned from one query. It is for a nagios plugin, that needs both values to perform its check – Bruno9779 Jun 10 '14 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .