以下表为例:
ORDER_KEY | STATUS_DATE | TASK_CREATED_DATE | TASK_KEY | TASK_ROW_ID | TASK_REVISION_ID
-----------|-----------------|-------------------|------------|-------------|------------------
1274796898 | 10/4/2019 18:19 | 10/4/2019 18:12 | 5277852673 | 1 |
1274796898 | 10/4/2019 18:19 | 10/4/2019 18:12 | 5277852674 | 2 |
1274796898 | 10/4/2019 18:19 | 4/7/2020 14:22 | 5277853673 | 3 | 1
1274796898 | 10/4/2019 18:19 | 4/7/2020 14:22 | 5277853674 | 4 | 1
1274796898 | 10/4/2019 18:19 | 4/7/2020 14:22 | 5277853675 | 5 | 1
1274737653 | 10/4/2019 15:32 | 10/4/2019 15:31 | 5277852586 | 1 |
1274737653 | 10/4/2019 15:32 | 10/4/2019 15:31 | 5277852587 | 2 |
1274737653 | 10/4/2019 15:32 | 10/4/2019 15:31 | 5277852588 | 3 |
1274737653 | 10/4/2019 15:32 | 10/4/2019 15:32 | 5277852589 | 4 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:42 | 5277855586 | 1 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:42 | 5277855587 | 2 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:42 | 5277855588 | 3 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:42 | 5277855589 | 4 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:42 | 5277855590 | 5 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:49 | 5277855587 | 6 |
1274737647 | 10/4/2019 11:49 | 10/4/2019 11:49 | 5277856270 | 7 |
1274737647 | 10/4/2019 11:49 | 4/7/2020 14:10 | 5281403575 | 8 | 1
1274737647 | 10/4/2019 11:49 | 4/7/2020 14:10 | 5281403576 | 9 | 1
1274737647 | 10/4/2019 11:49 | 4/7/2020 14:12 | 5281403595 | 10 | 1
1274737647 | 10/4/2019 11:49 | 4/14/2020 09:32 | 5281403599 | 11 | 2
1274737647 | 10/4/2019 11:49 | 4/26/2020 13:10 | 5281403600 | 12 | 3
可通过以下查询复制:
with data as (
select *
from (
values
(1274796898, '10/4/2019 18:19', '10/4/2019 18:12', 5277852673, 1),
(1274796898, '10/4/2019 18:19', '10/4/2019 18:12', 5277852674, 2),
(1274796898, '10/4/2019 18:19', '4/7/2020 14:22', 5277853673, 3),
(1274796898, '10/4/2019 18:19', '4/7/2020 14:22', 5277853674, 4),
(1274796898, '10/4/2019 18:19', '4/7/2020 14:22', 5277853675, 5),
(1274737653, '10/4/2019 15:32', '10/4/2019 15:31', 5277852586, 1),
(1274737653, '10/4/2019 15:32', '10/4/2019 15:31', 5277852587, 2),
(1274737653, '10/4/2019 15:32', '10/4/2019 15:31', 5277852588, 3),
(1274737653, '10/4/2019 15:32', '10/4/2019 15:32', 5277852589, 4),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:42', 5277855586, 1),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:42', 5277855587, 2),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:42', 5277855588, 3),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:42', 5277855589, 4),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:42', 5277855590, 5),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:49', 5277855587, 6),
(1274737647, '10/4/2019 11:49', '10/4/2019 11:49', 5277856270, 7),
(1274737647, '10/4/2019 11:49', '4/7/2020 14:10', 5281403575, 8),
(1274737647, '10/4/2019 11:49', '4/7/2020 14:10', 5281403576, 9),
(1274737647, '10/4/2019 11:49', '4/7/2020 14:12', 5281403595, 10),
(1274737647, '10/4/2019 11:49', '4/14/2020 09:32', 5281403599, 11),
(1274737647, '10/4/2019 11:49', '4/26/2020 13:10', 5281403600, 12)
) v (ORDER_KEY, STATUS_DATE, TASK_CREATED_DATE, TASK_KEY, TASK_ROW_ID)
)
select * from data;
我最终想要实现的行为是 TASK_REVISION_ID
列输出。
在上面的数据中,它是手动输入的,以便于说明。
TASK_ROW_ID = ROW_NUMBER() OVER(PARTITION BY ORDER_KEY ORDER BY TASK_CREATED_DATE, TASK_KEY)
在 ORDER_KEY
级别,我想增加 TASK_REVISION_ID
以以下方式:
套 TASK_REVISION_ID
到1时 TASK_CREATED_DATE
> STATUS_DATE
增量 TASK_REVISION_ID
当当前行之间以分钟为单位的差值 TASK_CREATED_DATE
和上一行 TASK_CREATED_DATE
>= 5
2条答案
按热度按时间e3bfsja21#
你只需要稍微修改一下原来的答案就可以看出正确的想法:
nszi6y052#
这回答了问题的原始版本。
您所描述的内容由以下查询捕获:
这是一把小提琴。