@@ -37,20 +37,24 @@ select
3737 )::numeric as total_late_fees,
3838 count (* ) filter (where s .status = ' overdue' )
3939 as overdue_count,
40+ min (s .due_date )
41+ filter (where s .status in (' scheduled' , ' partially_paid' , ' overdue' ))
42+ as next_due_date,
43+ (array_agg(s .emi_amount order by s .due_date )
44+ filter (where s .status in (' scheduled' , ' partially_paid' , ' overdue' )))[1 ]
45+ as next_emi_amount,
46+ -- New columns are APPENDED at the end (after next_emi_amount) so that
47+ -- `create or replace view` succeeds on databases that already have the 040
48+ -- view: Postgres only allows appending columns, never reordering/renaming
49+ -- existing ones (else 42P16). Order is irrelevant to callers (select by name).
4050 count (* ) filter (
4151 where s .status in (' scheduled' , ' partially_paid' , ' overdue' )
4252 and s .due_date < (now() at time zone ' Asia/Kolkata' )::date
4353 ) as past_due_count,
4454 min (s .due_date ) filter (
4555 where s .status in (' scheduled' , ' partially_paid' , ' overdue' )
4656 and s .due_date < (now() at time zone ' Asia/Kolkata' )::date
47- ) as oldest_past_due_date,
48- min (s .due_date )
49- filter (where s .status in (' scheduled' , ' partially_paid' , ' overdue' ))
50- as next_due_date,
51- (array_agg(s .emi_amount order by s .due_date )
52- filter (where s .status in (' scheduled' , ' partially_paid' , ' overdue' )))[1 ]
53- as next_emi_amount
57+ ) as oldest_past_due_date
5458from public .loans l
5559left join public .loan_emi_schedule s on s .loan_id = l .id
5660where l .repayment_model = ' emi'
0 commit comments