Saturday, February 25, 2012

Query the results of another query

I'm new to the database world - I know what I want to do, but not sure if or how to do it...

Is there a way to run a query and then build on it to query the results of that query?

My example is as follows:

Query the number of distinct machines by ID in Week 1 = Results

Query the distinct number of machines by ID in Week 2 minus the Results from Week 1

- NOT EXISTS
SELECT pub_name
FROM publishers
WHERE NOT EXISTS
(SELECT *
FROM titles
WHERE pub_id = publishers.pub_id
AND type = 'business')

- Use NOT IN
SELECT pub_name
FROM publishers
WHERE pub_id NOT IN
(SELECT pub_id
FROM titles
WHERE type = 'business')


- Perform a LEFT OUTER JOIN and check for a NULL condition
SELECT pub_name
FROM publishers A
LEFT OUTER JOIN titles B
ON A.pub_id = B.pub_id
WHERE A.pub_id IS NULL

|||

Thank you - I used the NOT IN and it seems to be doing exactly what I wanted.

Much Appreciated!!

No comments:

Post a Comment