I have below data which needs to be formatted to display only consecutive days without a break.
For example, I want to display the records with dates from 2018-06-18 until 2018-06-21 as these are consecutive days. But then I don't want to show rows with dates of 2018-06-27 or 2018-07-03 as these days don't have a sequence.
The exception is for Friday-Monday where there will be obvious gap. For example if the dates are 2018-06-21 (Thursday), 2018-06-22 (Friday) and 2018-06-25 (Monday), than that will be a valid sequence.
Sequence of 2018-6-1, 2018-6-3, 2018-6-4 has to be hidden as it is broken (there was no record of 2018-6-2 in the table).
I had written below, but it fails to deliver the output I am looking for. Any help will be greatly appreciated.
select date, diff, Percent_gain, day, month, Year
from (
select date
,datediff(day, '2018-1-29',date) - coalesce(lag(datediff(day, '2018-1-29', date)) over (order by date), 0) as diff
,Percent_gain, day, month, year
from tab
) t1
where t1.diff < 2
and t1.Percent_gain < 0
Output

