• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Complex query to relate three addressing tables

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 <> ''
1 Solution
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


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now