How to get first and last value by same item in sequence, with possibility of the same item occurring again
WITH 
cte1 AS ( SELECT *, 
                 CASE WHEN name = LAG(name) OVER (ORDER BY id) 
                      THEN 0 
                      ELSE 1 
                      END the_same
          FROM test ),
cte2 AS ( SELECT *, SUM(the_same) OVER (ORDER BY id) group_num
          FROM cte1 )
SELECT DISTINCT
       name, 
       FIRST_VALUE(value) OVER (PARTITION BY group_num ORDER BY id) value_1st,
       FIRST_VALUE(value) OVER (PARTITION BY group_num ORDER BY id DESC) value_last 
FROM cte2;


fiddle