This pull request looks great and I'd really like to pull it in. But I'd still like to see the syntax tweaked to be more standard. It would look like this instead:

    SELECT LAST_VALUE(val) WITHIN GROUP (ORDER BY date ASC) FROM firstValue GROUP BY page_id

and the version with the offset argument would be:

    SELECT NTH_VALUE(val, 2) WITHIN GROUP (ORDER BY date ASC) FROM firstValue GROUP BY page_id

Everything else would be the same - we already support this syntax in our grammer - you'd define an NTH_VALUE built-in that could be derived from you FIRST_VALUE/LAST_VALUE built-in. This way our syntax is in line with other databases and when we introduce support for PARTITION BY down the road, we can add that syntax without breaking b/w compat.

What do you think, @tzolkincz?