title: Spark sql sum based on multiple cases
body: <p>I am using Spark data frame to extract some statistic from my data. 
  let's assume my data is like :</p>
  
  <pre><code>+----+------------+------+--------+-------+---------+
  | id |    date    | type | player | level |  stage  |
  +----+------------+------+--------+-------+---------+
  |  1 | 2018-03-26 | XXX  | John   |     4 | stage_2 |
  |  2 | 2018-03-26 | YYY  | Adam   |     1 | stage_1 |
  |  3 | 2018-03-26 | ZZZ  | Sarah  |     6 | stage_3 |
  |  4 | 2018-03-26 | XXX  | Bruce  |    12 | stage_2 |
  |  5 | 2018-03-26 | YYY  | Sarah  |     6 | stage_1 |
  |  6 | 2018-03-26 | ZZZ  | John   |     4 | stage_2 |
  +----+------------+------+--------+-------+---------+
  </code></pre>
  
  <p>I want to count some data here, for example, when I want to count how many time I have something with type <code>XXX</code> each day I will do something like </p>
  
  <pre><code>dataframe.groupBy(date).agg(expr("sum(case when type = 'XXX'then 1 else 0 end) as XXX_Count"))
  </code></pre>
  
  <p>But I don't know what should I do for the more complicated use cases.</p>
  
  <p>let's say I want to count how many time each player played at the highest stage he could, based on his/her level</p>
  
  <p>I need to have this</p>
  
  <p>When level between 1 and 3 stage should be 1<br>
  When level between 3 and 6 stage should be 2<br>
  When level between 6 and 12 stage should be 3<br>
  When level between 12 and 20 stage should be 4<br>
  When level > 20 stage should be 5</p>
  
  <p>the result should be something like</p>
  
  <pre><code>+--------+-------+
  | player | count |
  +--------+-------+
  | John   |     2 |
  | Adam   |     1 |
  | Sarah  |     1 |
  | Bruce  |     0 |
  +--------+-------+
  </code></pre>
  
  <p>let's say for John, his level is 4 so he should play on stage2 and based on the data he played 2 times on that stage, in Sarah's case she is level 6 so she should play on stage 3 but she only played on that stage one</p>