sql选择最新日期的记录

时间:2011-11-02 17:13:21

标签: sql db2

我给出了股票分配管理的任务,其中股票可以是三种类型(1个供应商,2个公司,3个客户),

我有2个表(A和B)来维护它,用于跟踪当前状态,B用作历史表来跟踪运动

初始将是库存类型1,因此插入A,如果公司购买,表B将从表A插入该记录,并将更新表A中的库存类型,

所以要关闭库存余额,直到我可以从表A中读取特定日期,

但难以获得开放式库存余额,我必须从表B中读取

任何人都可以提供帮助,抱歉,如果我的问题令人困惑

表的结构:

表A

NUM_REF CHAR (6) NOT NULL,
BRANCH CHAR (5) NOT NULL,
STOCK_TYPE  CHAR (3) NOT NULL,
INSERT_DATE TIMESTAMP NOT NULL

表B

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

表A中的记录

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 

表B中的记录

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

3 个答案:

答案 0 :(得分:1)

您是否尝试Max(date)获取最后一条记录?或者,您可以使用Min(date)作为最早的日期。

我将搜索限制在一个月结束的最后日期,并使用max(date)获得截止日期的最后一次输入。

Select item, max(inventorydate)
from table
where inventorydate is < '6/30/2011'

答案 1 :(得分:1)

SELECT StockId, Max(Date)
FROM (SELECT StockId, max(Date)
      FROM TableA
      GROUP BY StockId
      UNION ALL
      SELECT StockId, max(Date)
      FROM TableB
      GROUP BY StockId) a
GROUP BY StockId

答案 2 :(得分:0)

select * from tablea order by insert_date desc fetch first 1 row only for read only with ur