Тогда это может быть что-то вроде этого:
WITH
-- your input ...
input(level,attempt,score) AS (
SELECT 1,1,2
UNION ALL SELECT 1,2,3
UNION ALL SELECT 2,1,3
UNION ALL SELECT 3,1,3
UNION ALL SELECT 4,1,1
UNION ALL SELECT 4,2,3
UNION ALL SELECT 5,1,2
UNION ALL SELECT 5,2,2
UNION ALL SELECT 5,3,3
)
-- your input ends here
, -- replace comma with WITH in real query ..
-- creating a table with 5 rows per each of the 3 attempts
frame (level,attempt) AS (
SELECT
i.level
, a.attempt
FROM input i
CROSS JOIN (
SELECT DISTINCT
attempt
FROM input
) a
WHERE i.attempt=1
)
-- SELECT * FROM frame; -- un-comment this line to test the frame table
,
gapfilled AS (
-- Query before GROUPing: left join the frame table with the input table
-- and fill the resulting NULLs using the LAST_VALUE ( ... IGNORE NULLS)
-- OLAP function. If you can take a previous one, pick it, if not , pick
-- a following one.
-- Vertica has named OLAP windows, which we use here - one forward, one backward
SELECT
frame.level
, NVL(
LAST_VALUE(input.attempt IGNORE NULLS) OVER(fwd)
, LAST_VALUE(input.attempt IGNORE NULLS) OVER(bwd)
) AS attempt
, NVL(
LAST_VALUE(input.score IGNORE NULLS) OVER(fwd)
, LAST_VALUE(input.score IGNORE NULLS) OVER(bwd)
) AS score
FROM frame LEFT JOIN input USING(level,attempt)
WINDOW fwd AS (PARTITION BY frame.attempt ORDER BY frame.level)
, bwd AS (PARTITION BY frame.attempt ORDER BY frame.level DESC)
)
-- SELECT * FROM gapfilled ORDER BY 2,1; -- UN-comment to test gapfilled table
SELECT
attempt
, SUM(score) AS score_sum
FROM gapfilled
GROUP BY
attempt;
-- out attempt | score_sum
-- out ---------+-----------
-- out 1 | 11
-- out 2 | 14
-- out 3 | 15