title: How to optimize script and handle dynamic data in Oracle SQL?
body: <p>I need to create 12 months report, which counts values per months. I have made a separate temp table using WITH for each months which counts parts for each aircraft. It takes data from the PARTS table. My table for January looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>type</th>
  <th>qty</th>
  <th>month</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Airbus</td>
  <td>248</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Boeing</td>
  <td>120</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Emb</td>
  <td>14</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Then I count amount of aicrafts each type per months using AC table, here's table for January:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>type</th>
  <th>qty</th>
  <th>month</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Airbus</td>
  <td>23</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Boeing</td>
  <td>10</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Emb</td>
  <td>5</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Since I need to find a division of Qty to Count, I implement division Qty / count. So I joined table 1 and table 2 using month column.  And combined table for January looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>type</th>
  <th>qty</th>
  <th>count</th>
  <th>div</th>
  <th>month</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Airbus</td>
  <td>248</td>
  <td>23</td>
  <td>10.7</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Boeing</td>
  <td>120</td>
  <td>10</td>
  <td>12</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Emb</td>
  <td>14</td>
  <td>5</td>
  <td>2.8</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I create temp table for each month and the combine them with UNION ALL. But I am afraid it could lead to DB slowdown. I think I need to rewrite and optimize my script. Any ideas how I could implement that?</p>
  <p>Also the data in tables is dynamic and can change. So I need to look only for the last 12 months.</p>
  <p>In my script I will have to manually add more months, which is not applicaple.</p>
  <p>Is there a way that could possibly solve the problem of optimization and take into account only last 12 months?</p>