根据最新交易过滤给定日期/时间的数据

时间:2019-12-19 22:19:50

标签: sql oracle

这是unitlog表内容的示例。它显示了执行了哪些事务,在哪个单位上以及在哪个时间/日期上执行。一旦转移到WH的设备,WH工作人员可以出于任何原因将其退回。因此,一个单位一天可以多次进入WH。每当单位进入仓库/被仓库接受时,这将是“ WI”交易类型,而在此之前,总是会有交易“ VT”来告诉单位现在进入仓库。

交易类型为:

VT:指示去仓库W01
WI:单位已被接受并进入仓库W01
V1:单位从一个位置进入另一个位置,或者您可以说是从一个架子到另一个架子,或者从一个传送带到另一个传送带。 (如果WH_LOC不是RTI,CONV,CONVS无关紧要)
V2:仓库内移动(无关紧要)
SP:将单位移至仓库外。

单元状态类型:

FL:单位未包装且不在仓库中。
WH:单位已被包装,可以转到WH。或仅在WH_loc不是(RTI,CONV,CONVS,RHS)的情况下才在WH中

要求:我需要查看在给定日期有多少单位进入仓库W01,并且从未退还,这意味着这些单位必须仍在仓库中或要进一步发货。

方法:我当时想在单位的最后一笔交易上打上勾号,如果最后一笔交易是SP,则单位在外面,或者如果最后一笔交易中的wh_loc在(RTI,CONV,CONVS之内) ,RHS),则装置在外面。但是我不知道如何实现这一目标。

enter image description here

“我的输出”表,如果我获取进入仓库的所有单位的报告,则应该看起来像

enter image description here

样本数据为

CREATE TABLE LOG( UNIT_ID INT, DATE_TRANSAC datetime, TYPE_TRANSAC varchar(10), UNIT_STAT varchar(10), WH_CODE varchar(10), WH_LOC varchar(10) );

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1129463816', '20191117 14:30:08', 'V2', 'WH', 'W01', 'I002');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200552', '20191117 15:29:50', 'WI' ,'WH', 'W01' ,'H001');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200552', '20191117 14:49:29', 'VT' ,'WH' ,'W01' ,'CONV');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200552', '20191117 14:45:40', 'SP' ,'WH' ,'F01R' ,'*');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200552', '20191117 14:39:33', 'WI' ,'WH' ,'W01' ,'H001');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200456', '20191117 15:30:04', 'WI' ,'WH' ,'W01' ,'H001');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200346', '20191117 14:53:25', 'VI' ,'WH' ,'W01' ,'CONV');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200345', '20191117 15:29:21', 'SP' ,'WH' ,'W01' ,'RTI');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200344', '20191117 15:35:53', 'SP' ,'WH' ,'W01' ,'RTI');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200333', '20191117 14:23:06', 'WI' ,'WH' ,'W01' ,'F004');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200332', '20191118 14:23:06', 'WI' ,'WH' ,'W01' ,'F004');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1129463816', '20191118 16:30:29', 'SP' ,'WH' ,'W01' ,'RTI');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200345', '20191118 16:35:29', 'VT' ,'WH' ,'W01' ,'CONV');

insert into LOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC) values ('1193200345', '20191118 16:36:34', 'WI' ,'WH' ,'W01' ,'D004');

SELECT * FROM LOG WHERE DATE_TRANSAC BETWEEN '20191117 13:00:00' and '20191117 17:00:00'

Oracle命令(但不显示时间戳,仅显示日期)

CREATE TABLE UNITLOG(

UNIT_ID NUMBER,

DATE_TRANSAC DATE,

TYPE_TRANSAC varchar2(10),

UNIT_STAT varchar2(10),

WH_CODE varchar2(10),

WH_LOC varchar2(10)

);

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1129463816',TO_DATE('17/10/2019 14:30:08', 'DD/MM/YYYY HH24:MI:SS') , 'V2', 'WH', 'W01', 'I002');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200552',TO_DATE('17/10/2019 15:29:50', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH', 'W01' ,'H001');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200552',TO_DATE('17/10/2019 14:49:29', 'DD/MM/YYYY HH24:MI:SS') , 'VT' ,'WH' ,'W01' ,'CONV');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200552',TO_DATE('17/10/2019 14:45:40', 'DD/MM/YYYY HH24:MI:SS') , 'SP' ,'WH' ,'F01R' ,'*');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200552',TO_DATE('17/10/2019 14:39:33', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH' ,'W01' ,'H001');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200456',TO_DATE('17/10/2019 15:30:04', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH' ,'W01' ,'H001');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200346',TO_DATE('17/10/2019 14:53:25', 'DD/MM/YYYY HH24:MI:SS') , 'VI' ,'WH' ,'W01' ,'CONV');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200345',TO_DATE('17/10/2019 15:29:21', 'DD/MM/YYYY HH24:MI:SS') , 'SP' ,'WH' ,'W01' ,'RTI');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200344',TO_DATE('17/10/2019 15:35:53', 'DD/MM/YYYY HH24:MI:SS') , 'SP' ,'WH' ,'W01' ,'RTI');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200333',TO_DATE('17/10/2019 14:23:06', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH' ,'W01' ,'F004');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200332',TO_DATE('18/10/2019 14:23:06', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH' ,'W01' ,'F004');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1129463816',TO_DATE('18/10/2019 16:30:29', 'DD/MM/YYYY HH24:MI:SS') , 'SP' ,'WH' ,'W01' ,'RTI');

insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200345',TO_DATE('18/10/2019 16:35:29', 'DD/MM/YYYY HH24:MI:SS') , 'VT' ,'WH' ,'W01' ,'CONV');


insert into UNITLOG (UNIT_ID, DATE_TRANSAC, TYPE_TRANSAC, UNIT_STAT, WH_CODE, WH_LOC)

values ('1193200345',TO_DATE('18/10/2019 16:35:29', 'DD/MM/YYYY HH24:MI:SS') , 'WI' ,'WH' ,'W01' ,'D004');

这是示例数据的输出,我用相同的颜色代码突出显示了相同的单位。

This is the Output of sample data, I highlighted same units with same color code.

这些是我需要的输出。

enter image description here

1 个答案:

答案 0 :(得分:0)

据我了解,您需要使用analytical function,如下所示:

Select * from
(Select t.*,
       Row_number() over (partition by unit_id, trunc(DATE_TRANSAC) order by DATE_TRANSAC desc) as rn
  From UNITLOG t
Where WH_CODE = 'W01' )
Where rn = 1
  And (TYPE_TRANSAC <> 'SP' 
        or coalesce(wh_loc,'RTI') not in ('RTI', 'CONV', 'CONVS','RHS')
      );
如果

Coalesce为null,则不得将其包含在最终输出中。如果此列不可为空,则可以删除coalesce

干杯!