I have the below select statement. I am using a LEFT JOIN to push two tables together. There are two conditions where the LEFT JOIN should work:
Condition 1: ATTOM_ID. ATTOM ID is the unique Identifier
Condition 2: ZIP, LAST NAME, and FULL ADDRESS. These fields are string fields and all three must match to be a JOIN.
Any other condition should result in a NULL, hence the LEFT JOIN. If either condition passes, the JOIN should occur, which is why I wanted an OR statement here.
for some reason Google Big Query does not like the query because I have an OR in it. The error I get is:
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
Here is the SQL Statement. All other issues on this statement work. Is there a GBQ limitation on using LEFT JOINS and the "OR" operator? Thanks.
SELECT
Source,
FirstName,
LastName,
MiddleName,
Gender,
Age,
DOB,
Address,
Address2,
City,
State,
Zip,
Zip4,
TimeZone,
Income,
HomeValue,
Networth,
MaritalStatus,
IsRenter,
HasChildren,
CreditRating,
Investor,
LinesOfCredit,
InvestorRealEstate,
Traveler,
Pets,
MailResponder,
Charitable,
PolicalDonations,
PoliticalParty,
coalesce(P.ATTOM_ID, T.ATTOM_ID) as ATTOM_ID,
coalesce(P.GEOID, T.GEOID) as GEOID,
Score,
Score1,
Score2,
Score3,
Score4,
Score5,
PropertyLatitude AS Latitude,
PropertyLongitude AS Longitude
FROM `db.ds.table1` P
LEFT JOIN `db.ds.table2` T
ON 1 =
CASE
WHEN (P.ATTOM_ID = T.ATTOM_ID)
THEN 1
WHEN P.Zip = T. PropertyAddressZIP
AND (
LOWER(P.LastName) = LOWER(T.DeedOwner1NameLast)
OR LOWER(P.LastName) = LOWER(T.PartyOwner1NameLast)
)
AND (
STRPOS(LOWER(P.Address), LOWER(T.PropertyAddressFull) ) > 0
OR STRPOS(LOWER(T.PropertyAddressFull), LOWER(P.Address) ) > 0
)
AND IFNULL(T.PropertyAddressFull,'') != ''
THEN 1
ELSE 0 END
Below is simplified example of your issue
it produces
Error: LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
You can rewrite above as
here you are simply moving all your conditions inside IF() function and replace LEFT JOIN with CROSS JOIN
result is
as you can see - you are not missing id=4 from Table1
Hope, you can adopt this to your specific query (should be straight forward copy-pasting)
Maybe try using a
CASE
as the join clause?Maybe a different approach? Can you split the
OR
in yourJOIN
condition, create twoINNER JOIN
queries andUNION
them together?Try moving the last part of the join condition to a WHERE clause:
That may be where the issue is coming from, since it doesn't reference both tables in the join.