Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

How do I get the difference in 2 datatables

Posted on 2014-12-15
9
Medium Priority
?
74 Views
Last Modified: 2016-02-18
I have 2 datatables,

Table A        Table B
1                   3
2                   4
3                   5
                     6

I want the results to be

Table C
1
2

I thought this was called a left outer join but statement is not giving me the correct results

               var results = (from rA in dtA.AsEnumerable()
                                                 join rB in dtB.AsEnumerable()
                                         on rA.Field<string>(PrimaryKeyName) equals rB.Field<string>(PrimaryKeyName) into joinedRows
                                                 from ab in joinedRows.DefaultIfEmpty()
                                                 select ab);
0
Comment
Question by:matthew phung
[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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40500975
SELECT a.Field
FROM TableA a
   LEFT JOIN TableB b ON a.Field = b.Field
WHERE b.Field IS NULL

or (SQL 2008 and beyond)

SELECT a.Field
FROM TableA
EXCEPT
SELECT b.Field
FROM TableB
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 40500976
I mean... the SQL query would be:

select
   *
from
   tableA as A
      left join tableB as B on B.<col> = A.<col>
where
   B.<col> is null;

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40500984
Test code in SSMS..
IF EXISTS (SELECT name FROM sys.tables WHERE name='a') 
	DROP TABLE a

IF EXISTS (SELECT name FROM sys.tables WHERE name='b') 
	DROP TABLE b

CREATE TABLE a (val int) 
INSERT INTO a (val) VALUES (1), (2), (3) 

CREATE TABLE b (val int) 
INSERT INTO b (val) VALUES (3), (4), (5), (6)  

SELECT a.val
FROM a
   LEFT JOIN b ON a.val = b.val 
WHERE b.val IS NULL

-- or (SQL 2008 and beyond) 

SELECT a.val
FROM a
EXCEPT
SELECT b.val
FROM b

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

by:psdesignadmin
ID: 40501087
dtA.Merge(dtB ); //Here we marge the datatables
dtA.GetChanges();// Get Changes

Open in new window


Now we find the Distinct records of DataTable dtA
  dtC= dtA.DefaultView.ToTable(true, cols); 

Open in new window


DefaultView returns the Distinct record but before that we have to pass the datatable as a column Array (cols) to this method.

~Tim
0
 

Author Comment

by:matthew phung
ID: 40501219
I've requested that this question be deleted for the following reason:

My code was correct. My data was wrong.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40501220
Cancelling the delete request, as multiple experts answered the question as it was asked.

If your question did not accurately reflect requirements, then please accept answer(s) here, finalize your requirements, and ask them in a new question.
0
 
LVL 1

Expert Comment

by:psdesignadmin
ID: 40502528
Accept http:#a40500975 since it was the first to give a correct answer based on information given.

~Tim
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 40502726
^=== agree
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

647 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