title: Checking same condition in different tables in a query
body: <p>I want to add up inventory movements made before a date grouped by product.  I have to check the date from different tables depending on the document type.</p>
  
  <p>Basically I have 3 type of movements: invoices, nulled invoices and other types of documents. The information of those documents are in 3 tables:</p>
  
  <ul>
  <li><strong>INVOICES:</strong> <code>DOC</code>, <code>DATE</code>, ....</li>
  <li><strong>NULLED_INV:</strong> <code>DOC</code>, <code>DATE</code>, ....</li>
  <li><strong>OTHERS:</strong> <code>DOC</code>, <code>DATE</code>, ....</li>
  </ul>
  
  <p>Then I have all the movements in another table:</p>
  
  <ul>
  <li><strong>MOVEMENTS</strong>(<code>PRODUCT_ID</code>, <code>DOC</code>, <code>DOC_TYPE</code>, <code>QUANTITY</code>...)</li>
  </ul>
  
  <p>So I need to check the dates in the corresponding table depending on <code>DOC_TYPE</code> in order to add up the <code>QUANTITY</code> of a movement. For instance if <code>DOC_TYPE = 1</code> then check the date of the <code>DOC</code> in <code>INVOICES</code>, if <code>DOC_TYPE = 2</code> then check the date in <code>NULLED_INV</code> and if <code>DOC_TYPE = 3</code> then check the date in <code>OTHERS</code>, and add up all the movements grouped by product:</p>
  
  <pre><code>PRODUCT_ID TOTAL_MOVEMENTS_BEFORE_XXXX
  1001       10
  1002       25
  ...
  </code></pre>
  
  <p>Any idea in how to make such query?</p>
  
  <p>I tried using an IF in the <code>WHERE</code> clause but it didn't work.</p>