每月过去12个月的数据,后12个月的数据

时间:2019-07-11 11:05:11

标签: tsql

这是TSQL,我正在尝试计算最近12个月的重复购买率。这可以通过查看过去12个月内购买次数超过1次的客户总数以及最近12个月内的客户总数来实现。 下面的SQL代码将给我这些;但我想在过去的12个月中动态地进行此操作。这是我受困的部分,而不是如何最好地实现这一目标。 每个月应包含可追溯到12个月的数据。即6月应保留2018年6月至2018年6月之间的数据,5月应保留2018年5月至2019年5月之间的数据。

[订购日期]是正常的日期字段(yyyy-mm-dd hh:mm:ss)

DECLARE @startdate1 DATETIME
DECLARE @enddate1 DATETIME

SET @enddate1 = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) -- Starting June 2018
SET @startdate1 = DATEADD(mm,DATEDIFF(mm,0,GETDATE())-13,0) -- Ending June 2019
;

with dataset as (
select [Phone No_] as who_identifier,
count(distinct([Order No_])) as mycount
from [MyCompany$Sales Invoice Header]
where [Order Date] between @startdate1 and @enddate1
group by [Phone No_]
),

frequentbuyers as (
select who_identifier, sum(mycount) as frequentbuyerscount
from dataset
where mycount > 1
group by who_identifier),

allpurchases as (
select who_identifier, sum(mycount) as allpurchasescount
from dataset
group by who_identifier
)

select sum(frequentbuyerscount) as frequentbuyercount, (select sum(allpurchasescount) from allpurchases) as allpurchasecount
from frequentbuyers

我希望最终结果看起来像这样: ...每个月的12月,1月,2月,3月,4月,5月,6月都保存着频繁购买者数量和所有购买者数量的值。

1 个答案:

答案 0 :(得分:0)

这是代码。我对经常购买者帐户和所有购买者帐户进行了一些修改。如果您使用类似sumif的表达式,则不需要第二个cte。

if object_id('tempdb.dbo.#tmpMonths') is not null drop table #tmpMonths
create table #tmpMonths ( MonthID datetime, StartDate datetime, EndDate datetime)

declare @MonthCount int = 12
declare @Month datetime = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)

while @MonthCount > 0 begin
    insert into #tmpMonths( MonthID, StartDate, EndDate )
    select @Month, dateadd(month, -12, @Month), @Month
    set @Month = dateadd(month, -1, @Month)
    set @MonthCount = @MonthCount - 1
end


;with dataset as (
    select m.MonthID as MonthID, [Phone No_] as who_identifier,
    count(distinct([Order No_])) as mycount
    from [MyCompany$Sales Invoice Header]
    inner join #tmpMonths m on [Order Date] between m.StartDate and m.EndDate
    group by m.MonthID, [Phone No_]
    ),
buyers as (
    select MonthID, who_identifier
        , sum(iif(mycount > 1, mycount, 0)) as frequentbuyerscount --sum only if count > 1
        , sum(mycount) as allpurchasescount
    from dataset
    group by MonthID, who_identifier
    )
select 
    b.MonthID
    , max(tm.StartDate) StartDate, max(tm.EndDate) EndDate
    , sum(b.frequentbuyerscount) as frequentbuyercount
    , sum(b.allpurchasescount) as allpurchasecount
from buyers b inner join #tmpMonths tm on tm.MonthID = b.MonthID
group by b.MonthID

请注意,该代码仅在语法方面进行了测试。

测试数据之后,结果如下:

 MonthID   | StartDate  | EndDate    | frequentbuyercount | allpurchasecount
-----------------------------------------------------------------------------
2018-08-01 | 2017-08-01 | 2018-08-01 | 340                | 3702
2018-09-01 | 2017-09-01 | 2018-09-01 | 340                | 3702
2018-10-01 | 2017-10-01 | 2018-10-01 | 340                | 3702
2018-11-01 | 2017-11-01 | 2018-11-01 | 340                | 3702
2018-12-01 | 2017-12-01 | 2018-12-01 | 340                | 3703
2019-01-01 | 2018-01-01 | 2019-01-01 | 340                | 3703
2019-02-01 | 2018-02-01 | 2019-02-01 | 2                  | 8
2019-03-01 | 2018-03-01 | 2019-03-01 | 2                  | 3
2019-04-01 | 2018-04-01 | 2019-04-01 | 2                  | 3
2019-05-01 | 2018-05-01 | 2019-05-01 | 2                  | 3
2019-06-01 | 2018-06-01 | 2019-06-01 | 2                  | 3
2019-07-01 | 2018-07-01 | 2019-07-01 | 2                  | 3
相关问题