Solved

Exclusive Join

Posted on 2014-09-29
5
145 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 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 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
string fuctions 4 26
Update a text value in another table 10 40
ms sql + get number in list out of total 7 29
access query to sql server 3 20
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 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