我想通过与以前的条目(针对该帐户)进行比较来从表中检索记录。请看下面的表格和数据。
在这个输出中,我想要的是,
ID_NUM DELIVERY_TYPE
100 2
101 2
102 2
说明:我需要 100,因为它是第一次出现 DELIVERY_TYPE IS 2(旧记录有 1) 101 因为它是第一次出现 DELIVERY_TYPE IS 2(旧记录有 3)102 因为这个 ID_NUM 只有一个条目,并且DELIVERY_TYPE 为 2
我不需要 103,因为最近的 DELIVERY_TYPE 是 1,即使它有 DELIVERY_TYPE 是 2 104,因为它有两条或更多条 DELIVERY_TYPE 是 2 的记录
任何机构都知道如何达到这个结果?
CREATE TABLE DEMO
(
ID_NUM NUMBER(10,0),
DELIVERY_TYPE NUMBER(2,0),
NAME VARCHAR2(100),
CREATED_DATE DATE
);
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(100, 2, TO_DATE('10-FEB-12 11:08:49 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(100, 1, TO_DATE('29-JAN-12 11:09:00 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(101, 2, TO_DATE('09-FEB-12 11:09:26 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(101, 3, TO_DATE('14-JAN-12 11:09:33 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(102, 2, TO_DATE('02-FEB-12 10:09:26 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(103, 1, TO_DATE('01-FEB-12 10:09:26 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(103, 2, TO_DATE('02-JAN-12 11:09:33 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(104, 2, TO_DATE('02-FEB-12 10:09:26 AM', 'DD-MON-RR HH:MI:SS AM'));
INSERT INTO DEMO
(ID_NUM, DELIVERY_TYPE, CREATED_DATE)
VALUES
(104, 2, TO_DATE('02-FEB-12 10:09:26 AM', 'DD-MON-RR HH:MI:SS AM'));