在交易平台中,用户有三种不同的操作:LOGIN
, SEARCH
, ORDER
.
被遗弃SEARCH
操作定义为:客户LOGIN
然后做一些SEARCH
并且不要ORDER
之前 下一个 LOGIN
。
现在我有一张 table :action_table
,录音customer_id
, action
, request_time
.
我想知道如何通过 SQL 查找所有废弃的 SEARCH 操作?
更新: 这是一个简短的示例:
CREATE TABLE action_table(
customer_id VARCHAR(1) NOT NULL
,action VARCHAR(6) NOT NULL
,request_time DATE NOT NULL
);
INSERT INTO action_table(customer_id,action,request_time)
VALUES
('A','LOGIN','2023-05-01'),
('A','SEARCH','2023-05-02'),
('A','SEARCH','2023-05-03'),
('A','ORDER','2023-05-04'),
('B','LOGIN','2023-05-01'),
('B','SEARCH','2023-05-02'),
('B','SEARCH','2023-05-03'),
('B','LOGIN','2023-05-04'),
('B','SEARCH','2023-05-05')
在本例中,NO 放弃了对 A 的搜索,3 放弃了对 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
这是多余的,而且似乎效果不佳:(
最佳答案
一个选项使用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
如果您想专注于搜索,并获取每个放弃搜索的完整记录,我们可以使用相同的逻辑,但使用 lead
和 lag
:
select *
from (
select a.*,
lag (action) over(partition by customer_id order by request_time) lag_action,
lead(action) over(partition by customer_id order by request_time) lead_action
from action_table a
) a
where action = 'SEARCH' -- a SEARCH action...
and lag_action = 'LOGIN' -- ... preceded by a LOGIN
and lead_action is distinct from 'ORDER' -- ... but not followed by an ORDER
关于sql - 从客户事件、SQL 中查找放弃的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76388701/