title: Select first and last records in grouped results - Oracle 11g
body: <p>Say I have the following information in an Oracle 11g table:</p>
  
  <pre><code>Qty Production  order   Date and time
  --- -----------------   ---------------
  20  00000000000000001   12-JAN-14 00:02
  20  00000000000000001   12-JAN-14 00:05
  20  00000000000000001   12-JAN-14 00:07
  20  00000000000000001   13-JAN-14 00:09
  30  00000000000000002   12-JAN-14 00:11
  30  00000000000000002   12-JAN-14 00:15
  30  00000000000000002   12-JAN-14 00:20
  30  00000000000000002   14-JAN-14 00:29
  </code></pre>
  
  <p>I would like to write a query that would return the following:</p>
  
  <pre><code>Qty Production  order   First               Last
  --- -----------------   ---------------     ---------------
  80  00000000000000001   12-JAN-14 00:02     13-JAN-14 00:09
  120 00000000000000002   12-JAN-14 00:11     14-JAN-14 00:29
  </code></pre>
  
  <p>That is, the sum of the <strong>Qty</strong> column grouped by <strong>Production order</strong>, and the date/time of the first and last records for each <strong>Production order</strong>.
  I came up with a query that yielded this result:</p>
  
  <pre><code>Qty Production  order   First               Last
  --- -----------------   ---------------     ---------------
  80  00000000000000001   12-JAN-14 00:02     14-JAN-14 00:29
  120 00000000000000002   12-JAN-14 00:02     14-JAN-14 00:29
  </code></pre>
  
  <p>Which means that the <strong>First</strong> and <strong>Last</strong> columns show the overall first and last date / time of the whole table.
  Please note that this is a dummy table. Sorry I am now allowed to write the actual query I came up with since work policies do not allow me to share it.
  Also, I tried with windowing functions such as <strong>rank()</strong> and <strong>row_number()</strong> but my user does not have enough privileges to do so.
  Any help or hints will be greatly appreciated.</p>