Solved

Complex query to relate three addressing tables

Posted on 2013-05-23
1
190 Views
Last Modified: 2013-05-29
I have three tables, PerfectedAddresses, Parcel, and tmpAddresses.  Only two of them have a uid (PerfectedAddresses and tmpAddresses).  I am trying to come up with a select query that will combine PerfectedAddresses and Parcel and then include the data in tmpAddresses in order to eventually update tmpAddresses with the data in the other two tables.  The query is a long one so bear with me...

The total record count of PerfectedAddresses and tmpAddresses is 2141971, Parcel (has addresses that should not be present on the other two tables is 3148339.  The query that I have presently comes up with 5188349 records...

Another note,  the CASE WHEN is present because I have to combine several fields of the PerfectedAddresses in order to match the standardized address on Parcel

Any help of untangling this would be most appreciated.


select addr.addressid, addr.addrline1, addr.addrline2, pc.situs, pc.situscitystatezip from PerfectedAddresses addr INNER JOIN Parcel pc
ON (((CASE WHEN addrnum = '' THEN addrnum ELSE addrnum + ' ' END) +
            (CASE WHEN addrstreetdir = '' THEN addrstreetdir ELSE addrstreetdir + ' ' END) +
            (CASE WHEN addrstreet = '' THEN addrstreet ELSE addrstreet + ' ' END) +
            (CASE WHEN addrstreetsfx = '' THEN addrstreetsfx ELSE addrstreetsfx + ' ' END)
            )) = pc.situs INNER JOIN tmpAddresses tmp ON tmp.AddressID = addr.addressID WHERE pc.situs <> ''
0
Comment
Question by:gwarcher
1 Comment
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
I'm not sure about the case expressions in that inner join as you are excluding pc.situs <> '' anyway. To arrive at more records after the joins than you have in the base tables suggests to are getting multiple matches for some addr records to those in pc. The other things is do you really have a trailing space on pc.situs? Anyway, could you try the following.
SELECT count(*)
FROM PerfectedAddresses addr
INNER JOIN Parcel pc
    ON ( addr.addrnum + ' ' + addr.addrstreetdir + ' ' + addr.addrstreet + ' ' + addr.addrstreetsfx + ' ' ) = pc.situs
INNER JOIN tmpAddresses tmp ON tmp.AddressID = addr.addressID

Open in new window

0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now