再帰的な共通テーブル式を使用する必要があると思います。
編集:再帰的な共通テーブル式を使用する代わりに、手続き型の実装をさらに下に追加しました。私が含めた再帰的な CTE クエリではおそらく処理できないデータ シナリオが多数あると思われるため、その手続き型アプローチを使用することをお勧めします。
以下のクエリは、提供されたシナリオに対する正しい回答を提供しますが、おそらく、いくつかの追加の複雑なシナリオを考え出し、バグがあるかどうかを確認する必要があります。
たとえば、以前の複数の注文が後の注文と重複している場合、このクエリが機能しなくなる可能性があると感じています。
with CurrentOrders (UserId, SubscriptionDays, StartDate, EndDate) as
(
select
userid,
sum(subscriptiondays),
min(orderdate),
dateadd(day, sum(subscriptiondays), min(orderdate))
from #orders
where
#orders.orderdate <= @currentdate
-- start with the latest order(s)
and not exists (
select 1
from #orders o2
where
o2.userid = #orders.userid
and o2.orderdate <= @currentdate
and o2.orderdate > #orders.orderdate
)
group by
userid
union all
select
#orders.userid,
#orders.subscriptiondays,
#orders.orderdate,
dateadd(day, #orders.subscriptiondays, #orders.orderdate)
from #orders
-- join any overlapping orders
inner join CurrentOrders on
#orders.userid = CurrentOrders.UserId
and #orders.orderdate < CurrentOrders.StartDate
and dateadd(day, #orders.subscriptiondays, #orders.orderdate) > CurrentOrders.StartDate
)
select
UserId,
sum(SubscriptionDays) as TotalSubscriptionDays,
min(StartDate),
sum(SubscriptionDays) - datediff(day, min(StartDate), @currentdate) as RemainingSubscriptionDays
from CurrentOrders
group by
UserId
;
Philip は、共通テーブル式の再帰制限に関する懸念について言及しました。以下は、テーブル変数と while ループを使用した手続き型の代替手段であり、同じことを達成すると私は信じています。
この代替コードが機能することを確認しましたが、少なくとも提供されたサンプル データについては、このアプローチに関するコメントをお待ちしております。良いアイデア?悪いアイデア?注意すべき懸念事項はありますか?
declare @ModifiedRows int
declare @CurrentOrders table
(
UserId int not null,
SubscriptionDays int not null,
StartDate date not null,
EndDate date not null
)
insert into @CurrentOrders
select
userid,
sum(subscriptiondays),
min(orderdate),
min(dateadd(day, subscriptiondays, orderdate))
from #orders
where
#orders.orderdate <= @currentdate
-- start with the latest order(s)
and not exists (
select 1
from #orders o2
where
o2.userid = #orders.userid
and o2.orderdate <= @currentdate
-- there does not exist any other order that surpasses it
and dateadd(day, o2.subscriptiondays, o2.orderdate) > dateadd(day, #orders.subscriptiondays, #orders.orderdate)
)
group by
userid
set @ModifiedRows = @@ROWCOUNT
-- perform an extra update here in case there are any additional orders that were made after the start date but before the specified @currentdate
update co set
co.SubscriptionDays = co.SubscriptionDays + #orders.subscriptiondays
from @CurrentOrders co
inner join #orders on
#orders.userid = co.UserId
and #orders.orderdate <= @currentdate
and #orders.orderdate >= co.StartDate
and dateadd(day, #orders.subscriptiondays, #orders.orderdate) < co.EndDate
-- Keep attempting to update rows as long as rows were updated on the previous attempt
while(@ModifiedRows > 0)
begin
update co set
SubscriptionDays = co.SubscriptionDays + overlap.subscriptiondays,
StartDate = overlap.orderdate
from @CurrentOrders co
-- join any overlapping orders
inner join (
select
#orders.userid,
sum(#orders.subscriptiondays) as subscriptiondays,
min(orderdate) as orderdate
from #orders
inner join @CurrentOrders co2 on
#orders.userid = co2.UserId
and #orders.orderdate < co2.StartDate
and dateadd(day, #orders.subscriptiondays, #orders.orderdate) > co2.StartDate
group by
#orders.userid
) overlap on
overlap.userid = co.UserId
set @ModifiedRows = @@ROWCOUNT
end
select
UserId,
sum(SubscriptionDays) as TotalSubscriptionDays,
min(StartDate),
sum(SubscriptionDays) - datediff(day, min(StartDate), @currentdate) as RemainingSubscriptionDays
from @CurrentOrders
group by
UserId
EDIT2: 上記のコードにいくつかの調整を加えて、さまざまな特殊なケースに対処しました。たとえば、同じ日付で終了するユーザーの 2 つの注文が発生した場合などです。
たとえば、セットアップ データを次のように変更すると、元のコードで問題が発生しましたが、これを修正しました。
insert into #orders
select 1, 2, 10, '2011-01-01'
union
select 2, 1, 10, '2011-01-10'
union
select 3, 1, 10, '2011-01-15'
union
select 4, 2, 6, '2011-01-15'
union
select 5, 2, 4, '2011-01-17'
EDIT3: 他の特殊なケースに対処するために、いくつかの追加調整を行いました。特に、以前のコードでは、次のセットアップ データで問題が発生しました。これを修正しました。
insert into #orders
select 1, 2, 10, '2011-01-01'
union
select 2, 1, 6, '2011-01-10'
union
select 3, 1, 10, '2011-01-15'
union
select 4, 2, 10, '2011-01-15'
union
select 5, 1, 4, '2011-01-12'