Solved

Complex query to relate three addressing tables

Posted on 2013-05-23
1
197 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
ID: 39193009
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Date Report Options 2 28
First Max value 3 28
sql server query 18 36
SQL - Simple Pivot query 8 13
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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