title: How to query on another query result?
body: <p>I have a problem as I am not so strong on queries.</p>
  
  <p>I have a query with consists of a union of two select queries : </p>
  
  <pre><code>   SELECT em.emp_code,  
            em.emp_name,  
            COALESCE(SUM(pe.hours_allotted),0) AS hours,  
            pe.dated 
       FROM employee_master em
  LEFT JOIN project_employee pe ON (pe.Emp_code = em.emp_code)
      WHERE (dated &gt;= '2011-03-14'
        AND dated &lt; '2011-03-20' ) 
         OR dated IS NULL 
   GROUP BY em.emp_code 
  UNION
    (SELECT em.emp_code,
            em.emp_name,
            '0' AS hours,
            pe.dated
       FROM employee_master em
  LEFT JOIN project_employee pe ON (pe.Emp_code = em.emp_code)
      WHERE (dated &gt;= '2011-03-14'
        AND dated &lt; '2011-03-20' ) 
         OR dated IS NOT NULL 
   GROUP BY em.Emp_code)
  ORDER BY emp_name;
  </code></pre>
  
  <p>Now the result sets are returning for example as:</p>
  
  <pre><code>ecode  ename    hours 
  ----------------------
  201   Alak basu  10 
  201 alak basu    0
  </code></pre>
  
  <p>The first result is from first select statement of the union where hours = 10 
  and hours = 0 is from second select statement of union.</p>
  
  <p>What I want is:</p>
  
  <pre><code>ecode     ename       hours 
  ----------------------------
  201      alak basu     10
  </code></pre>
  
  <p>Like in the case there should be only one result per ecode. How to group it like summing up the hours on as group by ecode so that it gives me only one result as above?</p>