如何连续获取哪些用户进行了交易?

问题描述 投票:0回答:2

table_1包含以下列:user_id, order_id, transaction_time

transaction_time的数据类型是timestamp

SELECT user_id 
FROM table_1
WHERE transaction_time > date_sub(transaction_time, interval 20 MINUTE_SECOND)

我想让连续完成交易的人不到20分钟,但我缺乏这方面的知识。

mysql sql transactions timestamp intervals
2个回答
0
投票

试试这个,

SELECT user_id,  
       case when sum(case when  transaction_time between transaction_time and dateadd(minute,20,transaction_time) then 1 else 0 end )  > 1 
            then 'User did transaction consecutively' 
            else 'No consecutive Transaction' end
FROM table_1
group by user_id

0
投票

这将选择交易时间和交易时间后20分钟的窗口,并计算该用户在该窗口中的交易数量,然后返回计数大于1的位置

select
  user_Id,
  transaction_time, 
  date_add(transaction_time, interval 20 MINUTE_SECOND)
  count(*) countofTransactions
from table_1
group by 
  user_Id,
  transaction_time, 
  date_add(transaction_time, interval 20 MINUTE_SECOND)
having count(*)>1 --having more than one transaction in the window
© www.soinside.com 2019 - 2024. All rights reserved.