Solved

Exclusive Join

Posted on 2014-09-29
5
141 Views
Last Modified: 2014-09-29
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
Comment
Question by:James Elliott
5 Comments
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 50 total points
ID: 40349477
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
 
LVL 14

Assisted Solution

by:Vikas Garg
Vikas Garg earned 200 total points
ID: 40349479
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
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 40349480
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
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 50 total points
ID: 40349611
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
 
LVL 12

Author Closing Comment

by:James Elliott
ID: 40349662
Thanks all. Really helpful. Second & Third solutions appear quickest, especially with indexing the two columns.

Rgds
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now