SQL 查询以获取活跃客户列表。活跃的是那些每周至少在 table 中一次的人

SQL query to get list of active customers. Active are those that are in the table at least once each week

我有一个 table,其中包含我们客户的使用数据 - customerID、DateofUsage。因此,任何一天,同一个 customerID 都可能出现多次。

我想找出活跃客户的列表。活跃的是那些每周至少使用该产品一次的人。

当我使用这个时:

    SELECT distinct [CustomerId], datepart(week, [Date]) FROM CustomerUsage
    group by [subscriptionid]

我得到了 CustomerID 的列表以及它们在哪一周被使用。我尝试通过这样做来扩展它:

    SELECT distinct [CustomerId], COUNT(datepart(week, [Date])) FROM CustomerUsage
    group by [subscriptionid]

但是现在计数的数字全乱了。我希望获得客户活跃的周数,如果该数字大于到目前为止的周数,我就有了我的清单。知道我做错了什么吗?

您需要在日期部分之前添加一个不同的日期,以确保您只计算客户在记录中出现的不同天数。您发布的查询计算客户出现的每一行。

SELECT distinct [CustomerId]
     , COUNT(Distinct datepart(week, [Date])) 
FROM CustomerUsage
group by [subscriptionid]