SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


compare the value in a column against multiple databases


compare the value in a column against multiple databases

Author
Message
GF
GF
Mr or Mrs. 500
Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)Mr or Mrs. 500 (562 reputation)

Group: General Forum Members
Points: 562 Visits: 985
Using SQL 2000

I have a table in Database A, lets call it customer.
In the customer table there are several columns but I am interested in customer_name and address and city

I want to do a comparison against similar tables in other databases.

Sample TSQL

Select A.customer_name, A.address, A.city, B.customer_name, B.address, b.city, c.customer_name, c.address, c.city
from customer as A
join DatabaseB.dbo.customer as B on left(A.customer_name,18) = left(B.customer_name,18)
join DatabaseC.dbo.customer as C on left(A.customer_name,18) = left(B.customer_name,18)


The above is for example only and may not be entirely correct.

My challenge is
I want to return to my result set one record for each possible match based on the best match.

Meaning if Database B matches exactly on customer_name and address and city but Database C does not then only return database B's results.

I don't know if this is possible but if anyone can show me how to get the results I need I would greatly appreciate it.



laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)

Group: General Forum Members
Points: 616 Visits: 1272
I haven't got SQL2000, but I think this will work.

Assuming I've understood the problem, ie it's the comparison & not the fact it's across databases...

I've used 3 tables on the same DB, but it should work across databases.

You could end up with 2 nulls if neither B or C match...




--== Sample Data ==--
use tempdb
go

if object_id('tempdb..#Customer1') is not null drop table #Customer1
if object_id('tempdb..#Customer2') is not null drop table #Customer2
if object_id('tempdb..#Customer3') is not null drop table #Customer3

create table #Customer1
(
customer_name varchar(30),
address varchar(30),
city varchar(30)
)

insert into #Customer1 values ('Joe James', '43 Holgate St', 'York')
insert into #Customer1 values ('Ken Fletcher', '15 North St', 'Leeds')

create table #Customer2
(
customer_name varchar(30),
address varchar(30),
city varchar(30)
)

insert into #Customer2 values ('Joe James', 'Holgate St', 'York')
insert into #Customer2 values ('Ken Fletcher', '15 North St', 'Leeds')

create table #Customer3
(
customer_name varchar(30),
address varchar(30),
city varchar(30)
)

insert into #Customer3 values ('Joe James', '43 Holgate St', 'York')
insert into #Customer3 values ('Ken Fletcher', 'North St', 'Leeds')

--== Example to show result from joins ==--
Select
A.customer_name, A.address, A.city,
B.customer_name, B.address, b.city,
c.customer_name, c.address, c.city
from #Customer1 as A
left join #Customer2 as B on left(A.customer_name,18) = left(B.customer_name,18)
and A.Address = B.Address and A.City = B.City
left join #Customer3 as C on left(A.customer_name,18) = left(C.customer_name,18)
and A.Address = C.Address and A.City = C.City

--== Possible Solution ==--
Select
ISNULL(B.customer_name, C.customer_name) as customer_name,
ISNULL(B.address, C.address) as address,
ISNULL(B.city, C.city) as city
from #Customer1 as A
left join #Customer2 as B on left(A.customer_name,18) = left(B.customer_name,18)
and A.Address = B.Address and A.City = B.City
left join #Customer3 as C on left(A.customer_name,18) = left(C.customer_name,18)
and A.Address = C.Address and A.City = C.City





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search