Skip to content
Advertisement

Is there a more efficient way to write code for bin values in Databricks SQL?

I am using Databricks SQL, and want to understand if I can make my code lighter:

   Select
case when (
        age_18__24 is null AND
        age_25__34 is null AND
        age_35__44 is null AND
        age_45_or_more is null
      ) then 1 else 0 end as flag1...

Instead of writing each line, is there a cool way to state that all of these columns starting with “age_” need to be null in 1 or 2 lines of code?

Advertisement

Answer

If each bin is a column then you probably are going to have to spell it out, but you could use coalesce:

select
  case when 
    coalesce(age_18__24, age_25__34, age_35__44, age_45_or_more) is null 
    then 1 else 0
  end as flag1
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement