Complex query to relate three addressing tables
Posted on 2013-05-23
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 <> ''