title: SQL Query replace UNION ALL with JOINS or something
body: <p>Now I have 3 tables</p>
  
  <p>Orders:</p>
  
  <blockquote>
    <p>ID | Date | Machine | Planning</p>
  </blockquote>
  
  <p>GoodPieces:</p>
  
  <blockquote>
    <p>ID | Date | Machine | Pieces | Product</p>
  </blockquote>
  
  <p>BadPieces:</p>
  
  <blockquote>
    <p>ID | Date | Machine | Pieces | Product | Component</p>
  </blockquote>
  
  <p>The output of the query is</p>
  
  <blockquote>
    <p>Date | Machine | Planning | GoodPieces | BadPieces</p>
  </blockquote>
  
  <p>The actual query is something like that:</p>
  
  <pre><code>SELECT
    data.Date AS Date,
    data.Machine AS Machine,
    SUM(CASE WHEN data.type = 'Planning' THEN data.value END ) AS Planning,
    SUM(CASE WHEN data.type = 'GoodPieces' THEN data.value END ) AS GoodPieces,
    SUM(CASE WHEN data.type = 'BadPieces' THEN data.value END ) AS BadPieces
  FROM
        ( SELECT
            'Planning' AS Type,
            Date AS Date,
            Machine AS Machine,
            Planning AS Value
          FROM Orders
          UNION ALL
          SELECT
            'GoodPieces',
            Date,
            Machine,
            Pieces AS Value
          FROM GoodPieces
          UNION ALL
          SELECT
            'BadPieces'
            Date,
            Machine,
            Pieces AS Value
          FROM BadPieces ) AS data
  GROUP BY
     Date,
     Machine
  </code></pre>
  
  <p>My question is if is there a way to get the same output with this 3 tables</p>
  
  <p>Orders:</p>
  
  <blockquote>
    <p>ID | Date | Machine| Planning</p>
  </blockquote>
  
  <p>GoodPieces:</p>
  
  <blockquote>
    <p>OrderID | Pieces | Product</p>
  </blockquote>
  
  <p>BadPieces:</p>
  
  <blockquote>
    <p>OrderID | Pieces | Product | Component</p>
  </blockquote>
  
  <p>Thanks,</p>