Need help composing a query looking for missing data

I have a sql table that contains a patient id column, an assignment type column and an assignment id column. a patient can have multiple assignments of different and same types. I need to come up with a query to search the table of any patients that do not have a certain assignment type assigned to them. For example an assignment type of 41 is a doctor, if no doctor has been assigned the row doesn't exist though. So patient A might have assignments types 38, 32, 4 and so on but if no doctor then the row for 41 does not even exist in the table. I need to find patient ID's that do not have that row.

It's Win Server 2008 R2 and sql 2008.
LVL 2
ScottJones74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Something like this...

SELECT P.*
FROM Patient AS P
LEFT OUTER JOIN Assignment AS A
   ON P.PatientID = A.PatientID
   AND A.AssignmentTypeID = 41
WHERE A.AssignmentID IS NULL
0
ScottJones74Author Commented:
I wrote it out like this:

SELECT *
FROM PT_BASIC AS P
LEFT OUTER JOIN PT_ASSIGNMENT AS A
   ON P.PATIENT_ID = A.PATIENT_ID
   AND A.ASSIGNMENT_ID = '41'
WHERE A.ASSIGNMENT_ID IS NULL

That returns all patients.  Can I use the "is null" at the end when the row will not exist is there is no 41?
0
Scott PletcherSenior DBACommented:
SELECT P.*
FROM PT_BASIC AS P
INNER JOIN (
    SELECT PATIENT_ID
    FROM PT_ASSIGNMENT
    GROUP BY PATIENT_ID
    HAVING MAX(CASE WHEN ASSIGNMENT_ID = '41' THEN 1 ELSE 0 END) = 0
) AS A41 ON A41.PATIENT_ID = P.PATIENT_ID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ScottJones74Author Commented:
That may be gettign us closer.  It returns patients that have a type 41 assigned to them, BUT the number of patients the 2nd query returned is about 266 patients less than what the first query gave us.  I am thinking that those are the records we need, just need to know how to show those versus the ones we are getting
0
Scott PletcherSenior DBACommented:
Is ASSIGNMENT_ID numeric (integer or similar) or char/varchar?

The query I wrote can't return a patient that has an assignment id equal to '41'.
0
ScottJones74Author Commented:
Scott, that was it.  I had the column name wrong in your query and now I have all the patients.  Thank you very much!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.