?
Solved

finding common records from two tables

Posted on 2013-01-07
8
Medium Priority
?
285 Views
Last Modified: 2013-02-05
I have two tables with same columns  i have key based upon that key i want to see  if i have any dups or common records for that two tables

how can i do that.

Thanks
0
Comment
Question by:sam2929
  • 5
  • 2
8 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38752661
Use intersect?

select key_col1, key_col2 from table1
intersect
select key_col1, key_col2 from table2
/

You could join them as well but I figure intersect will be faster.
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38752663
forgot the join:

select t1.keycol1, t1.keycol2 from tab1 t1, tab2 t2
where t1.keycol1=t2.keycol1 and t1.keycol2=t2.keycol2;
0
 
LVL 14

Expert Comment

by:ajexpert
ID: 38753060
You can also try out EXISTS clause.

assuming you want data only from table1 that EXISTS in table2

select t1.* from tab1 t1
WHERE EXISTS
(select 1 from tab2 t2
where t1.col1=t2.col1
and t1.col2=t2.col2)
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:sam2929
ID: 38753126
Now i want to delete the dups how can i do that
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38753138
Something like:
Delete from table1 where rowid in
(
Select t1.rowid
From ...whatever select you wanted to use
)

If you want to delete from table2, just switch the names.
0
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38753141
Oops.

That deletes all rows.  If that isn't what you want, just let us know.
0
 

Author Comment

by:sam2929
ID: 38753305
No i don't want to delete all rows

table a looks like

doc_num    yr           line
123            2010         1
234            2010         1

table b looks like
doc_num    yr           line
123            2010         1
345           2010         1

i want final result as
doc_num    yr           line
234            2010         1

table b looks like
doc_num    yr           line
123            2010         1
345           2010         1
0
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 38753339
I didn't mean ALL rows.  I just meant all 'matching' rows. That is what you showed in the results.

When trying things like this, set up a simple test case to see what happens.

Here is a complete test using your sample data.  If you do not get the expected results, please add to it and show where it fails.

drop table taba purge;
create table taba(doc_num number, yr number, line number);

drop table tabb purge;
create table tabb(doc_num number, yr number, line number);

insert into taba values(123,2010,1);
insert into taba values(234,2010,1);

insert into tabb values(123,2010,1);
insert into tabb values(345,2010,1);
commit;

delete from taba
where (doc_num,yr,line) in (select doc_num,yr,line from tabb);
commit;

select * from taba;
select * from tabb;

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

749 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