Solved

Complex query to relate three addressing tables

Posted on 2013-05-23
1
194 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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