• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 79
  • Last Modified:

How do I get the difference in 2 datatables

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
matthew phung
Asked:
matthew phung
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
PadawanDBACommented:
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
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
psdesignadminCommented:
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
 
matthew phungAuthor Commented:
I've requested that this question be deleted for the following reason:

My code was correct. My data was wrong.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
psdesignadminCommented:
Accept http:#a40500975 since it was the first to give a correct answer based on information given.

~Tim
0
 
PadawanDBACommented:
^=== agree
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now