postgresql 从客户活动中查找放弃的搜索

omhiaaxx  于 2023-06-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(105)

在交易平台中,用户有三种不同的行为:LOGINSEARCHORDER
放弃的SEARCH操作定义为:客户LOGIN然后做一些SEARCH,并且在下一个LOGIN之前不做ORDER。我有一张table:action_table,记录customer_idactionrequest_time
我想知道如何通过SQL找到所有被放弃的SEARCH操作?
下面是一个简单的例子:
| 客户ID|作用|请求时间|
| - -----|- -----|- -----|
| 一个|登录|2023- 05 -01 2023- 05 -01|
| 一个|搜索|2023- 05 -02 2023- 05 -02|
| 一个|在线订单|2023-5-03|
| B|登录|2023- 05 -01 2023- 05 -01|
| B|搜索|2023- 05 -02 2023- 05 -02|
| B|登录|2023- 05 -03 2023- 05 -03|
| B|搜索|2023-5-04|
在这种情况下,**否 * 放弃搜索A,2放弃搜索B。
下面是我的代码:

select customer_id, count(1)
from action_table c1
left join 
(
  select customer_id, action, request_time 
  from action_table
  where action = 'LOGIN'
) c2
  on c1.customer_id = c2.customer_id
  and c2.request_time > c1.request_time
left join 
(
  select customer_id, action, request_time 
  from action_table
  where action = 'ORDER'
) c3
  on c1.customer_id = c3.customer_id
  and c3.request_time > c1.request_time
  and c3.request_time < c2.request_time
where c1.action = 'SEARCH'
  and c2.customer_id IS NOT NULL
  and c3.customer_id IS NULL
group by 1

它是多余的,似乎工作不好:(

pgvzfuti

pgvzfuti1#

一个选项使用LEAD;其思想是检查每个登录事件的以下两个动作以识别放弃的搜索:

select customer_id, count(*) cnt_abandonned_search
from (
    select a.*, 
        lead(action, 1) over(partition by customer_id order by request_time) lead_action_1,
        lead(action, 2) over(partition by customer_id order by request_time) lead_action_2
    from action_table a
) a
where action = 'LOGIN'                         -- a LOGIN action...
  and lead_action_1 = 'SEARCH'                 -- ... followed by a SEARCH
  and lead_action_2 is distinct from 'ORDER'   -- ... but not followed by an ORDER
group by customer_id

相关问题