0

I have a bigquery select statement where in some cases, the where clauses show null = null

In the case of this statement:

select * from `db.ds.table1` t1
join `db.ds.table2` t2
on t1.first_name = t2.first_name
and t1.last_name = t2.last_name

as you can see, this would appear to be looking for people, however, in some cases, I have businesses in the tables. In those cases, first_name is used, and last_name is null.

this means I have a null = null scenerio, and fails join.

How do I tell bigquery to allow null = null, without having to put coalesce statements all over the place?

Thanks

arcee123
  • 101
  • 9
  • 41
  • 118

2 Answers2

3

Make the last name optional, and allow both last names being NULL as valid:

SELECT *
FROM `db.ds.table1` t1
INNER JOIN `db.ds.table2` t2
    ON t1.first_name = t2.first_name AND
       (t1.last_name = t2.last_name OR (t1.last_name IS NULL AND t2.last_name IS NULL));

We could also try to write a query using COALESCE, but it isn't really clear what value should be used in place of a NULL last name.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2
#standardSQL
select * from `db.ds.table1` t1
join `db.ds.table2` t2
on t1.first_name = t2.first_name
and ifnull(t1.last_name, '') = ifnull(t2.last_name, '')   

obviously ifnull is equivalent of coalesce so not sure if this is what you can accept as you mention you wanted not to use coalesce for some reason

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230