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 |
|
sqlprog
Starting Member
2 Posts |
Posted - 2007-08-02 : 11:06:06
|
| I have two tables. One with employee records, the other with a combination of department and occupation used to assign a number to each employee.How do I find occupation/department combinations in employee table that do not have a number assigned to them in the combo table(that is, have not been entered in table 2? |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-08-02 : 11:40:45
|
Something like:SELECT *FROM Employee EWHERE NOT EXISTS ( SELECT * FROM DepartmentOccupation DO WHERE DO.DepartmentID = E.DepartmentID AND DO.OccupationID = E.OccupationID ) or SELECT E.*FROM Employee E LEFT JOIN DepartmentOccupation DO ON E.DepartmentID = DO.DepartmentID AND E.OccupationID = DO.OccupationIDWHERE DO.DepartmentID IS NULL |
 |
|
|
sqlprog
Starting Member
2 Posts |
Posted - 2007-08-02 : 14:20:25
|
| Thanks, Ifor, for your quick reply. It works great.But, my query is a little more involved. The occupation in DepartmentOccupation (DO) table is the parent occupation from occupation table. So, basically, Employee.OccupationID=Occupation.OccupationIDDO.OccupationID=Occupation.ParentOccupationIDI need to see what occupation/department combos haevn't been added to the DO table yet. Here is my query below. Unfortunately, it includes some combos that are already there in the DO table. I cannot figure out why. Can yall help?select distinct Employee.OccupationID, Occupation.ParentOccupationID,Occ2.OccupationDesc ParentOcc,Occupation.OccupationDesc, Employee.DepartmentID, Department.DepartmentDescriptionfrom Employee, Occupation, Department, Occupation Occ2whereEmployee.DepartmentID = Department.DepartmentIDand Employee.OccupationID = Occupation.OccupationIDand Occupation.ParentOccupationID = Occ2.OccupationIDand not exists (select 1 from DepartmentOccupation DO where DO.OccupationID = Occupation.ParentOccupationID and DO.DepartmentID = Employee.DepartmentID) Thanks in advance. Stuck for too long on this crazy query! |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2007-08-03 : 04:54:56
|
It is difficult to tell what you want without DDL, sample data and expected results.Maybe the following will help you:-- To ViewSELECT DISTINCT E.OccupationID ,O.ParentOccupationID ,O.OccupationDesc ,P.OccupationDesc ,E.DepartmentID ,D.DepartmentDescriptionFROM Employee E JOIN Occupation O ON E.OccupationID = O.OccupationID JOIN Department D ON E.DepartmentID = D.DepartmentID JOIN Occupation P ON O.ParentOccupationID = P.OccupationIDWHERE NOT EXISTS ( SELECT * FROM DepartmentOccupation DO WHERE DO.DepartmentID = E.DepartmentID AND DO.OccupationID = O.ParentOccupationID )-- To InsertINSERT INTO DepartmentOccupation (DepartmentID, OccupationID)SELECT DISTINCT ,E.DepartmentID ,O.ParentOccupationIDFROM Employee E JOIN Occupation O ON E.OccupationID = O.OccupationIDWHERE NOT EXISTS ( SELECT * FROM DepartmentOccupation DO WHERE DO.DepartmentID = E.DepartmentID AND DO.OccupationID = O.ParentOccupationID ) |
 |
|
|
|
|
|
|
|