简体   繁体   中英

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. 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. (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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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