如何获取连续进行交易的用户?

时间:2019-04-08 10:28:12

标签: mysql sql transactions timestamp intervals

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分钟的人,但是我缺乏知识。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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