KatInterview

Cumulative Loss Projection Across Policy Renewals

SQL & DataAuto-graded
MediumAuto-gradedSQL & Datawindow-functionsaggregationinsurance

Last updated

Compute the projected total loss from each policy renewal onward, summing the current period's loss with all subsequent renewal periods.

You are analyzing insurance policy renewal data. Each policy can renew multiple times, and each renewal period records the loss incurred during that period.

For each renewal period, calculate the projected cumulative loss: the sum of losses starting from that period through all later renewals of the same policy.

You have the following table structure:

CREATE TABLE policy_term_losses (
    policy_id VARCHAR,        -- Policy identifier
    term_id VARCHAR,          -- Renewal period identifier
    term_sequence INTEGER,    -- Chronological position (higher = later)
    term_start_date DATE,
    term_end_date DATE,
    loss_amount DECIMAL(18, 2) -- Loss recorded in this period
);

Given constraints:

  • Each term_id is associated with exactly one policy_id
  • Within a policy, term_sequence defines the chronological order
  • Each (policy_id, term_sequence) pair appears at most once
  • loss_amount may be negative (adjustments/recoveries), positive, or zero
  • Calculations depend on sequence order, not dates

Required output: Return all renewal periods with these columns:

  • policy_id
  • term_id
  • term_sequence
  • loss_amount
  • projected_cumulative_loss — sum of loss_amount from this period forward

Example:

Input for policy P1:

policy_idterm_idterm_sequenceloss_amount
P1T11100
P1T2250
P1T3325

Expected output:

policy_idterm_idterm_sequenceloss_amountprojected_cumulative_loss
P1T11100175
P1T225075
P1T332525

Write a SQL query to produce this result.

Hints

Hint 1

This is a reverse running total—start from the last period and accumulate backward.

Hint 2

Window functions with custom frame specifications can compute sums over a range relative to the current row.

Hint 3

Partition by policy so calculations don't cross policies; order matters for defining the accumulation range.