Solved

I need help with a query

Posted on 2013-11-19
5
278 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
5 Comments
 
LVL 24

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 73

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 31

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 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

15 Experts available now in Live!

Get 1:1 Help Now