Skip to content
Advertisement

Percentage of tardiness and first date for defaults in postgresql

I have a table where I register a debt and the paid date:

What I want is to aggregate this data per debt_id, payments (the quantity of payments per debt_id), tardiness (if the paid_date > due_date), the first due_date per debt_id and the percentage that each debt was late. This table should give the idea:

So I tried this so far:

I get an error where it says I need an OVER clause, which means that I need a partition but I’m not sure how to combine GROUP BY and PARTITION. Any help will be greatly appreciated.

Advertisement

Answer

Aggregation seems appropriate:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement