Solved

I need help with a query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

22 Experts available now in Live!

Get 1:1 Help Now