Recently we started using temporal tables feature in MSSQL. Now our analytics department wants to write query aggregating data on per month basis.
Let's say we have table
+----+-------+------------+------------+ | Id | Value | BeginDate | EndDate | +------------+------------+------------+ | 1 | 10 | 2019-12-24 | 9999-12-31 | +------------+------------+------------+ | 1 | 20 | 2019-12-05 | 2019-12-24 | +------------+------------+------------+ | 1 | 40 | 2019-11-05 | 2019-12-05 | +----+-------+------------+------------+
And now I would like to know average value of Value
on per month basis. Is that even possible with MSSQL temporal tables? Is it possible to somehow calculate those values (for example avg(5/31 * 40 + 19/31 * 20 + 7/31 * 10)
for December) automatically and then return them in following format
+----+-------+-------+------+ | Id | Avg | Month | Year | +------------+--------------+ | 1 | 15,16 | 12 | 2019 | +----+-------+-------+------+