识别客户的唯一访问

时间:2014-11-04 16:14:30

标签: sql sql-server

我在SQL Server工作,我有一个CustomerID,VisitDate,Sales和Quantity表。 我试图通过访问来计算平均支票,因此客户在他/她的第一次重复购买,第二次重复购买等时购买了多少。此外,每次购买之间需要花费多少时间。

任何指导都会有所帮助, 谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用row_number()和条件聚合。这是一个例子:

select customerid, avg(sales) as avgsales,
       max(case when segnum = 1 then sales end) as sales_01,
       max(case when segnum = 2 then sales end) as sales_02,
       max(case when segnum = 3 then sales end) as sales_03
from (select t.*, row_number() over (partition by customerid order by visitdate) as segnum
      from table t
     ) t
group by customerid;

顺便说一下,你的问题确实应该包括样本数据,期望的结果和表格布局,