I have a table:
account | onln_status | browse status | beg_date | end_date
----------+-------------+-----------------+----------+-------------
123456789 | On | Y | 1/1/2018 | 2/1/2018
123456789 | On | N | 2/2/2018 | 4/1/2018
123456789 | On | Y | 4/2/2018 | 5/1/2018
123456789 | Off | N | 5/2/2018 | 7/1/2018
123456789 | Off | Y | 7/2/2018 | 8/1/2018
123456789 | On | Y | 8/2/2018 | 10/1/2018
123456789 | On | N | 10/2/2018| 11/1/2018
and need the result to show :
account | onln_status | beg_date | end_date
----------+-------------+------------+------------
123456789 | On | 1/1/2018 | 5/1/2018
123456789 | Off | 5/2/2018 | 8/1/2018
123456789 | On | 8/2/2018 | 11/1/2018
At first, I used using min(beg date) and max(end date) but it doesn't work in this situation:
select
omsid, onln_status, min(beg_date), max(end_date)
from
table
group by
omsid, onln_status
I also tried getting a unique number whenever the previous online_status changes, but could not get a way to just add on to the number:
select
*,
case
when onln_status <> lag(onln_status) over (partition by account order by beg_date)
then 1
else 0
end as status_change
from
table