title: case-when in pyspark checking against several tables
body: <p>I would like to know how can I do the below sql code in pyspark?</p>
  
  <p>In sql, I've created several tables inside the query. </p>
  
  <p>Can I do it in pyspark avoiding the creation of several dfs? An id can be in none, all or some of the time periods (I have 5 time periods).</p>
  
  <pre><code>create table mytable as
  select p.id, 
      case when t1.id is not null then 1 else 0 end as active_period_1,
      case when t2.id is not null then 1 else 0 end as active_period_2
  
  from
  (select id from table1) p
  left join (select id from initial_table where buy_date&gt;='2018-01-01' and 
   buy_date&lt;'2019-01-01') t1  on p.id= t1.id
  left join (select id from initial_table where buy_date&gt;='2017-01-01' and 
   buy_date&lt;'2018-01-01') t2  on p.id= t2.id
  </code></pre>