I love y'alls website! It has been a lot of fun to get on, practice, and see how other people answer the same question in many different ways. Thank you
Great question! In my opinion it's not right to include the call made on 2022-07-06 because there is only one call made on that day. The way the question is asked it seems like we should filter out that day. In my solution I filtered it out by getting only the days where there is more than one call.
i couldnt figure out the assumptions but this is what i came up with... select x.* from (select *, case when datepart(day,date_called)=lead((datepart(day,date_called))) over(partition by caller_id order by date_called) and recipient_id =lead(recipient_id) over (partition by caller_id order by date_called) then 1 else 0 end as flag from caller_history) x where x.flag=1
My solution with first_last_calls as ( select *, first_value(recipient_id) over (partition by caller_id,date_called::DATE order by date_called asc) as first_call_for_that_day, first_value(recipient_id) over (partition by caller_id, date_called::DATE order by date_called desc range between unbounded preceding and unbounded following) as last_call_for_that_day from caller_history) select distinct caller_id, recipient_id, date_called::DATE from first_last_calls where first_call_for_that_day = last_call_for_that_day;
This channel is a gold mine of info!
I love y'alls website! It has been a lot of fun to get on, practice, and see how other people answer the same question in many different ways. Thank you
Thank you! Appreciate the kind words. If you have any feedback or feature requests, you can always reach out to me here or at team@stratascratch.com.
Great! Thank you so much!
Glad it was helpful!
Great question!
In my opinion it's not right to include the call made on 2022-07-06 because there is only one call made on that day.
The way the question is asked it seems like we should filter out that day.
In my solution I filtered it out by getting only the days where there is more than one call.
i couldnt figure out the assumptions but this is what i came up with...
select x.* from
(select *,
case when datepart(day,date_called)=lead((datepart(day,date_called))) over(partition by caller_id order by date_called)
and recipient_id =lead(recipient_id) over (partition by caller_id order by date_called)
then 1 else 0 end as flag
from caller_history) x
where x.flag=1
Can we write case statement in first_value() function?
I think so. What would it look like? Try it in the coding editor. There's a link in the description.
My solution
with first_last_calls as (
select *,
first_value(recipient_id) over (partition by caller_id,date_called::DATE order by date_called asc) as first_call_for_that_day,
first_value(recipient_id) over (partition by caller_id, date_called::DATE order by date_called desc range between unbounded preceding and unbounded following) as last_call_for_that_day
from caller_history)
select distinct caller_id, recipient_id, date_called::DATE
from first_last_calls
where first_call_for_that_day = last_call_for_that_day;