title: How to union two tables and extract first record
body: <p>I have two stock trading related intraday tables:</p>
  
  <ol>
  <li>Best bid and ask with quantity and timestamp </li>
  <li>Trade table with traded price and quantity</li>
  </ol>
  
  <p>Example (just fake prices and quantities):</p>
  
  <p>Trade Table</p>
  
  <pre><code>char, char, integer, number, number
  
  Ticker   time,   milliseconds    price   qty
  MSFT  09:10:02      800          23.45   10
  MSFT  09:10:02      900          23.45    5
  MSFT  09:10:06      600          23.42    1
  </code></pre>
  
  <p>Best bid ask table </p>
  
  <pre><code>char, char, integer, number, number, number, number
  
  Ticker   time   milliseconds     bid  bid_qty   ask   ask_qty
  MSFT   09:10:02     850         23.42   50     23.45    4
  MSFT   09:10:02     930         23.44   50     23.45    1
  MSFT   09:10:02     980         23.41   49     23.45    1
  MSFT   09:10:06     600         23.42   45     23.43    2
  MSFT   09:10:06     700         23.42   25     23.43    2
  MSFT   09:10:06     710         23.41   20     23.43    2
  </code></pre>
  
  <p>Question: Starting with trade timestamp I want to find the FIRST subsequent best bid best ask  record after this trade was made. In other words: this trade caused next order book situation.</p>
  
  <p>How the proper SQL statement should look like?</p>
  
  <p>Important: </p>
  
  <ul>
  <li>trade table records are far less than best bid ask records</li>
  <li>time is formated as charakter, milliseconds are stored in a separate column as integer</li>
  <li>timestamp (time + millisecond) of the best bid best ask can be the same as of a trade or a couple of milliseconds later (but not earlier!).</li>
  </ul>
  
  <p>So in my example above I need to have the following joined query result:</p>
  
  <pre><code>Ticker time    milliseconds price qty ba_time  ba_milliseconds bid    bid_qty ask   ask_qty
   MSFT  09:10:02    800      23.45 10  09:10:02       850       23.42    50    23.45    4
   MSFT  09:10:02    900      23.45  5  09:10:02       930       23.44    50    23.45    1
   MSFT  09:10:06    600      23.42  1  09:10:06       600       23.42    45    23.43    2
  </code></pre>
  
  <p>Thank you!</p>