每周在SQL Server中获取新客户

时间:2014-11-13 21:06:05

标签: sql sql-server

我有一些与客户相关的数据以及他们每天执行的交易数量。我想看看有多少" new"我们每周都会得到客户。数据如下所示:

Custnum    Created    Revenue
1    2014/10/23    30
4    2014/10/23    20
5    2014/10/23    40
2    2014/10/30    13
3    2014/10/30    45
1    2014/10/30    56

在上面的(示例)数据中,我们可以看到custnum 1的客户连续几周都有交易,我只想要下周的新客户,那些从未与我们开展业务的客户过去。换句话说,我想要每周全新客户的数量。所以结果应该是:

CustCount    Created
3    2014/10/23
2    2014/10/30

我尝试使用以下查询:

select 
    count(distinct custnum), 
    DATEADD(wk, DATEDIFF(wk, 0, created), 0) as Date
from ORDERS
where created > '2013-01-01'
group by 
    DATEADD(wk, DATEDIFF(wk, 0, created), 0)
order by
    DATEADD(wk, DATEDIFF(wk, 0, created), 0)

但是这个查询给了我每周唯一客户的数量,我想要每周新客户的数量。

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

听起来您想要使用只有每个客户的第一个订单日期的ORDERS子集。

select 
    count(custnum), 
    DATEADD(wk, DATEDIFF(wk, 0, created), 0) as Date
from
    (Select custnum, min(created) as created From Orders Group by custnum) o
where created > '2013-01-01'
group by 
    DATEADD(wk, DATEDIFF(wk, 0, created), 0)
order by
    DATEADD(wk, DATEDIFF(wk, 0, created), 0)

答案 1 :(得分:3)

我对你的问题的理解

我接受了这些陈述:

  • 我想看看有多少" new"我们每个星期都有客户。
  • [客户]过去从未与我们做过生意
  • 我想要每周全新客户的数量

你想要吗

 CustCount    Created
 2            week1    -- customer 1 and 2
 1            week2    -- customer 3
 2            week3    -- customer 4 and 5
 -- Option A
 1            week4    -- customer 6 is new and 2 was not counted 
 -- or Option B
 2            week4    -- customer 6 and 2; 
                       -- since customer 2 did not order anything in week3

选项A

此查询SELECT Custnum, DATEPART ( week , created) as WeekNumber from Revenues Order by Custnum返回提供的示例数据的输出

Custnum    WeekNumber 
1          31       -- counts
1          44       -- does not count, since customer already ordered once
2          36       -- counts
3          36       -- counts
3          44       -- does not count
4          43       -- counts
5          43       -- counts
5          45       -- does not count

第一步:过滤记录

要获得客户(新客户)的第一条记录,您可以执行以下操作:

SELECT Distinct Custnum, Min(Created) as Min_Created 
FROM Revenues 
GROUP BY Custnum

第二步:按周计算和分组

首先我使用grouping customer orders by week中的sql,你可以在old sqlfiddle找到它。但后来我决定使用

Select Count(Custnum) as CountCust 
    , DATEPART(week, Min_Created) as Week_Min_Created 
FROM (
        SELECT Distinct Custnum, Min(Created) as Min_Created  
        FROM Revenues Group By Custnum
    ) sq Group by DATEPART(week, Min_Created)

在我的上返回

CountCust  Week_Min_Created
1          31    -- only customer 1
2          36    -- customer 2 and 3
2          43    -- customer 4 and 5
-- nothing for week 45 since customer 5 was already counted

一些示例数据

这是我使用的样本数据

CREATE TABLE Revenues 
(
   Custnum int , 
   Created datetime,
   Revenue int 
);

INSERT INTO Revenues (Custnum, Created, Revenue)
VALUES
  (1, '20140801', 30),
  (2, '20140905', 13), (3, '20140905', 45),
  (4, '20141023', 20), (5, '20141023', 40),
  (3, '20141030', 45), (1, '20141030', 56),
  (5, '20141106', 60);

答案 2 :(得分:0)

您可以添加一个where子句,以便在一周之前不存在custnum,例如

custnum not in (select custnum from orders where created < (start date of week))

伪代码,因为我对sql server函数并不熟悉

答案 3 :(得分:0)

更好的设计是存储客户注册的日期以及其他信息。 完成后,您可以使用以下链接中的解决方案。

http://sqlfiddle.com/#!12/2caaa/1