你有一个有趣的问题。从根本上说,它是关于从一年到下一年花费五分之一的迁移。我会通过查看这三年的所有五分之一来解决这个问题,看看人们在哪里移动。
首先是按年份和电子邮件汇总数据。关键功能是ntile()
。老实说,我经常使用row_number()
and自己进行计算count()
,这就是为什么它们在 CTE 中(但随后没有使用):
with YearSummary as (
select year(OrderDate) as yr, o.BillEmail, SUM(o.total) as TotalSpent,
count(o.OrderID) as TotalOrders,
row_number() over (partition by year(OrderDate) order by sum(o.Total) desc) as seqnum,
count(*) over (partition by year(OrderDate)) as NumInYear,
ntile(5) over (partition by year(OrderDate) order by sum(o.Total) desc) as Quintile
from dbo.tblOrder o with (nolock)
where o.DomainProjectID=13 and o.BillEmail not like ''
group by o.BillEmail, year(OrderDate)
)
select q2010, q2011, q2012,
count(*) as NumEmails,
min(BillEmail), max(BillEmail)
from (select BillEmail,
max(case when yr = 2010 then Quintile end) as q2010,
max(case when yr = 2011 then Quintile end) as q2011,
max(case when yr = 2012 then Quintile end) as q2012
from YearSummary
group by BillEmail
) ys
group by q2010, q2011, q2012
order by 1, 2, 3;
最后一步是为每封电子邮件获取多行并将它们组合成计数。请注意,某些电子邮件在某些年份不会有任何支出,因此它们对应的Quintile
将为 NULL(这实际上应该产生更像 180 行 - 5*6*6 - 而不是 125 行 - 5*5*5
我还在最终结果(min()
和max()
)中包含示例电子邮件,这样您就可以查看每个组的示例。
注意:对于保留率,请计算 (1, 1, 1)(所有年份的最高分)与 2010 年最高五分之一的总数之间的比率。