SQL sql选择最新日期的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7984495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:07:08  来源:igfitidea点击:

sql to select record for latest date

sqldb2

提问by user438159

I given task for stock entries management where stock can be either three types(1-supplier, 2-Companies, 3-customer),

我给了库存条目管理任务,其中库存可以是三种类型(1 个供应商、2 个公司、3 个客户),

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

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

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,

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

so to get closing stock balance till a particular date i can read from table A,

所以为了在特定日期之前获得期末库存余额,我可以从表 A 中读取,

but having difficulty to get opening stock balance where i have to read from table B

但是很难获得我必须从表 B 中读取的期初库存余额

can anyone help, sorry if my question confusing

任何人都可以帮忙,对不起,如果我的问题令人困惑

structure of the table:

表结构:

TABLE A

表A

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

TABLE B

表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

record from table A

来自表 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 

record from table B

来自表 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

回答by Bobby1

Have you tried Max(date)to get the last record? Alternately, you can use Min(date)for earliest date.

你有没有试过Max(date)得到最后的记录?或者,您可以使用Min(date)最早的日期。

I have limited the search to last date of a month closing and using max(date)to get last entry up to the closing date.

我已将搜索限制在关闭日期的最后一个日期,并max(date)用于获取截止日期之前的最后一个条目。

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

回答by Bucsa Lucian

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

回答by jcolino

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