title: MySQL Join Two Queries Horizontally
body: <p>I have a query that works correctly to pull a series of targets and total hours worked for company A. I would like to run the exact same query for company B and join them on a common date, which happens to be grouped by week. My current query:</p>
  
  <pre><code>SELECT * FROM (
    SELECT org, date,
           ( SELECT SUM( target ) FROM target WHERE org = "companyA" ) AS companyA_target,
           SUM( hours ) AS companyA_actual
    FROM time_management_system
    WHERE org = "companyA"
    GROUP BY WEEK( date )
    ORDER BY DATE
  ) q1
  
  LEFT JOIN (
    SELECT org, date,
           ( SELECT SUM( target ) FROM target WHERE org = "companyB" ) AS companyB_target,
           SUM( hours ) AS companyB_actual
    FROM time_management_system
    WHERE org = "companyB"
    GROUP BY WEEK( date )
    ORDER BY DATE
  ) q2 
  
  ON q1.date = q2.date
  </code></pre>
  
  <p>The results show all of the dates / information of companyA, however companyB only shows sporadic data. Separately, the two queries will show the exact same set of dates, just with different information in the 'target' and 'actual' columns. </p>
  
  <pre><code>companyA    2012-01-28  105.00  39.00   NULL      NULL          NULL   NULL 
  companyA    2012-02-05  105.00  15.00   NULL      NULL          NULL   NULL
  companyA    2012-02-13  105.00  60.50   companyB  2012-02-13    97.50  117.50
  </code></pre>
  
  <p>Any idea why I'm not getting all the information for companyB?</p>
  
  <p>As a side note, would anybody be able to point in the direction of converting each row's week value into a column? With companyA and companyB as the only two rows?</p>
  
  <p>I appreciate all the help! Thanks.</p>