title: mysql join same table different result set
body: <p>I would like to combine different results from the same table as one big result. </p>
  
  <pre><code>SELECT host_name,stats_avgcpu,stats_avgmem,stats_avgswap,stats_avgiowait 
      FROM sar_stats,sar_hosts,sar_appgroups,sar_environments
      WHERE stats_host = host_id
      AND host_environment = env_id
      AND env_name = 'Staging 2'
      AND host_appgroup = group_id
      AND group_name = 'Pervasive'
      AND DATE(stats_report_time) =  DATE_SUB(curdate(), INTERVAL 1 DAY)
  
  SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
   FROM sar_stats 
   WHERE  stats_id = "stat_id of the first query" and DATE(stats_report_time) 
     BETWEEN DATE_SUB(curdate(), INTERVAL 8 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)
  
  SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
   FROM sar_stats 
   WHERE  stats_id = "stat_id of the first query" and DATE(stats_report_time) 
     BETWEEN DATE_SUB(curdate(), INTERVAL 31 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)
  </code></pre>
  
  <p>Desired output would be something like ...</p>
  
  <pre><code>host_name|stats_avgcpu|stats_avgmem|stats_avgswap|stats_avgiowait|7daycpuavg|7daymemavg|7dayswapavg|7dayiowaitavg|30daycpuavg|30daymemavg|....etc
  </code></pre>
  
  <p>SQL Fiddle
  <a href="http://sqlfiddle.com/#!8/4930b/3" rel="nofollow">http://sqlfiddle.com/#!8/4930b/3</a></p>