Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Tricks on ANY_VALUE with HAVING MAX in GoogleSQL

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Overview and Dialect Availability

In GoogleSQL (formerly ZetaSQL, used by BigQuery and Cloud Spanner), ANY_VALUE(c1 HAVING MAX ts) returns the value of c1 from a row where ts attains its maximum value within each group (and HAVING MIN ts does the same for the minimum). BigQuery also provides MAX_BY(c1, ts) and MIN_BY(c1, ts) as direct synonyms for ANY_VALUE(c1 HAVING MAX ts) and ANY_VALUE(c1 HAVING MIN ts).

If multiple rows tie for the maximum ts, GoogleSQL skips rows where c1 is NULL (as if IGNORE NULLS were set) and nondeterministically picks one of the remaining tied rows.

Engine / DialectEquivalent Function
GoogleSQL / BigQueryANY_VALUE(x HAVING MAX y) or MAX_BY(x, y)
DuckDB / Spark SQL / Trino / SnowflakeMAX_BY(x, y)
ClickHouseargMax(x, y)
PostgreSQLDISTINCT ON, ORDER BY y DESC LIMIT 1, or FIRST_VALUE

Watch Out for NULL in ts

When using ANY_VALUE(c1 HAVING MAX ts), be careful if ts can be NULL:

  1. Rows with ts IS NULL are ignored by HAVING MAX: HAVING MAX ts evaluates the maximum over non-NULL values of ts. If all rows in a group have ts IS NULL, the result is NULL.

  2. A NULL c1 on the winning row(s) still returns NULL: HAVING MAX ts narrows the candidate rows to those with the maximum ts first, and ANY_VALUE selects from that restricted pool (preferring non-NULL c1 values if any exist among the tied rows). If every row tied at the maximum ts has c1 IS NULL, the function returns NULL rather than falling back to an earlier row with a non-NULL c1.

  3. Guarding NULL timestamps: If you want NULL timestamps treated as the earliest possible time (so non-NULL timestamps always win, while still allowing a NULL-timestamp row to be picked if no non-NULL timestamp exists), use COALESCE with a minimum sentinel timestamp such as TIMESTAMP '0001-01-01 00:00:00 UTC':

    SELECT
      id,
      ANY_VALUE(c1 HAVING MAX COALESCE(ts, TIMESTAMP '0001-01-01 00:00:00 UTC')) AS latest_c1
    FROM events
    GROUP BY id;

    If NULL timestamps should be excluded altogether, filter them out with WHERE ts IS NOT NULL.

Both Arguments Can Be Expressions: As-Of Timestamp t Tricks

Both c1 and ts in ANY_VALUE(c1 HAVING MAX ts) can be arbitrary expressions. This makes it possible to compute the latest value up to a cutoff timestamp t (ts <= t) in a single GROUP BY pass without a separate subquery or join.

Trick 1: Sentinel Timestamp vs. NULL for ts > t

You can demote rows after t by mapping their having_expression to a sentinel minimum timestamp that does not appear in your data:

-- Caution: if ALL rows in a group have ts > t, every row ties on the sentinel timestamp
-- and ANY_VALUE(c1 ...) will leak a post-t row's c1!
ANY_VALUE(c1 HAVING MAX IF(ts <= t, ts, TIMESTAMP '0001-01-01 00:00:00 UTC'))

Notice the edge case: if a group has no rows with ts <= t, every row gets the sentinel timestamp 0001-01-01 00:00:00 UTC, so ANY_VALUE will still pick one of the ts > t rows.

Alternatively, mapping post-t timestamps to NULL causes HAVING MAX to ignore them completely and return NULL when no row satisfies ts <= t:

ANY_VALUE(c1 HAVING MAX IF(ts <= t, ts, NULL))

Trick 2: Transform Both Value and Timestamp Expressions

To prevent leaking a post-t value when using a sentinel timestamp—or when you want an explicit non-NULL default (such as 0) whenever no row satisfies ts <= t—transform both arguments:

  1. Map timestamps > t to a smaller sentinel timestamp that will not appear in the database.

  2. Map values c1 corresponding to timestamps > t to NULL or a default/smallest value.

Note that when the masked value is NULL, this produces the same result as Trick 1’s IF(ts <= t, ts, NULL); Trick 2 is mainly useful when you want an explicit non-NULL fallback value such as 0.

SELECT
  id,
  ANY_VALUE(
    IF(ts <= t, c1, NULL)
    HAVING MAX IF(ts <= t, ts, TIMESTAMP '0001-01-01 00:00:00 UTC')
  ) AS c1_as_of_t,
  ANY_VALUE(
    IF(ts <= t, val, 0)
    HAVING MAX IF(ts <= t, ts, TIMESTAMP '0001-01-01 00:00:00 UTC')
  ) AS val_as_of_t_or_zero
FROM events
GROUP BY id;

Even if every row in a group has ts > t and ties on the sentinel timestamp, all candidate values in that tied pool have already been masked to NULL (or 0), so no post-t data can leak through.

Comparison with Window Functions (ROW_NUMBER / QUALIFY)

References