Solved

How do I get the difference in 2 datatables

Posted on 2014-12-15
9
67 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
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 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 65

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 65

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting PHP triple des decrypt to c# 9 76
How come this XML node is not read? 3 31
SQL Syntax 6 42
SQL Server remove line breaks and tabbed 2 9
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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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