We have a dataset of contracts, with columns indicating counterparty, value, start date, and end date.
We are looking for a summary of total contract value, per counterparty, per calendar year.
Before we could apply a GROUP BY to the data, we would need a calculated column for each calendar year, with the contract value assigned pro rata.
Example: start date 30/06/2015, end date 31/12/2017, contract value €500.000
- the contract is about 2,5 years, so a value of €200.000 [€500.000 / 2,5] is allocated (pro rata) per year.
- in the year 2015, the value is for half a year, so is assigned about €100.000
- in the year 2016, the value is for a full year, so is assigned about €200.000
- in the year 2017, idem, so the value assigned is about €200.000
(The values in the example are not exact but simplified for illustrative purposes, as the time between 30/06/2015 and 31/12/2015 for instance is not exactly half a year, but this goes beyond the purpose of the issue)
There is no fixed contract length; some span years, others are daily or hourly (the dates are of data type 'timestamp').
How can we do this efficiently, without having to write a select clause for every single calendar year? The start and end dates namely span several decades.
The Oracle version is 19c.