Solved

Set notation vs SQL

Posted on 2011-09-14
2
383 Views
Last Modified: 2012-06-21
Scenario: In my organisation I want to identify the PCs having only authorised software installed.

To do this I could compare the applications installed on each PC with my list of authorised applications.  Using set theory / notation the solution is easy to express i.e. a PC is considered authorised if its applications are a subset of the authorised list.
What I’m trying to get my head around is how this ‘solution’ could be implemented using SQL? I’m after some sound pointers or approaches.
Thanks.
0
Comment
Question by:antonms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36536965
Instead of looking at the PCs that only have authorized software installed, you could exclude the PCs that do not have authorized software installed.  That would be an easier query to write.

Greg



0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36536969
antonms,

The principal is the same. Another way to look at this under set theory / notation is that no member of the PC's set of applications is outside the set of authorised ones.

Therefore, in SQL you can use EXISTS and NOT EXISTS to your favor. Basically, you can query for the rows where there does not exist another row for the same PC that has an application not listed on the authorised list, i.e., an application that does not match to a row in your authorised applications table.

In general terms:

SELECT {column list}
FROM A a1
WHERE NOT EXISTS (
   SELECT 1
   FROM A a2
   LEFT OUTER JOIN B b1 ON a2.AppID = b1.AppID
   WHERE a2.PcID = a1.PcID AND b1.AppID IS NULL
)

You can do the second piece with a NOT EXISTS as well, but wanted to show OUTER JOIN is also an option. But with NOT exists, you can find the PCs that have an invalid app but finding that the app does not exist in the authorised apps table. Then to find PCs with only authorised apps, you check for the inverse of that list. So top level could also use NOT IN if you replace SELECT 1 with SELECT PcID and remove filter on PcID in the inner queries WHERE clause.

Hopefully that isn't too convoluted an explanation and everything makes sense.

Kevin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to solve "recursive" database tables 2 55
What is needed to become a DBA? 7 53
mssql 7 32
Report 8 27
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question