Matching addresses using fuzzy logic - sql

I have two tables of addresses that I am trying to match to. Both are structured with Street (including number), city, state and zipcode. In my query, I am trying to join the two tables Like this:
JOIN [AxSupport].[dbo].Address ADR ON LPA.City = ADR.City
and LPA.State = ADR.State
and ADR.ZipCode = LPA.ZIPCODE
and ADR.COUNTRY = LPA.COUNTRY
and ADR.Street = LPA.STREET
I tested with one customer that has 2020 address records records and the joined result only returns 79 records. If I change the street comparison to Soundex(ADR.Street) = SoundEx(LPA.Street) I get 2660 records. Is there another way of formatting the streets to get an accurate return?

Related

Updating table with COALESCE and JOIN PostgreSQL

My data set is a list of house sales. I am trying to update the address column so that any NULL values get replaced with an actual address. I am getting these replacement addresses from other inputs within the data. There are other sales of the same house so they share a "parcelid". I am doing an UPDATE with COALESCE and an INNER JOIN to complete the task. I am doing so with this code.
UPDATE house
SET property_address =
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
When I look at my 'house' table though, every single entry in the 'property_address' line has been updated to the same address. When I did some digging that address is the first address that occurs when I look at the join with this code.
SELECT
house1.parcelid,
house2.parcelid,
house1.property_address,
house2.property_address,
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
How to I get it to one ignore any addresses that are not null and two update to the appropriate address and not just keep copying the same address. Thanks for the help. (This is my first time asking a question here so any pointers on how to properly format would be appreciated)

SQL Update based on secondary table in BQ

I have 2 tables, 1 containing the main body of information, the second contains information on country naming convensions. in the information table, countries are identified by Name, I would like to update this string to contain an ISO alpha 3 value which is contained in the naming convention table. e.g turning "United Kingdom" -> "GBR"
I have wrote the following query to make the update, but it effects 0 rows
UPDATE
`db.catagory.test_votes_ds`
SET
`db.catagory.test_votes_ds`.country = `db.catagory.ISO-Alpha`.Alpha_3_code
FROM
`db.catagory.ISO-Alpha`
WHERE
`LOWER(db.catagory.ISO-Alpha`.Country) = LOWER(`db.catagory.test_votes_ds`.country)
I've done an inner join outside of the update between the 2 to make sure that the values are compatable and it returns the correct value, any ideas as to why it isn't updating?
The join used to validate the result is listed below, along with the result:
SELECT
`db.catagory.test_votes_ds`.country, `db.catagory.ISO-Alpha`.Alpha_3_code
from
`db.catagory.test_votes_ds`
inner join
`db.catagory.ISO-Alpha`
on
LOWER(`db.catagory.test_votes_ds`.country) = LOWER(`db.catagory.ISO-Alpha`.Country)
1,Ireland,IRL
2,Australia,AUS
3,United States,USA
4,United Kingdom,GBR
This is not exactly an answer. But your test may not be sufficient. You need to check where the values do not match. So, to return those:
select tv.*
from `db.catagory.test_votes_ds` tv left join
`db.catagory.ISO-Alpha` a
on LOWER(tv.country) = LOWER(a.Country)
where a.Country IS NULL;
I suspect that you will find countries that do not match. So when you run the update, the matches are getting changed the first time. Then the non-matches are never changed.

MSSQL comparing 2 columns issue

