🗃️Repeated / Nested Fields
What are they and how to query them
Last updated
What are they and how to query them
Last updated
A repeated or nested field is a type of data type where multiple rows can exists for a single record in the database. They are often used when multiple sets of the information exists and belong to the same record. In the example below, for a single address, they can hold multiple tokens.
This is what the data looks like. The reason this type of data structure is used is to allow the database to efficiently store all the information for each address, without creating new rows every time an address acquire or dispose of a new token.
Repeated field are like any other field and can be selected directly. The above example was generated using the following SQL:
To work with the data within a nested field, it is necessary to first unnest the field. In the following example, all of the tokens associated with this address have been "flatten" into a normal table.
Once the the columns has been unnested, operations can be performed on it like any other field.
Here are some sample queries for when working with nested fields.
Note that when the record has been unnested, the address
field is repeated for each row. This has an important implication when calculating count and sum across columns that are repeated as a result of unnesting!
It is common to create a table that contains a mixture of normal and unnested columns.
In this case, summing the incoming_nof_transactions
would provide an incorrect result as the incoming_nof_transactions
columns was repeated as a result on unnesting.
Similarly, if there are two unnested fields in the same SELECT statement, the number of rows created from unnested would be a combination of everything in the first nested field and the second nested field.
For more information, please refer to the BigQuery page on working with arrays: https://cloud.google.com/bigquery/docs/reference/standard-sql/arrays