Solved

I need help with a query

Posted on 2013-11-19
5
280 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Insert parts by customer 12 31
Help creating a spatial object in SQL Server 4 20
Query Syntax 17 31
SQL Syntax: How to force case sensitive query? 2 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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

815 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

12 Experts available now in Live!

Get 1:1 Help Now