我有一个包含客户、锚定日期(原始销售日期)、呼叫频率(7,14 或 21 天)的表。我需要使用客户特定的锚日期和频率来创建未来的通话记录并将其添加到表中,直到定义的结束日期。我正在使用 SQL 2008
问问题
21 次
1 回答
0
这看起来像一个开始:
declare @Customers as Table ( CustomerId Int Identity, AnchorDate Date, CallFrequency Int );
insert into @Customers ( AnchorDate, CallFrequency ) values
( '20130101', 7 ), ( '20130102', 14 ), ( '20130103', 21 ),
( '20130104', 7 ), ( '20130105', 14 ), ( '20130106', 21 ),
( '20130107', 7 ), ( '20130108', 14 ), ( '20130109', 21 );
select *
from @Customers
declare @EndDate as Date = '20131231';
declare @Today as Date = SysDateTime();
with FutureCalls as (
select CustomerId, DateAdd( day, ( DateDiff( day, AnchorDate, @Today ) / CallFrequency + 1 ) * CallFrequency, AnchorDate ) as CallDate, CallFrequency
from @Customers
union all
select CustomerId, DateAdd( day, CallFrequency, CallDate ), CallFrequency
from FutureCalls
where DateAdd( day, CallFrequency, CallDate ) <= @EndDate
)
select *
from FutureCalls
order by CallDate, CustomerId
option ( MaxRecursion 0 )
于 2013-05-08T02:29:21.067 回答