Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exclusive Join

Posted on 2014-09-29
5
Medium Priority
?
169 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 200 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 15

Assisted Solution

by:Vikas Garg
Vikas Garg earned 800 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 800 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

971 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