title: Given time/interval to calculate open/high/low/close value in each grouped data
body: <p>Suppose raw data is:</p>
  
  <pre><code>  Timestamp   High Low Volume
    10:24.22345 100   99  10
    10:24.23345 110   97  20
    10:24.33455 97    89  40
    10:25.33455 60    40  50
    10:25.93455 40    20  60
  </code></pre>
  
  <p>With a sample time of 1 second, the output data should be as following (they are grouped by second):</p>
  
  <pre><code>  Timestamp   Open Close High  Low Volume 
    10:24         82   83   110   89  70     
    10:25         50   40   60    20  110    
  </code></pre>
  
  <ul>
  <li><code>Open</code> means the <em>price</em> of the earliest data in the group</li>
  <li><code>Close</code> means the  price of the lastest data in the group</li>
  <li><code>Volume</code> means the sum(Volume) in the group</li>
  </ul>
  
  <p>The sampling unit from varying from <code>1 second, 5 sec, 1 minute, 1 hour, 1 day, ...</code></p>
  
  <p>Now I can get the High, Low, Volume  by the following SQL:</p>
  
  <pre><code>SELECT date_trunc(\'#{interval}\', ticktime) AS ticktime_stamp,
         max(bid_price) as high,
         min(bid_price) as low,
         sum(bid_volume) as volume,
         max(product_type) as product_type
  FROM   czces
  WHERE  ticktime &gt;=  \'#{begin_time}\'::timestamp
  AND  ticktime &lt;  \'#{end_time}\'::timestamp
  AND  product_type =\'#{product_type}\'
  GROUP  BY 1
  ORDER  BY ticktime_stamp ASC
  </code></pre>
  
  <p>But how to get the <code>open</code>, <code>close</code> value in each group based on the above query?</p>