add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE T
([CustomerKey] int, [SubscriptionKey] int, [StartDate] datetime, [EndDate] datetime, [Value] int);
INSERT INTO T
([CustomerKey], [SubscriptionKey], [StartDate], [EndDate], [Value])
VALUES
(385884, 1256, '2020-01-17 00:00:00', '2025-06-17 00:00:00', 200),
(385884, 2346, '2020-03-11 00:00:00', '2022-06-10 00:00:00', 400),
(385884, 5433, '2022-10-17 00:00:00', '2024-07-17 00:00:00', 500);
3 rows affected
select *
from T t1 cross apply (
select sum(Value) from T t2
where t2.CustomerKey = t1.CustomerKey
and t1.EndDate between t2.StartDate and t2.EndDate
) v(ValueAtEndDate);
CustomerKey SubscriptionKey StartDate EndDate Value ValueAtEndDate
385884 1256 2020-01-17 00:00:00.000 2025-06-17 00:00:00.000 200 200
385884 2346 2020-03-11 00:00:00.000 2022-06-10 00:00:00.000 400 600
385884 5433 2022-10-17 00:00:00.000 2024-07-17 00:00:00.000 500 700