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 |
|
degmo
Starting Member
4 Posts |
Posted - 2005-03-22 : 15:07:38
|
| I'm working on a query and I can't quite get the results I want. I have three tables as follows.tblDepartment(DepartmentID, DepartmentName)tblMachines(MachineID, MachineName, fkDepartmentID)tblSoftware(SoftwareID, Caption, Version, fkMachineID)A department can have multiple machine and each machine can have multiple softwares installed on it.I am attempting to write a query that returns me a list of machines that don't have a specific software installed on them. The result set should have the "DepartmentID, DepartmentName, MachineName".Here is what I have, but it doesn't work:select distinct tblDepartment.DepartmentID, tblDepartment.DepartmentName, tblMachines.MachineName from tblDepartment left outer join tblMachines on tblDepartment.DepartmentID = tblMachines.fkDepartmentIDleft outer join tblSoftware on tblSoftware.fkMachineID = tblMachines.MachineIDwhere not exists (select * from tblSoftware where tblSoftware.fkMachineID = tblMachines.MachineID and Caption = 'xxx')order by DepartmentID asc |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-22 : 15:17:18
|
| Do you have a table that is a unique list of software titles without the MachineID?Brett8-) |
 |
|
|
degmo
Starting Member
4 Posts |
Posted - 2005-03-22 : 15:22:26
|
| no I don't. It would have made it easier. But I'm working with an already existing database structure. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-22 : 15:48:22
|
Not tested SELECT DISTINCT a.DepartmentID, a.DepartmentName, b.MachineName FROM tblDepartment a INNER JON tblMachines b ON a.DepartmentID = b.DepartmentID LEFT JOIN tblSoftware c ON b.MachineId = c.fkMachineID WHERE NOT EXISTS (SELECT DISTICT SoftwareID, Caption, Version FROM tblSoftware d WHERE c.SoftwareID = d.SoftwareID) Brett8-) |
 |
|
|
degmo
Starting Member
4 Posts |
Posted - 2005-03-22 : 16:09:24
|
| Brett,That's essentially what I had. But,say Department A has machine x that has software tand Department B has mahcine y that has software(d,e,f)If I run the query and attempt to return the department machine that doesn't have the software 't', I get three rows(for d, e, and f). |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-22 : 16:57:22
|
This is bugging the hell out of me...run it with out the existance check to see the reults...USE NorthwindGOSET NOCOUNT ONCREATE TABLE mytblDepartment99(DepartmentID char(1), DepartmentName varchar(10))CREATE TABLE mytblMachines99(MachineID char(1), MachineName varchar(10), fkDepartmentID char(1))CREATE TABLE mytblSoftware99(SoftwareID char(1), Caption varchar(10), Version int, fkMachineID char(1))GOINSERT INTO mytblDepartment99(DepartmentID, DepartmentName)SELECT 'A', 'Dept A' UNION ALLSELECT 'B', 'Dept B'INSERT INTO mytblMachines99(MachineID, MachineName, fkDepartmentID)SELECT 'x', 'Machine x', 'A' UNION ALLSELECT 'y', 'Machine y', 'B'INSERT INTO mytblSoftware99(SoftwareID, Caption, Version, fkMachineID)SELECT 't','Soft t',1,'x' UNION ALLSELECT 'd','Soft d',1,'y' UNION ALLSELECT 'e','Soft e',1,'y' UNION ALLSELECT 'f','Soft f',1,'y'GOSELECT DISTINCT xxx.DepartmentID, xxx.DepartmentName, xxx.MachineName, d.SoftwareID AS MissingSoftware FROM ( SELECT DISTINCT a.DepartmentID, a.DepartmentName, b.MachineID, b.MachineName, c.SoftwareID FROM mytblDepartment99 a INNER JOIN mytblMachines99 b ON a.DepartmentID = b.fkDepartmentID INNER JOIN mytblSoftware99 c ON b.MachineId = c.fkMachineID) AS XXXCROSS JOIN (SELECT DISTINCT SoftwareID FROM mytblSoftware99) AS d WHERE XXX.SoftwareId <> d.SoftwareID AND NOT EXISTS (SELECT * FROM mytblSoftware99 e WHERE e.fkMachineId = xxx.MachineID AND e.SoftwareId = xxx.SoftwareID)GODROP TABLE mytblDepartment99DROP TABLE mytblMachines99DROP TABLE mytblSoftware99GO Brett8-) |
 |
|
|
degmo
Starting Member
4 Posts |
Posted - 2005-03-22 : 17:32:02
|
| That's what I was looking for.... Thanks. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-23 : 09:10:51
|
| Great, but it doesn't work...Brett8-) |
 |
|
|
bclarki7
Starting Member
1 Post |
Posted - 2005-03-25 : 13:40:21
|
| Here are two ways of doing it...both ought to return the same answer.SELECT a.DepartmentID, a.DepartmentName, b.MachineName FROM tblDepartment a INNER JON tblMachines b ON a.DepartmentID = b.DepartmentID LEFT JOIN tblSoftware c ON b.MachineId = c.fkMachineID and c.Caption = 'xxx'WHERE c.softwareid is null SELECT a.DepartmentID, a.DepartmentName, b.MachineName FROM tblDepartment a INNER JON tblMachines b ON a.DepartmentID = b.DepartmentIDWHERE not exists (select 'x' from tblSoftware c where b.machineid=c.fkMachineID and c.Caption='xxx') |
 |
|
|
|
|
|
|
|