Hi,
As I asked in the comment, more data and expected result can help to clarify the scenario and your needs.
I am not sure that I covered all cases but I think that this query is what you need or at least the base of what you need (we might need to cover more cases in the lase CASE statement once I will be sure that |I am on the right track - meaning that I understood what you need
Please check the query and if this does not cover all cases and you do not succeed the use it for the final query (please try since it should provide all the tricks that you need) then please re-read my comment and add the missing information
;With MyCTE as (
SELECT
[invoice],[instalment_id],
[current_instalment_price],
[running_sum_price] = SUM ([current_instalment_price]) OVER (PARTITION BY [invoice] ORDER BY [instalment_id] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
[previous_running_sum_price] = ISNULL(SUM ([current_instalment_price]) OVER (PARTITION BY [invoice] ORDER BY [instalment_id] ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0)
from _instalments
)
,MyCTE2 as (
SELECT
t1.[invoice],t1.[instalment_id],t1.[current_instalment_price],t1.[running_sum_price], t1.[previous_running_sum_price],
t2.low_border, t2.up_border, t2.operation_serial, t2.payment_type
FROM MyCTE t1
LEFT JOIN _payments t2
ON t1.[invoice] = t2.[invoice]
AND t1.[running_sum_price] >= t2.low_border and t1.previous_running_sum_price <= t2.up_border
)
SELECT
t1.[invoice],t1.[instalment_id],t1.[current_instalment_price],t1.[running_sum_price], t1.[previous_running_sum_price],
t1.operation_serial, t1.low_border, t1.up_border,t1.payment_type,
The_Paymet = --t1.[running_sum_price] - t1.up_border
CASE
WHEN t1.[running_sum_price] > t1.up_border THEN t1.up_border
WHEN t1.[running_sum_price] <= t1.up_border THEN t1.[running_sum_price] - t1.low_border
END
FROM MyCTE2 t1
Check the last column in the result which provide the solution
Or maybe this is what you need (as I said it is only playing with the lase CASE stetement to cover all cases and to final adjasment to what exactly you need)
;With MyCTE as (
SELECT
[invoice],[instalment_id],
[current_instalment_price],
[running_sum_price] = SUM ([current_instalment_price]) OVER (PARTITION BY [invoice] ORDER BY [instalment_id] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
[previous_running_sum_price] = ISNULL(SUM ([current_instalment_price]) OVER (PARTITION BY [invoice] ORDER BY [instalment_id] ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0)
from _instalments
)
,MyCTE2 as (
SELECT
t1.[invoice],t1.[instalment_id],t1.[current_instalment_price],t1.[running_sum_price], t1.[previous_running_sum_price],
t2.low_border, t2.up_border, t2.operation_serial, t2.payment_type
FROM MyCTE t1
LEFT JOIN _payments t2
ON t1.[invoice] = t2.[invoice]
AND t1.[running_sum_price] >= t2.low_border and t1.previous_running_sum_price <= t2.up_border
)
SELECT
t1.[invoice],t1.[instalment_id],t1.[current_instalment_price],t1.[running_sum_price], t1.[previous_running_sum_price],
t1.operation_serial, t1.low_border, t1.up_border,t1.payment_type,
The_Paymet = --t1.[running_sum_price] - t1.up_border
CASE
WHEN t1.[running_sum_price] > t1.up_border THEN t1.up_border
WHEN t1.[running_sum_price] <= t1.up_border and t1.previous_running_sum_price <= t1.low_border THEN t1.[current_instalment_price] - t1.low_border
else t1.[current_instalment_price]
END
FROM MyCTE2 t1
GO