【问题标题】:How to fetch which users did transaction consecutively?如何获取连续交易的用户?
【发布时间】:2019-04-08 10:28:11
【问题描述】:

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


    【解决方案1】:

    试试这个,

    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
    

    【讨论】:

      【解决方案2】:

      这会选择交易时间和交易时间后 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 2012-06-25
        • 1970-01-01
        • 2023-01-05
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多