Really dumb SQl question.

Status
Not open for further replies.

BradTheGeek

Ars Tribunus Militum
1,878
Subscriptor
I am a hardware guy - but I do understand basics - so please bear with me.
I have a data set, sample schema here - http://sqlfiddle.com/#!9/5d9bdd/4

Need to find missing items. For example, all devices (except toms) are missing "antivirus" in this data. I need to query to show me devices that do not have 'antivirus'

I think I am on the right track here by generating a cross join of devices and software - http://sqlfiddle.com/#!9/5d9bdd/3 -I get the list of all possible combinations. Now I need to join that with the actual data to find nulls. This is where I am falling on my face.

Am I on the right track? If so, how do I finish? If not - call me out and put me on the right one!

Thanks!

PS, my real data is in MySQl - but for my own sanity I am using Access as a front end. That is why I do not have an actual cross join as it is not directly supported in access.
 

Jehos

Ars Legatus Legionis
55,560
Here you go.

Code:
SELECT DISTINCT
  client.name, 
  as_device.name, 
  as_catalog.name
FROM
  client 
  INNER JOIN as_device 
    ON client.clientid = as_device.clientid
  INNER JOIN as_software 
    ON as_device.deviceid = as_software.deviceid
  LEFT OUTER JOIN as_catalog 
    ON as_software.catalogid = as_catalog.catalogid
    AND as_catalog.name = 'antivirus'
WHERE
  as_catalog.name IS NULL
ORDER BY 
  client.name, 
  as_device.name;

Using as_catalog.name = 'antivirus' as a join condition means that you're only including rows where the catalog name is "antivirus". Using a LEFT OUTER JOIN means you include all the clients and devices, and if there isn't a catalog "antivirus", just include a null.

Then we filter down using the WHERE clause, so we're *only* including the rows where there's a null.

Finally we're using a DISTINCT in the SELECT clause to get rid of duplicates that are showing up because we have clients and devices with other software we don't care about that brings in dupe rows.

Also I ditched your parentheses because they're unnecessary for joins.
 
Status
Not open for further replies.