The idea is to create an sorted cummulative array and take second element.
PostgreSQL(it could be easily extended if 3rd/4th/5th element is required by adjusting array index):
SELECT t.*,
(sort(ARRAY_AGG(t.value) OVER(ORDER BY t.n), 'desc'))[2] AS sec_max
FROM t
ORDER BY n;
db<>fiddle demo
Ufnortunately Snowflake does not support cummulative ARRAY_AGG/STRING_AGG.
Below the version that build cummulative array using recursive cte, then array is sorted and second element is taken.
Data prep:
CREATE OR REPLACE TABLE t
AS
SELECT 1 AS n, 1 AS value
UNION ALL SELECT 2,4
UNION ALL SELECT 3,4
UNION ALL SELECT 4,6
UNION ALL SELECT 5,4
UNION ALL SELECT 6,8;
Helper function:
CREATE OR REPLACE FUNCTION array_sort_desc(a array)
RETURNS array
LANGUAGE JAVASCRIPT
AS
$$
return A.sort().reverse();
$$
;
Main query:
WITH src AS (
SELECT *, ROW_NUMBER() OVER(ORDER BY n) AS rn FROM t
),cte AS (
SELECT *, ARRAY_CONSTRUCT(src.value) AS arr
FROM src
WHERE rn=1
UNION ALL
SELECT src.*, ARRAY_APPEND(arr, src.value)
FROM src
JOIN cte
ON cte.rn=src.rn-1
)
SELECT cte.n, cte.value, arr, array_sort_desc(arr), array_sort_desc(arr)[1] AS sec_max
FROM cte
ORDER BY n;
/*
+---+-------+-----------------------------------+-----------------------------------+---------+
| N | VALUE | ARR | ARRAY_SORT_DESC(ARR) | sec_max |
+---+-------+-----------------------------------+-----------------------------------+---------+
| 1 | 1 | [ 1 ] | [ 1 ] | |
| 2 | 4 | [ 1, 4 ] | [ 4, 1 ] | 1 |
| 3 | 4 | [ 1, 4, 4 ] | [ 4, 4, 1 ] | 4 |
| 4 | 6 | [ 1, 4, 4, 6 ] | [ 6, 4, 4, 1 ] | 4 |
| 5 | 4 | [ 1, 4, 4, 6, 4 ] | [ 6, 4, 4, 4, 1 ] | 4 |
| 6 | 8 | [ 1, 4, 4, 6, 4, 8 ] | [ 8, 6, 4, 4, 4, 1 ] | 6 |
+---+-------+-----------------------------------+-----------------------------------+---------+
*/