【发布时间】: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