Is there any way to get row number for each record in BigQuery? (From the specs, I haven't seen anything about it) There is a NTH() function, but that applies to repeated fields.

There are some scenarios where row number is not necessary in BigQuery, such as the use of TOP() or LIMIT function. However, I need it to simulate some analytical functions, such as a cumulative sum(). For that purpose I need to identify each record with a sequential number. Any workaround on this?

Thanks in advance for your help!

Leo

## Best Solution

2018 update: If all you want is a unique id for each row2018 #standardSQL solution:Ok, let's reproduce that error:

Yes - that happens because OVER() needs to fit all data into one VM - which you can solve with PARTITION:

Ok, ok. Let's use partitions to give a row number to each row, and let's combine that row number with the partition fields to get an unique id per row:

The original 2013 solution:

Good news: BigQuery now has a row_number function.

Simple example:

More complex, working example: