Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Exclusive Join

Hi All,

I'm looking for the most efficient way of getting an exclusive dataset.

I have circa 3m invoices in a table each with a client ID, and I have a list of 10k client IDs. I want the invoices that do not relate to the client IDs in the list.

For much smaller tasks I would do a 'WHERE ID NOT IN (SELECT ID FROM....)' etc. but I'm aware of this being a poor approach.

Thanks in advance.

Rgds
0
James Elliott
Asked:
James Elliott
4 Solutions
 
Haris DjulicCommented:
You can use EXCEPT to get the IDs which do no exist in the customer table

select distinct client_ID from invoices
EXCEPT
select distincT client_IDfrom customers
0
 
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can use Not Exists if you just want to use other table for filtering but if you want field from both tables you can use join,

HERE NOT EXISTS is best suited.

Select * from Table a where not exists
(select 1 from table b where a.clientID= b.clientID)

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
select * from invoices i
where not exists ( select null from clients c where c.id = i.clientid)

and index on both tables for the field, and the query will fly
0
 
PortletPaulCommented:
select i.*
from Invoices i
left join customers c ON i.customer_id = c.id
where c.id IS NULL

(a "Left Excluding JOIN")

-------------
btw: if you pursue using EXCEPT you do not also have to use SELECT DISTINCT

EXCEPT "Returns distinct values by comparing the results of two queries."
see
http://msdn.microsoft.com/en-au/library/ms188055.aspx
0
 
James ElliottManaging DirectorAuthor Commented:
Thanks all. Really helpful. Second & Third solutions appear quickest, especially with indexing the two columns.

Rgds
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now