Clodoaldo Neto
Clodoaldo Neto

Reputation: 125344

Boolean returning case

I need to join two tables. If b column is empty then the join will be done on c column. If not the join will be on b column.

This works as I need. But I suspect I'm missing something as it looks a bit convoluted for what it does:

select *
from the_table t
inner join another_table a
   on
   case when a.b = '' then
      case when t.c = a.c then 1 else 0 end
   else
      case when t.b = a.b then 1 else 0 end
   end = 1

Am I missing something?

Upvotes: 1

Views: 246

Answers (1)

Mark Byers
Mark Byers

Reputation: 838546

ON (a.b = '' AND t.c = a.c) OR (a.b <> '' AND t.b = a.b)

Upvotes: 7

Related Questions