Solved

Problem selecting rows that have only one assigned status

Posted on 2007-11-14
2
165 Views
Last Modified: 2010-03-20
Hi,

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'
minus
select teacher_id
from temp
where status in ('available', 'postponed')

Any suggestions ?
0
Comment
Question by:barney75
2 Comments
 
LVL 27

Accepted Solution

by:
MikeToole earned 100 total points
Comment Utility
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
0
 
LVL 18

Expert Comment

by:JR2003
Comment Utility
select distinct teacher_id
from temp
where status='cancelled' and teacher_type='fulltime'
minus
(
    select teacher_id
    from temp
    where status in ('available', 'postponed')
    union
    select teacher_id
    from temp
    where status = 'cancelled'
    and teacher_type='fulltime'
    group by teacher_id
    having count(*) > 1
)
)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now