Hi DBA's,
I need to figure out how to compare the column data in two distinct tables. I have two files that populate these two tables. Basically I am doing a file comparison here. Let me explain the process:
Table 1
Col 1 Col 2
ID Name
1 A
2 C,D
3 F
Table 2
Col
Name
E
F
D
Now if there is any data that is present in Table 2 that matches with the data in table 1 then I need to write the entire record of table 2 into a separate table OR file.
Here is what I think I need to do.
1. Take first record from Table 1 and scan Table 2 to see if the Name 'A' exists. If yes put/insert the record from Table 2 in a seprate table say table 3 and then go to the second record. If no match then go directly to the second record in table 1. Repeat the process till every record in table 1 is compared to the records in table 2.
2. Now the trick here is some Names have only last name. Others have last name and first name. So for Table 1, Name C,D should be a match to D in Table 2. I have to send this record to Table 3. How do I accomplish that? Should I spilt the Col2 into columns. How do I do that?
Please note that table 2 would have close to 5000 records.
Please advise.
Thanks in anticipation.Think you'll be having a lot of false matches on Smtih and Jones...
Maybe not Kaiser though...
Can you post the DDL of the Tables, and sample data..like
CREATE TABLE myTable99 (Col1 int, Col2, varchar(50), ect
For Sample Data, something like..
INSERT INTO myTable99 (Col1, Col2, ect)
SELECT 1, 'Brett Kaiser', ect UNION ALL
SELECT 2, 'Indiana Jones', ect UNION ALL
SELECT 3, 'Jones', ect UNION ALL
SELECT 4, 'Jeff Smith', ect UNION ALL
get the picture?
It's easier to help when we have the actual stuff...
Still think the matching will be a fudge though..
maybe you can match on exact, remove that population, then do the fudge on a smaller subset...sqlsql
Showing posts with label dba. Show all posts
Showing posts with label dba. Show all posts
Tuesday, March 20, 2012
Comparing column data in two tables
DBA's of SQL Server:
I have two tables, the columns of which I need to compare.
Table A
Col1............Col2
Name.......ID
ABC........ 1
DEF ........ 2
WXY.........3
Table B
Col1......Col2....Col3
Name1...Name2..Name3
A......... B.......C
D........ G........Z
I need to output every record of Table A where the even a single alphabet in name column matches with Table B.
So as in the above example there is a match for record 1 and record 2( D in DEF matches with record2 Name1 in Table B) in Table A but record3 (WXY) does not match with any of the alphabets of Table B. So I should get only the first two rows of Table A. Can anyone help me structure this query?
Appreciate your help.Try something like this
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name2) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name3) +'%'
Originally posted by vivek_vdc
DBA's of SQL Server:
I have two tables, the columns of which I need to compare.
Table A
Col1............Col2
Name.......ID
ABC........ 1
DEF ........ 2
WXY.........3
Table B
Col1......Col2....Col3
Name1...Name2..Name3
A......... B.......C
D........ G........Z
I need to output every record of Table A where the even a single alphabet in name column matches with Table B.
So as in the above example there is a match for record 1 and record 2( D in DEF matches with record2 Name1 in Table B) in Table A but record3 (WXY) does not match with any of the alphabets of Table B. So I should get only the first two rows of Table A. Can anyone help me structure this query?
Appreciate your help.|||This doesn't work. Here is what happens. Let me make the tables simple.
Table A
Name......ID
B C A........1
D E F.........2
A............3
Table B
Name1
A
I need all records that contain alphabet A. With the query -
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
Only record 3 (A...3) is returned. I also need record 1. Note that the individual alphabets are separated by spaces.
Let me know. Thanks.
Originally posted by fhunth
Try something like this
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name2) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name3) +'%'|||Here you go. I created two tables exactly as you specified:
create table table1
(Name varchar(3)
,ID int identity (1,1))
It has values:
ABC, 1
DEF, 2
WXY, 3
create table table2
(Name1 char(1)
,Name2 char(1)
,Name3 char(1))
It has values:
A, B, C
D, G, Z
Now run this:
select distinct table1.Name, table1.ID from table1, table2
where charindex (substring(NAME,1,1), Name1+Name2+Name3) <> 0
or charindex (substring(NAME,1,2), Name1+Name2+Name3) <> 0
or charindex (substring(NAME,1,3), Name1+Name2+Name3) <> 0
I have two tables, the columns of which I need to compare.
Table A
Col1............Col2
Name.......ID
ABC........ 1
DEF ........ 2
WXY.........3
Table B
Col1......Col2....Col3
Name1...Name2..Name3
A......... B.......C
D........ G........Z
I need to output every record of Table A where the even a single alphabet in name column matches with Table B.
So as in the above example there is a match for record 1 and record 2( D in DEF matches with record2 Name1 in Table B) in Table A but record3 (WXY) does not match with any of the alphabets of Table B. So I should get only the first two rows of Table A. Can anyone help me structure this query?
Appreciate your help.Try something like this
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name2) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name3) +'%'
Originally posted by vivek_vdc
DBA's of SQL Server:
I have two tables, the columns of which I need to compare.
Table A
Col1............Col2
Name.......ID
ABC........ 1
DEF ........ 2
WXY.........3
Table B
Col1......Col2....Col3
Name1...Name2..Name3
A......... B.......C
D........ G........Z
I need to output every record of Table A where the even a single alphabet in name column matches with Table B.
So as in the above example there is a match for record 1 and record 2( D in DEF matches with record2 Name1 in Table B) in Table A but record3 (WXY) does not match with any of the alphabets of Table B. So I should get only the first two rows of Table A. Can anyone help me structure this query?
Appreciate your help.|||This doesn't work. Here is what happens. Let me make the tables simple.
Table A
Name......ID
B C A........1
D E F.........2
A............3
Table B
Name1
A
I need all records that contain alphabet A. With the query -
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
Only record 3 (A...3) is returned. I also need record 1. Note that the individual alphabets are separated by spaces.
Let me know. Thanks.
Originally posted by fhunth
Try something like this
select * from tablea ta , tableb tb
where
ta.name like '%' + convert(varchar(1),tb.name1) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name2) +'%'
or
ta.name like '%' + convert(varchar(1),tb.name3) +'%'|||Here you go. I created two tables exactly as you specified:
create table table1
(Name varchar(3)
,ID int identity (1,1))
It has values:
ABC, 1
DEF, 2
WXY, 3
create table table2
(Name1 char(1)
,Name2 char(1)
,Name3 char(1))
It has values:
A, B, C
D, G, Z
Now run this:
select distinct table1.Name, table1.ID from table1, table2
where charindex (substring(NAME,1,1), Name1+Name2+Name3) <> 0
or charindex (substring(NAME,1,2), Name1+Name2+Name3) <> 0
or charindex (substring(NAME,1,3), Name1+Name2+Name3) <> 0
Subscribe to:
Posts (Atom)