sql - LAST_VALUE() with ASC and FIRST_VALUE with DESC return different results -
i have trouble using last_value()
window function in google bigquery.
in understanding, following 2 columns should return same results, return different results , seems 1 first_value()
correct.
select first_value(status) on (partition userid order timestamp desc), last_value(status) on (partition userid order timestamp asc) [table]
did make mistake?
there's subtlety on how over() functions work when have (order by): work incrementally.
see query:
select x, y, first_value(x) over(order y) first, last_value(x) over(order y desc) last, sum(x) over() plain_sum_over, sum(x) over(order y) sum_over_order (select 1 x, 1 y),(select 2 x, 2 y),(select 3 x, 3 y),(select 4 x, 4 y)
the plain_sum_over
, sum_over_order
reveal secret: order incremental results - , that's witnessing in results.
Comments
Post a Comment