Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2012-05-11 : 17:41:35
|
I need to find data that is missing in a table. Below are two tables:DirRoleDefinitionsRoleDefID Name89 MED - Consult Resident90 MED - Consult Attending91 MED - Night Med Intern 1100 * GROUP: MEDDirContactRolesPersonID RoleDefID113390 89113390 91113390 100118901 90118901 100123999 100I need to find the Group MED and its corresponding Roles which start with MED and then figure out which Person is missing a role. Each Person should be assigned to all roles in the MED group. Based on the example above, the results I would get would be the following:PersonID RoleDefID113390 90118901 89118901 91123999 89123999 90123999 91123999 100I can get the RoleDefIDs in DirRoleDefinitions that are NOT IN DirContactRoles but I get null values for the PersonID. No matter what I do to the code, I can't quite seem to make the PersonIDs show up. select dirRoleDefinitions.RoleDefID, DirCOntactRoles.PersonIDfrom DirRoleDefinitionsleft outer join DirContactRoleson DirContactRoles.RoleDefID = DirROleDefinitions.RoleDefiDWHERE DirRoleDefinitions.RoleDefID NOT IN (select DirContactRoles.RoleDefID from DirContactRoles)Can anyone help? Thanks!SLReidForum NewbieRenton, WA USA |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-11 : 17:57:52
|
If you have reference tables that lists all the persons, it is a little easier - if not here is one way to query:SELECT b.PersonId, a.RoleDefIdFROM DirRoleDefinitions a CROSS JOIN ( SELECT DISTINCT PersonId FROM DirContactRoles ) bWHERE NOT EXISTS ( SELECT * FROM DirContactRoles d WHERE d.PersonId = b.PersonId AND d.RoleDefId = a.RoleRefId ) |
 |
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2012-05-18 : 19:51:07
|
Thanks much!SLReidForum NewbieRenton, WA USA |
 |
|
|
|
|