我有一张表,其中记录显示物品何时被搁置或释放。 嵌套保持是可能的。有些记录变得混乱,需要新的逻辑。 为了解决这个问题,我需要将版本与保留相匹配 - 我在下面有一个粗略的示例 - id列不存在但是我需要生成。
我工作的表没有我可以使用的唯一标识符,除了它们发生的顺序之外没有任何东西可以匹配释放到保留。 我已经玩了一段时间了,并且不能把它钉下来。
当我在没有释放的情况下连续两次或多次持续犯罪时,问题就出现了。如果我们有hold1,hold2和hold3,则应在release3,release2,release1中读取此后的版本。
非常感谢任何帮助或建议。
CREATE TABLE "TBL_ACTIONS"
("ITEM_ID" CHAR(5 BYTE),
"ACTION_DATE" DATE,
"ACTION" VARCHAR2(40 BYTE)
);
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/11/16 01:00:28','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/11/16 01:12:45','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/11/16 16:00:19','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/11/16 16:12:40','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('20/11/16 14:52:42','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('20/11/16 16:00:10','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('24/11/16 17:52:49','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('24/11/16 21:12:40','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('24/11/16 23:12:48','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('24/11/16 23:32:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/11/16 14:40:17','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/11/16 14:40:18','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/11/16 19:12:40','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('29/11/16 08:17:59','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('29/11/16 08:17:59','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('29/11/16 08:18:40','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('29/11/16 08:18:40','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('01/12/16 11:03:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/12/16 08:10:51','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/12/16 11:27:06','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/12/16 11:27:11','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/12/16 07:10:07','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/12/16 07:10:07','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('16/12/16 14:37:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('16/12/16 14:37:40','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('22/12/16 12:20:52','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('22/12/16 12:20:52','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('22/12/16 12:29:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('22/12/16 12:29:39','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/01/17 10:23:47','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/01/17 10:23:48','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/01/17 11:52:26','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/01/17 11:52:26','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('09/01/17 13:47:52','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('09/01/17 13:47:59','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('25/01/17 09:42:35','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('25/01/17 09:42:35','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/01/17 12:11:24','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/01/17 12:11:31','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/01/17 12:12:20','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/01/17 12:12:28','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/02/17 09:01:48','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/02/17 09:01:48','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/02/17 09:02:58','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/02/17 09:02:58','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/02/17 16:57:47','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/02/17 16:57:47','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('07/02/17 16:56:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('07/02/17 16:56:47','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('07/02/17 16:57:12','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('07/02/17 16:57:21','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('08/02/17 16:41:54','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('08/02/17 16:41:54','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('08/02/17 17:21:53','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('08/02/17 17:22:04','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('09/02/17 16:38:35','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('09/02/17 16:38:43','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/02/17 07:40:05','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/02/17 07:40:13','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/02/17 23:31:52','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/02/17 23:31:52','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('11/02/17 09:16:22','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('11/02/17 09:16:23','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/02/17 16:48:13','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/02/17 16:48:13','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/02/17 16:49:16','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/02/17 16:49:17','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('21/02/17 15:42:40','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('21/02/17 17:25:08','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('21/02/17 17:25:09','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('21/02/17 17:25:18','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('21/02/17 17:25:18','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('27/02/17 16:51:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('27/02/17 16:51:39','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('27/02/17 16:51:46','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('27/02/17 16:51:46','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/02/17 14:06:59','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/02/17 14:06:59','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/02/17 14:07:10','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/02/17 14:07:10','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:04:08','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:04:08','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:04:09','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:04:09','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:23:27','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:23:27','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:23:27','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:23:27','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:55:55','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:55:55','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:55:55','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('03/03/17 10:55:55','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 13:23:25','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 13:23:25','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 13:23:26','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 13:23:26','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 15:07:20','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('06/03/17 15:10:09','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('08/03/17 15:35:45','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 12:17:36','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 12:17:37','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 13:22:47','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 13:22:55','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 13:23:20','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/03/17 13:23:27','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('18/03/17 07:12:27','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('18/03/17 07:12:28','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/03/17 16:17:54','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/03/17 17:52:44','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/03/17 18:12:49','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/03/17 23:32:45','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('01/04/17 20:32:34','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/04/17 19:53:43','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/04/17 20:52:44','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('04/04/17 21:39:18','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('04/04/17 21:45:23','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('05/04/17 20:54:49','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('05/04/17 21:00:19','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('10/04/17 17:35:13','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/04/17 11:29:33','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/04/17 21:32:44','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('18/04/17 00:40:12','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('23/04/17 09:20:35','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('23/04/17 10:52:41','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('25/04/17 20:40:50','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('25/04/17 20:52:47','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/04/17 01:13:17','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/04/17 04:32:35','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/04/17 16:12:47','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/04/17 16:20:08','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/04/17 16:40:32','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/04/17 18:20:19','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('01/05/17 03:12:06','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/05/17 16:58:17','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('17/05/17 16:58:17','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/05/17 15:10:28','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('19/05/17 15:10:28','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('28/05/17 21:14:06','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('30/05/17 15:41:58','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/05/17 10:05:00','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/05/17 10:05:11','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/05/17 10:05:17','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('31/05/17 10:05:27','DD/MM/YY HH24:MI:SS'),'Hold');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('02/06/17 00:30:22','DD/MM/YY HH24:MI:SS'),'Release');
Insert into TBL_ACTIONS (ITEM_ID,ACTION_DATE,ACTION) values ('ITEM1',to_date('04/06/17 06:43:33','DD/MM/YY HH24:MI:SS'),'Hold');
答案 0 :(得分:1)
根据评论,我假设您有一个可用于订购的ID列。我想可能有更有效的方法来做以下事情(我会搜索Last-In,First-Out或LIFO),但我想看看我能用自己的力量想出什么:
With PushPop as (
select
nh1.Id,
nh1.Activity,
sum(case when nh2.Activity = 'Release' then -1 else 1 end) as PushPop
from
NestedHolds nh1
LEFT JOIN NestedHolds nh2 ON
nh1.Id >= nh2.Id
group by
nh1.Id,
nh1.Activity
order by
nh1.id
)
SELECT
nh.*,
NewIDs.NewID
from
NestedHolds nh
JOIN
(
SELECT
a.Id HoldId,
min(b.Id) as ReleaseId,
row_number() over (order by a.Id) NewID
FROM
PushPop a
JOIN PushPop b ON
a.Id < b.Id
AND a.PushPop = b.PushPop + 1
WHERE
a.Activity = 'Hold'
AND b.Activity = 'Release'
GROUP BY
a.Id
order by
a.Id
) NewIDs on nh.Id in (NewIDs.HoldID, NewIDs.ReleaseID)
order by nh.id
;