Solved

Exclusive Join

Posted on 2014-09-29
5
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 15

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 143

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 49

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

623 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