Skip to content
Advertisement

How to combine BigQuery LAST_VALUE() and ARRAY_AGG()

Here is a toy example:

For each row, I would like to find the last row of distinct id2 within id1. For example for row #11 the output should be an array of two elements “row #9, row#10”, and for row #14 it is “row #11,row #13”.

here is an example output: enter image description here

Advertisement

Answer

Below is for BigQuery Standard SQL

If to apply to sample data from your question – output is

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement