Solved

Complex query to relate three addressing tables

Posted on 2013-05-23
1
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 49

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

717 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