title: Checking nested conditions in a spark sql
body: <p>I have a nested condition which I am trying to convert </p>
  
  <pre><code>if (rewards_curr_tier != 1 &amp;&amp; rewards_curr_tier != 2 &amp;&amp; rewards_curr_tier != 3) {
      detailBps.append("2")
      detailBps.append(stagNumberFormat(9999.99))
    }
    else if (sparkUtil.getRowDataInteger(currentRow, "rewards_curr_tier") eq 3) {
      detailBps.append("0")
      if (curr_floor_limit &gt; ytd_net_purch) detailBps.append(stagNumberFormat(curr_floor_limit - ytd_net_purch))
      else detailBps.append(stagNumberFormat(0.0))
    }
    else {
      detailBps.append(String.valueOf(sparkUtil.getRowDataInteger(currentRow, "rewards_next_tier")))
      if (currentRow.getAs("amt_rmngto_nxtier") == null) detailBps.append(stagNumberFormat(sparkUtil.getRowDataDouble(currentRow, "floor_limit")))
      else detailBps.append(stagNumberFormat(sparkUtil.getRowDataDouble(currentRow, "amt_rmngto_nxtier")))
    }
  </code></pre>
  
  <p>When i am using chained when it does not work as expected</p>
  
  <pre><code>when(col("rewards_curr_tier").notEqual(1)
          &amp;&amp; col("rewards_curr_tier").notEqual(2)
          &amp;&amp; col("rewards_curr_tier").notEqual(3), concat(lit("2"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(lit(9999.99)))).
          when(col("rewards_curr_tier") === 3 &amp;&amp; col("curr_floor_limit") &gt; col("ytd_net_purch"), concat(lit("0"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("curr_floor_limit") - col("ytd_net_purch")))).
          when(col("rewards_curr_tier") === 3 &amp;&amp; (col("curr_floor_limit") &lt; col("ytd_net_purch")), concat(lit("0"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(lit("0.0")))).
          when(!(col("rewards_curr_tier").notEqual(1) &amp;&amp; col("rewards_curr_tier").notEqual(2) &amp;&amp; col("rewards_curr_tier").notEqual(3)) &amp;&amp; (col("amt_rmngto_nxtier") === null), concat(col("rewards_next_tier"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("floor_limit")))).
          when((col("rewards_curr_tier") === 1 || col("rewards_curr_tier") === 2 || col("rewards_curr_tier") === 3) &amp;&amp; (col("amt_rmngto_nxtier").notEqual(null)), concat(col("rewards_next_tier"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("amt_rmngto_nxtier"))))
  </code></pre>
  
  <p>Is there a way i can use nested <code>when().otherwise(when())</code> in spark.</p>
  
  <p>Here is what i tried so far but i was unsuccessful.</p>
  
  <pre><code>when(col("rewards_curr_tier").notEqual(1)
          &amp;&amp; col("rewards_curr_tier").notEqual(2)
          &amp;&amp; col("rewards_curr_tier").notEqual(3), concat(lit("2"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(lit(9999.99))))
          .otherwise(when((col("rewards_curr_tier") === 3
                           &amp;&amp; (col("curr_floor_limit") &gt; col("ytd_net_purch"))),writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("curr_floor_limit") - col("ytd_net_purch"))).otherwise(concat(lit("0"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(lit("0.0")))))
          .otherwise(when(!(col("rewards_curr_tier").notEqual(1) 
            &amp;&amp; col("rewards_curr_tier").notEqual(2) 
            &amp;&amp; col("rewards_curr_tier").notEqual(3))
            &amp;&amp; (col("amt_rmngto_nxtier") === null), concat(col("rewards_next_tier"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("floor_limit"))))
            .otherwise(concat(col("rewards_next_tier"), writeHeaderAndTrailerUdf.stagNumberFormatUDF(col("amt_rmngto_nxtier")))))
  </code></pre>