Solved

Problem selecting rows that have only one assigned status

Posted on 2007-11-14
2
194 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
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 49
Sql server insert 13 36
Query Help - MSSQL - Averages 5 30
MS SQL Server time between records 14 46
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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