Solved

Problem selecting rows that have only one assigned status

Posted on 2007-11-14
2
201 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
[X]
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
2 Comments
 
LVL 27

Accepted Solution

by:
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
0
 
LVL 18

Expert Comment

by:JR2003
ID: 20279071
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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