Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.