我有以下数据:
Client_id Call_started Call_ended Outcome_id
----------- -------------------------- -------------------------- ----------------
111 Aug. 21, 2018, 4:10 p.m. Aug. 21, 2018, 4:13 p.m. Rescheduled
111 Aug. 22, 2018, 1:00 p.m. Aug. 22, 2018, 1:10 p.m. Rescheduled
112 Aug. 21, 2018, 3:10 p.m. Aug. 21, 2018, 3:11 p.m. Rescheduled
111 Aug. 22, 2018, 5:00 p.m. Aug. 22, 2018, 5:08 p.m. Interested
113 Aug. 22, 2018, 1:00 p.m. Aug. 22, 2018, 1:10 p.m. Rescheduled
114 Aug. 21, 2018, 2:10 p.m. Aug. 21, 2018, 2:11 p.m. NotReachable
112 Aug. 22, 2018, 9:10 a.m. Aug. 22, 2018, 9:20 a.m. NotInterested
113 Aug. 22, 2018, 5:20 p.m. Aug. 22, 2018, 5:25 p.m. Interested
下面是重新安排了客户呼叫的SQL查询
Select a.client_id, a.call_start,a.call_end, a.outcome_id
From client_analysis a
where a.outcome_id like %Rescheduled%'
现在,我还想选择后续重新安排的呼叫发生了什么(被按时呼叫等等)。如何选择对给定client.id的其他呼叫(或下一个a.call_start),其中a.outcome_id被重新计划(下一个a.call_start <重新计划a.call_end)?
以下是预期的输出:
Client_id Call_started Call_ended Outcome_id
----------- -------------------------- -------------------------- ----------------
111 Aug. 21, 2018, 4:10 p.m. Aug. 21, 2018, 4:13 p.m. Rescheduled
111 Aug. 22, 2018, 1:00 p.m. Aug. 22, 2018, 1:10 p.m. Rescheduled
111 Aug. 22, 2018, 5:00 p.m. Aug. 22, 2018, 5:08 p.m. Interested
112 Aug. 21, 2018, 3:10 p.m. Aug. 21, 2018, 3:11 p.m. Rescheduled
112 Aug. 22, 2018, 9:10 a.m. Aug. 22, 2018, 9:20 a.m. NotInterested
113 Aug. 22, 2018, 1:00 p.m. Aug. 22, 2018, 1:10 p.m. Rescheduled
113 Aug. 22, 2018, 5:20 p.m. Aug. 22, 2018, 5:25 p.m. Interested
Call_start在给定client_id的同一列中,并且根据开始时间,我们可以确定调用的顺序。
答案 0 :(得分:1)
这可以通过查找满足条件的客户的下一个电话(重新安排电话,查找下一个电话)来完成
这使用两个概念,LEAD-查找下一个值;和CASE WHEN-何时应用LEAD的条件
尝试以下SQL:
enable_shared_from_this
逐行解释:
仅当列Outcome_ID的行等于Resceduled时,CASE才执行语句。
如果重新安排了时间,LEAD将为下一个呼叫(ORDER BY呼叫已开始的ASC)的Client_ID(想想PARTITION BY与GROUP BY类似)查找Call_Started值
如果您想在此新列中放置与另一列不同的值,例如下一个呼叫的结束日期,请用LEAD(Call_Ended)等替换LEAD(Call_Started)等