【问题标题】:SQL query to get list of active customers. Active are those that are in the table at least once each week获取活跃客户列表的 SQL 查询。活跃的是那些每周至少出现一次的表
【发布时间】:2015-06-20 02:29:16
【问题描述】:

我有一个表,其中包含我们客户的使用数据 - 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]

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

【问题讨论】:

  • 你标记这个'mysql'是因为?
  • 将标签改为sql。
  • 你用的是什么数据库?
  • 我正在使用 SQL Azure
  • @Anurag 这应该为您提供客户活跃的周数。 select [CustomerId], count(weekactive) from ( SELECT distinct [CustomerId], datepart(week, [Date]) as weekactive FROM CustomerUsage group by [subscriptionid] ) group by [CustomerId]

标签: sql tsql azure-sql-database


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多