Exclude records matching subquery

Posted by Skudd on Stack Overflow See other posts from Stack Overflow or by Skudd
Published on 2010-06-01T01:02:38Z Indexed on 2010/06/01 1:13 UTC
Read the original article Hit count: 208

Filed under:
|
|

There is probably an obvious answer to this question, but I am having one heck of a time getting anywhere with it.

Consider the query:

SELECT *
FROM reports AS r
JOIN reportvalues AS rv ON rv.report_id = r.report_id
JOIN metrics AS m ON m.metric_id = rv.metric_id
WHERE r.report_id NOT IN(
    SELECT DISTINCT report_id
    FROM exclude_report
)

In this query, exclude_report is a view constructed in a similar manner.

Now what happens is the query takes an EXTREMELY long time to execute, presumably because the subquery is being executed over each and every row in the parent query. However, I am finding no feasible means of doing this any other way.

Oh great SQL wizards, please advise. I really need a way of doing this all in SQL, which I will be using in SSRS.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005