I have two tables:
Customers info, like id,customerName,address etc.. and RecipientOfGoods - customerId, address.
The addresses from both tables differ, the address in customer table is by registration and the address in second table is delivery address. I need to add in the first table the recipient of goods address in order to compare the two type of addresses.
use dwh01
SELECT distinct
cus.[BranchId]
,cus.[CustomerId]
,cus.[CustomerName]
,cus.[Street] as StreetByReg
,del.Street as StreetForDelivery
,cus.[Postalcode] as PKByReg
,del.Postalcode as PKForDelivery
,cus.[City] as CityByReg
,del.City as CityForDelivery
,cus.[PhoneNumber]
,cus.[EMail]
,cus.[IsActive]
,cus.[LastChangeDate]
FROM [dwh01].[live].[DimCustomer] cus
join live.DimRecipientOfGoods del on del.RecipientOfGoodsid = cus.Customerid
where cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060'
Thats my query, and i do get the needed result, but i also get on second row the same info but in the columns for address from the second table i get the address from first table. The question is how to remove that useless second row appearing and why it happens like this?
JOIN excluding equal rows to get only customers with new addresses.
SELECT distinct
cus.[BranchId]
,cus.[CustomerId]
,cus.[CustomerName]
,cus.[Street] as StreetByReg
,del.Street as StreetForDelivery
,cus.[Postalcode] as PKByReg
,del.Postalcode as PKForDelivery
,cus.[City] as CityByReg
,del.City as CityForDelivery
,cus.[PhoneNumber]
,cus.[EMail]
,cus.[IsActive]
,cus.[LastChangeDate]
FROM [dwh01].[live].[DimCustomer] cus
JOIN live.DimRecipientOfGoods del ON del.RecipientOfGoodsid = cus.Customerid
AND (cus.[Street] <> del.[Street] OR cus.[Postalcode] <> del.[Postalcode] OR cus.[City] <> del.[City])
WHERE cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060'
Instead of inner join please use left join to get all the customer information from DimCustomer but additional address information from DimRecipientOfGoods only.
Since there are two separate address information in second table and one is exact match from first table. You can have one address information from first table and
in second table you can ignore that address by comparing two addresses. But I am not sure whether this condition will be same for all the customers. You can choose inner join over left join if you want to remove those customers who have
no records in DimRecipientOfGoods table.
use dwh01
SELECT Distinct
cus.[BranchId]
,cus.[CustomerId]
,cus.[CustomerName]
,cus.[Street] as StreetByReg
,del.Street as StreetForDelivery
,cus.[Postalcode] as PKByReg
,del.Postalcode as PKForDelivery
,cus.[City] as CityByReg
,del.City as CityForDelivery
,cus.[PhoneNumber]
,cus.[EMail]
,cus.[IsActive]
,cus.[LastChangeDate]
FROM [dwh01].[live].[DimCustomer] cus
Left join live.DimRecipientOfGoods del on del.RecipientOfGoodsid = cus.Customerid
AND (cus.Street <> del.Street OR cus.Postalcode <> del.Postalcode OR cus.City <> del.City)
where cus.BranchId in('1080','1081') and ltrim(cus.CustomerId) = '99060'

Is there a method in Microsoft Access to populate a field based on another table?

I currently have a table called ClinicT that contains: ClinicID (primary key), ClinicSite, ClinicDiscipline and ClinicArea. I have other tables in my database that have the ClinicSite, ClinicDiscipline and ClinicArea as well. However, I would like to replace those three fields for a field called ClinicID that would capture all those information. I have tried two methods:
Lookup Wizard: it does show the corresponding Clinic ID with its discipline, site and area but I would need to manually enter the Clinic ID for each record and I have over 100,000 records.
Use a join and union: although the Clinic ID appears when only site and discipline are included in the query, it shows up blank when I introduce clinic area in the query.
SELECT
ClinicT.ClinicID, DNAT.UR, DNAT.ApptDate, DNAT.DNA, DNAT.ApptDuration, DNAT.ClinicSite, DNAT.ClinicDiscipline
FROM DNAT
LEFT JOIN ClinicT
ON (ClinicT.ClinicDiscipline = DNAT.ClinicDiscipline) AND (ClinicT.ClinicSite = DNAT.ClinicSite) AND (ClinicT.ClinicArea = DNAT.ClinicArea);
UNION
SELECT
ClinicT.ClinicID, DNAT.UR, DNAT.ApptDate, DNAT.DNA, DNAT.ApptDuration, DNAT.ClinicSite, DNAT.ClinicDiscipline
FROM ClinicT
RIGHT JOIN DNAT
ON (ClinicT.ClinicDiscipline = DNAT.ClinicDiscipline) AND (ClinicT.ClinicSite = DNAT.ClinicSite) AND (ClinicT.ClinicArea = DNAT.ClinicArea);
For the ClinicArea field, some records are empty (no data entered) because they don't have a specific area they work in (e.g. cardiac rehabilitation).
Thanks,
QuestionsEverywhere

UPDATE SQL based on 2 conditions

Given the following table structure
Locations
LocationName|Easting|Northing
Incidents
LocationString|Easting|Northing|LocationName
LocationString is a badly formatted Subway Station Name that the user of the application can type any old rubbish in to. The eastings and Northings (Co-ordinates) are consistent however. Using them i can give the location a consistent name by looking those values up in a look up table.
In ACCESS SQL i would do the following
UPDATE INCIDENTS, Locations
SET Incidents.LocationName = Locations.LocationsName
WHERE Incidents.Easting = Locations.Easting
AND
Incidents.Northing = Locations.Northing
How do i accomplish the same in T-SQL?
UPDATE I
SET I.LocationName = L.LocationsName
FROM Incidents I
JOIN Locations L
ON I.Easting = L.Easting AND I.Northing = L.Northing