简体   繁体   中英

Find 3 or more consecutive transaction record where the transaction amount greater than 100 and the records belong to the same category

I have a customer transaction table which has 3 columns, id, Category, TranAmount. Now I want to find 3 or more consecutive transaction records which belongs to the same category and the TranAmount greater than 100. Below is the sample table:

Id    Category   TranAmount
 1           A          190
 2           A          160
 3           A          169  
 4           B          190
 5           A          90
 6           B          219
 7           B          492
 8           B          129
 9           B          390
 10          B          40
 11          A          110
 12          A          130

And the output should be:

Id    Category   TranAmount
 1           A          190
 2           A          160
 3           A          169
 6           B          219
 7           B          492
 8           B          129
 9           B          390

Look into "gaps and islands" reference for a deeper understanding of the approach. Here's one of many you could read: https://www.red-gate.com/simple-talk/sql/t-sql-programming/the-sql-of-gaps-and-islands-in-sequences/

In this specific problem you have two conditions that cause a break in a consecutive series, those being a change in category or an amount that doesn't meet the threshold.

with data as (
    select *,
        row_number() over (order by Id) as rn,
        row_number() over (partition by
            Category, case when TranAmount >= 100 then 1 else 0 end order by Id) as cn
    from Transactions
), grp as (
    select *, count(*) over (partition by rn - cn) as num
    from data
    where TranAmount >= 100
)
select * from grp where num >= 3;

https://rextester.com/DUM44618

I can't really test this out yet but give this a try.

SELECT Id, Category, Amount FROM Table 
WHERE Amount > 100
and Category IN 
 (SELECT Category FROM Table
  WHERE Amount > 100
  GROUP BY Category HAVING COUNT (Category ) >= 3)

This will work if there are no gaps between the ids:

select distinct t.* 
from tablename t inner join (
  select t.id from tablename t 
  where t.tranamount > 100
  and
  exists (
    select 1 from tablename
    where id = t.id - 1 and category = t.category and tranamount > 100
  )
  and
  exists (
    select 1 from tablename
    where id = t.id + 1 and category = t.category and tranamount > 100
  )
) tt on t.id in (tt.id - 1, tt.id, tt.id + 1)

See the demo .
Results:

Id | Category | TranAmount
-: | :------- | ---------:
 1 | A        |        190
 2 | A        |        160
 3 | A        |        169
 6 | B        |        219
 7 | B        |        492
 8 | B        |        129
 9 | B        |        390

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