← Files SQL CopilotARCHIVED FILE
references/windows-and-dedup.md
925 Bytes · Nov 30, 2026 · 06:40 UTC
# Window functions, top-N and deterministic winners Use `ROW_NUMBER()` to choose exactly one row per partition, `RANK()` when ties should share a rank with gaps, and `DENSE_RANK()` when ties should share a rank without gaps. A request for "top three" may mean three rows or all ties at rank three; clarify or label the default. For last-write-wins CDC, `ORDER BY version DESC, source_updated_ts DESC, ingestion_ts DESC, immutable_event_id DESC` is more reliable than timestamp alone, assuming business version monotonicity. Choose tie-breaks based on actual source guarantees. A window with nonunique order may choose different winners in different runs. On platforms with `QUALIFY`, it can filter window results. Other dialects need subqueries/CTEs. Do not blindly port QUALIFY or DISTINCT ON across engines. Validate one winner per key, no missing keys, and expected handling of tombstones, late events and clock skew.
SHA-256: 5183c2a3796f6403ec302d4ad9db613c34dc76105c835b1e2bca22f41b4e689d