[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need help with a query

Posted on 2013-11-19
5
Medium Priority
?
294 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 25

Assisted Solution

by:chaau
chaau earned 500 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 500 total points
ID: 39661176
select * from tableB where accountno not in (select accountno from tableA)
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 500 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 500 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
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…

872 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