Here is my dummy dataset.
ID Order Case Date_created
123456 25800265 1 2018-06-27 07:40:23
123456 25800265 1 2018-06-25 05:29:23
123456 25800265 0 2018-07-26 06:16:28
789454 25906588 1 2018-07-12 05:59:50
789454 25906588 0 2018-07-12 07:41:29
789454 25906588 0 2018-07-10 05:43:45
789454 25906588 0 2018-07-09 05:59:26
789454 25906588 0 2018-07-05 10:39:45
287541 32140567 0 2018-07-12 07:41:29
287541 32140567 0 2018-07-10 05:43:45
287541 32140567 0 2018-07-09 05:59:26
287541 32140567 0 2018-07-05 10:39:45
I need only one record for each order based on the following conditions. Return the record where Case=1 when an Order contains both 0 and 1 in 'Case'. If multiple records are present where Case=1 then get the old Date_created record. If the Order has only Case=0, then return the record with oldest Date_created date.
i.e.
ID Order Case Date_created
123456 25800265 1 2018-06-25 05:29:23
789454 25906588 1 2018-07-12 05:59:50
287541 32140567 0 2018-07-05 10:39:45
In Redshift I could accomplish this using the following code.
select * from
(
select *,
ROW_NUMBER()over(partition by Order order by Case desc,Date_created) as latest_time
from tbl
)where latest_time=1
How do i accomplish this in R?