I am looking into phone calls to customers and I need a column that count the results for previous calls to the same customer.
E.g how many of the prior calls has ended with the result “No Contact”
I have been looking into the CASE WHEN
function and the LAG
function, but I haven’t been able get the result I am looking for
The below and example of what I am trying to achieve (the PriorNotInterested column):
Advertisement
Answer
You want a conditional window sum()
between the first record of the client and the record preceeding the current one. Most RDBMS support the rows between
clause in window functions:
x
select
t.*,
sum(case when Result = 'Not Interested' then 1 else 0 end)
over (
partition by customer_id
order by Call_date
rows between unbounded preceding and 1 preceding
) CountNotIntererstedPrior
from mytable t