Left Join Null Results

  • I have an issue trying to complile results on staff training, show who has and who hasn't undertaken certain training courses.

    This is what I'm doing at the moment

    Select a.trainingcourse, c.name, c.personID, b.expirydate

    from trainingcourses a

    left join trainingcourselog b on b.courseID = a.courseID

    left join staff c on c.staffID = b.staffID and c.staffID = '1234'

    where a.courseID in ('id1','id2','id3')

    The results I get...

    Training 1 John Smith 1234 05/08/2012

    Training 2 John Smith 1234 05/08/2012

    Training 3 null null null

    As John Smith hasn't done Training Course 3 it appears as null

    What I want to get is...

    Training 1 John Smith 1234 05/08/2012

    Training 2 John Smith 1234 05/08/2012

    Training 3 John Smith 1234 null

    I need to get the persons name and id to appear against the training courses they haven't done.

    Any ideas?

  • To provide you with the best help possible we really need more information. Please read and follow the instructions in the first article I reference below in my signature block. It will walk you through what to post and how to post it. With that information you will get much better answers to your questions.

  • If you would provide the setup (ddl & sample data) I could test my query, but...

    Select a.trainingcourse, c.name, c.personID, b.expirydate

    from trainingcourses a

    cross join staff c

    left join trainingcourselog b on b.courseID = a.courseID

    and b.staffID = c.staffID

    where a.courseID in ('id1','id2','id3')

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I don't have access to a SQL Server right now, so I'm not able to test if this will work, or if it performs very well, but I'll give it a try anyway.

    Select a.trainingcourse, c.name, c.personID, b.expirydate

    from trainingcourses a

    cross join staff c

    left join trainingcourselog b on b.courseID = a.courseID and c.staffId=b.staffId

    where a.courseID in ('id1','id2','id3') and c.staffID = '1234'

    No guarantee that this will compile.

  • Nils Gustav Stråbø (6/12/2012)


    I don't have access to a SQL Server right now, so I'm not able to test if this will work, or if it performs very well, but I'll give it a try anyway.

    Select a.trainingcourse, c.name, c.personID, b.expirydate

    from trainingcourses a

    cross join staff c

    left join trainingcourselog b on b.courseID = a.courseID and c.staffId=b.staffId

    where a.courseID in ('id1','id2','id3') and c.staffID = '1234'

    No guarantee that this will compile.

    That worked a treat, thank you.

    Sorry for not posting the code in the correct way. Haven't posted much on here and was in a bit of a rush. Will remember for next time.

    Thanks again.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply