Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformers - Add FIRST/LAST functions that ignore nulls #99

Merged
merged 3 commits into from
Aug 15, 2024

Conversation

marcus-snx
Copy link
Collaborator

@marcus-snx marcus-snx commented Aug 15, 2024

In Postgres there is no IGNORE NULLS for window functions (unlike e.g. Oracle or BigQuery), but they can be recreated using aggregates. Using these simplifies the queries a bit without impacting speed.

The code below takes the last non-null value in the window. The coalesce is there in case there are nulls at the beginning of the ordering.

coalesce(
    last(debt) over (
        partition by dim.collateral_type, dim.pool_id 
        order by dim.ts
        rows between unbounded preceding and current row
    ),
    0
) as debt

@marcus-snx marcus-snx requested a review from Tburm August 15, 2024 16:03
@Tburm Tburm merged commit ff804e8 into main Aug 15, 2024
1 of 2 checks passed
@Tburm Tburm deleted the transformers/add-null-ignoring-agg-functions branch August 15, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants