简体   繁体   English

查找3个或更多连续交易记录,其中交易金额大于100且属于同一类别的记录

[英]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.我有一个客户交易表,它有 3 列,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:现在我想找到 3 个或更多属于同一类别且 TranAmount 大于 100 的连续交易记录。以下是示例表:

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/这是您可以阅读的众多内容之一: 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 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:如果 id 之间没有间隙,这将起作用:

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

暂无
暂无

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

相关问题 SQL查询显示连续3行或更多行且人数超过100人的记录 - SQL query to display the records which have 3 or more consecutive rows and the amount of people more than 100 查找具有 3 个或更多具有相同值的连续记录的记录 - Find records with 3 or more consecutive records with same value 查找交易文件中的事件,其中100个交易中顺序出现5个以上序列号 - Finding events in a transaction file where more than 5 serial numbers appear in sequence within 100 transactions SQL Server:查找大于 5 的最近连续记录 - SQL Server : find recent consecutive records that are greater than 5 根据用户交易数据计算跨类别销售-适用于进行过1次以上交易的用户 - Computing cross-category sales from user transaction data - For users who have done more than 1 transaction 查找连续值长度小于阈值的记录 - Find records where length of consecutive values is less than threshold 找到最高的金额,尽管有不止一个达到相同的金额 - Find the highest amount although there are more than one achieving the same amount 查询每个项目的金额大于100的订单的总金额 - Query Total Amounts for Orders where the amount of each item was greater than >100 查找日期大于同一表中匹配条目的记录 - find records with date greater than matching entry in same table MYSQL - 获取具有相同ID的1条以上记录的所有记录 - MYSQL - Get all records that have more than 1 record for the same id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM