title: "SELECT" command in "FROM" that have sub "SELECT" codeigniter
body: <p>For example i have query like this.</p>
  
  <pre><code>SELECT 
     id, 
     SUM( CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
      SUM(CASE WHEN tgl_kondisi &lt;= "2017-09-14" THEN kl ELSE 0 end ) as sum_kl2 
  FROM 
     (SELECT 
        id_kondisi as id, 
        tgl_kondisi,
        nilai_potensi_kerugian AS kl 
      FROM 
        laporan_kondisi 
      UNION ALL 
      SELECT 
         id_sub_kondisi, 
         tgl_kondisi, 
         nilai_potensi_kerugian 
      FROM 
         laporan_kondisi ) 
      merged_table 
      GROUP BY id 
      ORDER BY cast(id as char)
  </code></pre>
  
  <p>How can i do this with query codeigniter function ?</p>
  
  <p>as i know i have to </p>
  
  <pre><code>$this-&gt;db-&gt;select('id, SUM( CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, SUM(CASE WHEN tgl_kondisi &lt;= "2017-09-14" THEN kl ELSE 0 end ) as sum_kl2 ');
  $this-&gt;db-&gt;from('(SELECT id_kondisi as id, tgl_kondisi,nilai_potensi_kerugian AS kl FROM laporan_kondisi UNION ALL SELECT id_sub_kondisi, tgl_kondisi, nilai_potensi_kerugian FROM laporan_kondisi ) merged_table ');
  $this-&gt;db-&gt;group_by('id');
  $this-&gt;db-&gt;order_by('id as char');
  </code></pre>
  
  <p>any better way so i dont have to put the query like this ?</p>
  
  <pre><code>$this-&gt;db-&gt;from('(SELECT id_kondisi as id,
  tgl_kondisi,nilai_potensi_kerugian AS kl FROM laporan_kondisi UNION ALL
  SELECT id_sub_kondisi, tgl_kondisi, nilai_potensi_kerugian FROM
  laporan_kondisi ) merged_table ');
  </code></pre>