简体   繁体   English

如何查找字段值为x且第二行为x + 1的两行

[英]How to find two rows where a field value is x and 2nd row is x + 1

I have a list of records (transactions). 我有一个记录列表(事务)。

I want to be able to ONLY include rows where the transaction numbers have a sequential value +1. 我只希望能够包含交易号具有连续值+1的行。 Not even sure where to start to get it to list ONLY those transactions. 甚至不确定从哪里开始仅列出那些交易。 I have it working to list all transactions sequentially, but not isolate just the transaction values, plus their + 1 transactions. 我可以按顺序列出所有事务,但不能仅隔离事务值及其+1事务。 (note that not all transactions values are sequential). (请注意,并非所有交易值都是连续的)。

For example, 例如,

If field A has values of 1,2,4,7,8,10 I want the script to just list 1,2,7,8 as results. 如果字段A的值为1,2,4,7,8,10,我希望脚本只列出1,2,7,8作为结果。

Thanks in advance. 提前致谢。

You want groups of at least two consecutive values? 您想要至少两个连续值的组?

SELECT * FROM tab
QUALIFY 
   MIN(a) OVER (ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) = a - 1
OR MIN(a) OVER (ORDER BY a ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) = a + 1

you can try something like this: 您可以尝试这样的事情:

select * from table as t1
where exists (
    select * from table as t2
    where t2.A = t1.A-1 or t2.A = t1.A+1
)
order by t1.A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM