I have a left join command that also checks for matching letters and two other combinctions in the data between the joined tables. The DISTINCT ON is to ensure the location_plus has no duplication as results from the table on the right. To test my results I added an AND to check for a particular account numbers results. This AND component seems to be completely ignored, returning almost all of the records instead of the ~10 for that a/c number. Why is this?
SELECT
DISTINCT ON (location_plus)
a.*,b.*
FROM schema.source_table b
LEFT JOIN schema.other_reference a
ON a.loc_id = (substring(b.loc_id,3))::integer
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((left(b.loc_id,1)='B' AND left(a.table_name,1)='A') OR (left(b.loc_id,1)='B' AND left(a.table_name,1)='B'))
and b.account_number='12345678'
ORwithANDwithout proper parentheses.