Повторите запрос, но упорядочите по продукту и времени. Для флага вам нужен один случай, поэтому, если он повторяется, вы получите его:
Эта проверка, только если есть повторение, следующее
with Input_table as
(
select 1 as userid,'2020-01-10 8:00:00' as time, 'A' as product
union select 1 ,'2020-01-10 9:00:00', 'B'
union select 1 ,'2020-01-10 9:00:00', 'A'
union select 1 ,'2020-01-10 10:00:00', 'C'
union select 1 ,'2020-01-10 10:00:00', 'B'
union select 1 ,'2020-01-10 11:00:00', 'D'
union select 1 ,'2020-01-10 11:00:00', 'E'
union select 1 ,'2020-01-10 11:00:00', 'A'
),
cte2 as
(
SELECT userid, "time", product,
CASE WHEN Lead(product) OVER (partition by userid order by product, time) = product THEN 1 else 0 END as "Is_Repeated?"
FROM Input_table
)
select * from cte2 order by time
, если вы хотите пометить все повторное использование :
with Input_table as
(
select 1 as userid,'2020-01-10 8:00:00' as time, 'A' as product
union select 1 ,'2020-01-10 9:00:00', 'B'
union select 1 ,'2020-01-10 9:00:00', 'A'
union select 1 ,'2020-01-10 10:00:00', 'C'
union select 1 ,'2020-01-10 10:00:00', 'B'
union select 1 ,'2020-01-10 11:00:00', 'D'
union select 1 ,'2020-01-10 11:00:00', 'E'
union select 1 ,'2020-01-10 11:00:00', 'A'
),
cte2 as
(
SELECT userid, "time", product,
CASE WHEN Lead(product) OVER (partition by userid order by product, time) = product THEN 1 else 0 END as "Is_Repeated?"
FROM Input_table
),
cte3 as
(
select userid, product, max("Is_Repeated?") as "Is_Repeated?" from cte2 group by userid, product
)
select a.userid, a.product, "time", "Is_Repeated?"
from Input_table a inner join cte3 b on a.userid = b.userid and a.product = b.product order by "time"