title: sql to select record for latest date
body: <p>I given task for stock entries management where stock can be either three types(1-supplier, 2-Companies, 3-customer),</p>
  
  <p>i have 2 tables(A and B) to maintain it,A use to track current status, and B used as history table to track the movement</p>
  
  <p>initial will be stock type 1, so inserted into A, if company buy it, table B will inserted from table A for that record and will updated stock type in table A, </p>
  
  <p>so to get closing stock balance till a particular date i can read from table A, </p>
  
  <p>but having difficulty to get opening stock balance where i have to read from table B</p>
  
  <p>can anyone help, sorry if my question confusing</p>
  
  <p>structure of the table:</p>
  
  <p>TABLE A</p>
  
  <pre><code>NUM_REF CHAR (6) NOT NULL,
  BRANCH CHAR (5) NOT NULL,
  STOCK_TYPE  CHAR (3) NOT NULL,
  INSERT_DATE TIMESTAMP NOT NULL
  </code></pre>
  
  <p>TABLE B</p>
  
  <pre><code>NUM_RIF CHAR (6) NOT NULL,
  BRANCH CHAR (5) NOT NULL,
  STOCK_TYPE  CHAR (3) NOT NULL,
  INSERT_DATE CHAR(2) NOT NULL,  
  UPDATE_DATE TIMESTAMP NOT NULL
  </code></pre>
  
  <p>record from table A</p>
  
  <pre><code>000295 10400 03 10/24/2011 5:11:12 PM  10/11/2011 12:27:21 PM
  000338 10400 02 10/14/2011 7:51:56 PM  10/13/2011 12:54:28 PM
  000399 10400 03 10/24/2011 6:25:53 PM  10/21/2011 6:13:20 PM 
  000400 10400 02 10/14/2011 8:06:21 PM  10/14/2011 4:21:37 PM 
  000410 10400 03 10/18/2011 5:17:36 PM  10/18/2011 5:17:36 PM 
  000416 10400 03 10/18/2011 5:27:19 PM  10/18/2011 5:27:19 PM 
  000420 10400 03 10/25/2011 6:18:35 PM  10/17/2011 3:01:49 PM 
  000423 10400 03 10/25/2011 6:18:35 PM  10/17/2011 4:10:00 PM 
  000450 10400 03 10/19/2011 4:45:38 PM  10/18/2011 2:30:40 PM 
  </code></pre>
  
  <p>record from table B</p>
  
  <pre><code>000284 10400 01 10/10/2011 5:49:15 PM
  000288 10400 01 10/11/2011 12:22:50 P
  000289 10400 01 10/11/2011 12:23:35 P
  000295 10400 01 10/11/2011 12:27:21 P
  000295 10400 03 10/13/2011 2:47:33 PM
  000295 10400 03 10/13/2011 2:58:31 PM
  000295 10400 03 10/13/2011 3:22:19 PM
  000295 10400 03 10/13/2011 4:34:07 PM
  000295 10400 03 10/13/2011 4:41:08 PM
  000295 10400 03 10/13/2011 4:41:40 PM
  000295 10400 03 10/13/2011 4:45:05 PM
  000295 10400 03 10/13/2011 4:46:36 PM
  000295 10400 03 10/13/2011 4:56:03 PM
  000295 10400 03 10/13/2011 4:58:47 PM
  000295 10400 03 10/14/2011 11:15:47 A
  000295 10400 03 10/14/2011 5:45:33 PM
  000296 10400 01 10/11/2011 12:27:21 P
  000328 10400 01 10/13/2011 12:20:49 P
  000328 10400 03 10/14/2011 12:31:07 P
  000328 10400 03 10/17/2011 3:41:45 PM
  000328 10400 03 10/18/2011 12:14:21 P
  000328 10400 02 10/20/2011 4:41:39 PM
  000331 10400 01 10/13/2011 12:25:02 P
  000331 10400 03 10/14/2011 3:03:31 PM
  000331 10400 03 10/17/2011 3:54:02 PM
  000333 10400 01 10/13/2011 12:33:19 P
  000333 10400 03 10/14/2011 4:01:59 PM
  000333 10400 03 10/18/2011 11:06:18 A
  000337 10400 01 10/13/2011 12:52:23 P
  000338 10400 01 10/13/2011 12:54:28 P
  000369 10400 01 10/14/2011 12:33:43 P
  000369 10400 02 10/14/2011 8:05:03 PM
  000369 10400 03 10/17/2011 2:42:19 PM
  000369 10400 03 10/18/2011 2:24:56 PM
  000399 10400 01 10/21/2011 6:13:20 PM
  000400 10400 01 10/14/2011 4:21:37 PM
  000420 10400 01 10/17/2011 3:01:49 PM
  000420 10400 03 10/17/2011 3:13:07 PM
  000420 10400 03 10/25/2011 6:16:43 PM
  000423 10400 01 10/17/2011 4:10:00 PM
  000423 10400 03 10/25/2011 6:16:43 PM
  000424 10400 01 10/17/2011 4:22:04 PM
  000424 10400 03 10/25/2011 6:42:06 PM
  000425 10400 01 10/17/2011 4:24:41 PM
  000425 10400 03 10/25/2011 6:42:06 PM
  </code></pre>