vendredi 24 juin 2016

SQL: Create new groups of Date Ranges from a Set of Date Ranges

I am trying to find an appropriate title for my question and so far it's been 30 minutes of trying... :) So far I have the following example DateFrom DateTo Amount 2015/01/01 2015/08/31 1$ 2015/01/01 2015/12/31 3$ 2015/08/01 2015/12/31 7$ For the first line we get that we have 0.125$/month (1$/8 months) For the 2nd line we get that we have 0.25$/month (3$/12 months) For the 3d line we get that we have 1.4$/month (7$/5 months) Considering the above, we would like to create a new group of date ranges in order to have the sums of the amounts. Something like the results below: DateFrom DateTo Amount 2015/01/01 2015/07/31 (0.125$+0.25$)*7 =2.625$ 2015/08/01 2015/08/31 (1.4$+0.125$)*1 =1.775$ 2015/09/01 2015/12/31 (1.4$+0.25$)*4 =6.6$ The sum of the above is 11$ just like the original data. The result we want is actually the sum of the amount per unique group of date ranges. Is this possible to be achieved with SQL?

Aucun commentaire:

Enregistrer un commentaire