If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > To compare two tables in db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-02-11, 06:31
Sajeev Sajeev is offline
Registered User
 
Join Date: Aug 2011
Posts: 10
To compare two tables in db2

what is the query to compare values in two different tables which contains same column name and datatype?

Eg:
TAB1
ID COL1 COl2 Col3
1 A A A
2 B B B
3 C C C
4 A A A

TAB2
ID COL1 COl2 Col3
1 A A A
2 B B B
3 C D C
4 A A A


Output should be :

ID COL1 COl2 Col3
3 C C C
3 C D C

Last edited by Sajeev; 08-03-11 at 04:11.
Reply With Quote
  #2 (permalink)  
Old 08-02-11, 06:49
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
with replication there is a utility shipped asntdiff for this purpose
have a look at this in info center
otherwise regular sql and joins.....
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 08-02-11, 06:54
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What information do you want?
Depending on the information you want,
you can use EXCEPT, NOT EXISTS, NOT IN, so on.
Reply With Quote
  #4 (permalink)  
Old 08-02-11, 08:32
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Code:
SELECT 'Only in A:', count(*)
FROM  (SELECT * FROM A EXCEPT ALL SELECT * FROM B)
UNION ALL
SELECT 'Only in B:', count(*)
FROM (SELECT * FROM B EXCEPT ALL SELECT * FROM A)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 08-03-11, 04:10
Sajeev Sajeev is offline
Registered User
 
Join Date: Aug 2011
Posts: 10
@All

Thanks for ur reply..
I got this doubt solved with this query.. Peter's query was also working fine..

SELECT ID,COL1,COl2,COl3,count(*)
FROM
(


SELECT A.ID, A.COL1,A.COL2,A.COL3
FROM TAB1 A

UNION ALL

SELECT B.ID, B.COL1,B.COL2,B.COL3

FROM TAB2 B
) TMPTBL
GROUP BY
ID,COL1,COL2,COl3

HAVING COUNT(*) <> 2
ORDER BY ID
Reply With Quote
  #6 (permalink)  
Old 08-03-11, 09:27
HostingBee HostingBee is offline
Registered User
 
Join Date: Aug 2011
Location: London, UK
Posts: 3
Quote:
Originally Posted by tonkuma View Post
What information do you want?
Depending on the information you want,
you can use EXCEPT, NOT EXISTS, NOT IN, so on.
Sure. All in Exccept
Reply With Quote
  #7 (permalink)  
Old 08-04-11, 01:16
Sajeev Sajeev is offline
Registered User
 
Join Date: Aug 2011
Posts: 10
@bee

But with EXCEPT u may get duplicates.
Reply With Quote
  #8 (permalink)  
Old 08-04-11, 01:44
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
From Peter's example...
Quote:
Code:
SELECT 'Only in A:', count(*)
FROM  (SELECT * FROM A EXCEPT ALL SELECT * FROM B)
UNION ALL
SELECT 'Only in B:', count(*)
FROM (SELECT * FROM B EXCEPT ALL SELECT * FROM A)
remove marked parts...
Code:
SELECT 'Only in A:', count(*)
FROM  (SELECT * FROM A EXCEPT ALL SELECT * FROM B)
UNION ALL
SELECT 'Only in B:', count(*)
FROM (SELECT * FROM B EXCEPT ALL SELECT * FROM A)
then, you'll get an answer.
Code:
(SELECT * FROM A EXCEPT ALL SELECT * FROM B)
UNION ALL
(SELECT * FROM B EXCEPT ALL SELECT * FROM A)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On