I am migrating some queries from PostgreSQL dialect over to BigQuery. One nice pattern in PostgreSQL is DISTINCT ON (key), which returns the first row for every key based on the sequence as defined in the ORDER BY section.
Is there a similar pattern I can use in BigQuery that would avoid wrapping the query as a CTE?
PostgreSQL query:
SELECT
DISTINCT ON (user_id)
user_id,
updated_on,
net_change
FROM
table_name
ORDER BY
user_id, updated_on DESC
The BigQuery equivalent of the PostgreSQL dialect's
DISTINCT ON (key)is theQUALIFYclause with a window function: