Solved

I need help with a query

Posted on 2013-11-19
5
282 Views
Last Modified: 2013-12-09
Hi Experts,
I have TableA and TableB in my SQL Server 2012 database.  I want to run a query that returns the AccountNo, and Amount that exists in TableB, but not in TableA.  The table can be compared by using the AccountNo.

Example of raw data.
TableA
AccountNo
1001
1002

TableB
AccountNo      Amount
1001            100.00
1002            250.00
1003             175.00
1003             200.00
1004            145.00


I want the result to be AccountNo in TableB only:
AccountNo      Amount
1003             175.00
1003             200.00
1004            145.00
0
Comment
Question by:mainrotor
[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 25

Assisted Solution

by:chaau
chaau earned 125 total points
ID: 39661175
You need to use LEFT JOIN, like this:
SELECT B.AccountNo, B.Amount FROM
TableB B LEFT JOIN TableA A ON B.AccountNo = A.AccountNo
WHERE A.AccountNo IS NULL

Open in new window

0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 125 total points
ID: 39661176
select * from tableB where accountno not in (select accountno from tableA)
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 125 total points
ID: 39663160
not exists may perform best...

select  accountno,amount
 from tableb as B
 where not exists (select accountno from tablea as X
                                where x.accountno = b.accountno)
 order by 1,2
0
 
LVL 32

Accepted Solution

by:
awking00 earned 125 total points
ID: 39663218
Another way -
select * from TableB
where AccountNo in
(select AccountNo from TableB
 except
 select AccountNo from TableA);
0
 
LVL 2

Expert Comment

by:Subhasree
ID: 39664861
Using the Left Join as suggested above is more efficient than using a subquery.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

733 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