Tobias Willi
05/30/2024, 11:48 AMJustin Beasley
05/30/2024, 2:01 PMSELECT
...
FROM
...
WHERE
...
QUALIFY
ROW_NUMBER() OVER(PARTITION BY your_key ORDER BY changed_at DESC) = 1
This will pull only the latest record for each primary key represented by your_key. Because it doesn't use grouping, or DISTINCT, it will work fine when there's nested data as well.
QUALIFY is a bit newer, but it's basically like a WHERE clause for window/analytic functions (the same way HAVING works for GROUP BY).
Note: If you're in a different SQL that doesn't support QUALIFY, you can select that window/analytic function as a column, and then filter it in an outer query using WHERE.