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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Seems like a simple query....

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.fkDepartmentID
left outer join tblSoftware
on tblSoftware.fkMachineID = tblMachines.MachineID
where 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?



Brett

8-)
Go to Top of Page

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.
Go to Top of Page

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)





Brett

8-)
Go to Top of Page

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 t

and 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).
Go to Top of Page

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 Northwind
GO

SET NOCOUNT ON
CREATE 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))
GO

INSERT INTO mytblDepartment99(DepartmentID, DepartmentName)
SELECT 'A', 'Dept A' UNION ALL
SELECT 'B', 'Dept B'

INSERT INTO mytblMachines99(MachineID, MachineName, fkDepartmentID)
SELECT 'x', 'Machine x', 'A' UNION ALL
SELECT 'y', 'Machine y', 'B'

INSERT INTO mytblSoftware99(SoftwareID, Caption, Version, fkMachineID)
SELECT 't','Soft t',1,'x' UNION ALL
SELECT 'd','Soft d',1,'y' UNION ALL
SELECT 'e','Soft e',1,'y' UNION ALL
SELECT 'f','Soft f',1,'y'
GO



SELECT 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 XXX
CROSS 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)
GO

DROP TABLE mytblDepartment99
DROP TABLE mytblMachines99
DROP TABLE mytblSoftware99
GO





Brett

8-)
Go to Top of Page

degmo
Starting Member

4 Posts

Posted - 2005-03-22 : 17:32:02
That's what I was looking for.... Thanks.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-03-23 : 09:10:51
Great, but it doesn't work...

Brett

8-)
Go to Top of Page

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.DepartmentID
WHERE
not exists (select 'x' from tblSoftware c where b.machineid=c.fkMachineID and c.Caption='xxx')
Go to Top of Page
   

- Advertisement -