0

I have an SQL query that I am trying that I am sure is easy, but I am not that well versed so I can't quite figure it out. Wasn't even sure how to word the question. Anyway here is what I am looking at:

I have a table that has the following columns: Hostname, Path, Filename, Filesize

It is essentially a directory listing of a number of computers (Hostnames).

What I want to get is a list of distinct Hostnames where neither of two paths exist for that host. For example, grab all Hostnames that do not have a corresponding C:\users\Jeff or C:\users\Mary directory. If they have one of the two, omit. Only return them if neither of these directories exist.

Any help would be much appreciated. Thanks!!

3
  • 2
    Please show what you've tried, we're not here to do your homework for you. You have to make an attempt, then we'll show you where you went wrong, and that's how you learn. Commented Dec 8, 2014 at 22:09
  • 3
    Also, please add a tag for the RDBMS you're using. Different databases provide different ways to do this kind of query. Commented Dec 8, 2014 at 22:10
  • Even if you have troubles with the SQL statement, show us the table architecture already. That is something we're all used to see, and I think it's more visible then. Now all the queries posted have all kind of different naming of columns and table. Commented Dec 8, 2014 at 22:25

3 Answers 3

4

One way

SELECT HostName
FROM YourTable
GROUP BY HostName
HAVING COUNT(CASE WHEN Path IN ('C:\users\Jeff',
                                'C:\users\Mary') THEN 1 END) =0;
Sign up to request clarification or add additional context in comments.

Comments

2

I would recommend splitting this into two parts:

  1. Generate the list of all host names.
  2. Generate the list of all host names that contain a path you don't want.

Then, use MINUS (which automatically removes duplicates) to get the unique results:

SELECT hostname
FROM table
MINUS
SELECT hostname
FROM table
WHERE path IN ('/search/path/one', '/search/path/two')

You can also use an anti-join instead of a MINUS, but I'll leave that up to you.

5 Comments

Excellent. I can see the use of MINUS being very useful. Didn't know it existed (as I said, very new to SQL) - Thanks a bunch!
MINUS/EXCEPT remove duplicates so the DISTINCTs are not needed.
@MartinSmith - Good point. The second DISTINCT isn't required. If the hostname appears multiple times in the second query, it will still only be removed once.
Neither are required. MINUS (like UNION) doesn't return duplicates,
@MartinSmith - Huh, today I learned. I thought MINUS and UNION suppressed duplicates between the two inputs, I didn't realize it deduped the inputs themselves.
1
select distinct hostname from your_table t1
where not exists (select 1 from your_table t2 
                  where t2.hostname = t1.hostname 
                    and t2.path in ('C:\users\Jeff', 'C:\users\Mary'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.