Problem selecting rows that have only one assigned status

Posted on 2007-11-14
Last Modified: 2010-03-20

I am trying to modify an SQL statement to retrieve teachers that have only one course listed and whose status is cancelled and type is fulltime.
(i.e. those fulltime teachers that are listed against ONE course that is cancelled)

course_id   course_name        teacher_id      teacher_type      status
001            French               Barry           fulltime               cancelled
002            English                Mary            fulltime                available
003            German              Mary                fulltime             cancelled
004             History                Colm             fulltime                postponed
005            Art                    Barry               parttime           cancelled

This SQL works for the most part, but will return a teacher_id that is listed against 2 cancelled courses (not allowed!)

select distinct teacher_id
from temp
where status='cancelled' and teacher_type='fulltime'
select teacher_id
from temp
where status in ('available', 'postponed')

Any suggestions ?
Question by:barney75
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Accepted Solution

MikeToole earned 100 total points
ID: 20278980
Off the top of my head, this should so it:

select teacherID
From temp
Where status='cancelled' and teacher_type='fulltime'
Group By teacherID
Having count(teacherID) = 1
LVL 18

Expert Comment

ID: 20279071
select distinct teacher_id
from temp
where status='cancelled' and teacher_type='fulltime'
    select teacher_id
    from temp
    where status in ('available', 'postponed')
    select teacher_id
    from temp
    where status = 'cancelled'
    and teacher_type='fulltime'
    group by teacher_id
    having count(*) > 1

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question