Home > Mobile >  How to compress SQL query more effectively using many OR conditions?
How to compress SQL query more effectively using many OR conditions?

Time:07-09

Is there any good way to re-write this to something less duplicative/more efficient?

   Select
case when (
        A is null OR
        B is null OR
        C is null OR
        D is null
      ) then 1 else 0 end as flag_1...

CodePudding user response:

You can write,

IN YSQL you can chekc with CONCAT , if one is nNULL it return NULL

Select
case when (
        CONCAT(A,B,C,D) IS NULL
      ) then 1 else 0 end as flag_1...

In REDSHIFT you have a similar syntax. The convesion is need if the columns are not varchar

Select
case when (
   A::varchar||B::varchar||C::varchar||D::varchar is null
      ) then 1 else 0 end as flag_1...
  •  Tags:  
  • sql
  • Related