我想计算在7天滚动期内发布 5次或更多次的唯一身份用户数。我该怎么做?
我知道如何计算在7天滚动期内发布 1次或更多次的用户数。查询如下所示:
with PostsPerDay as (
select cast(CreationDate as Date) [Day]
, OwnerUserId [User]
, count(*) Post
from Posts
where CreationDate > '2017-07-01'
group by
cast(CreationDate as Date)
, OwnerUserId
)
select [Day], count(distinct [User]) DailyPosters, Rolling7DayCount
from PostsPerDay
outer apply (
select count(distinct [User]) Rolling7DayCount
from PostsPerDay ppd
where ppd.[Day] >= dateadd(dd, -7, PostsPerDay.[Day])
and ppd.[Day] < PostsPerDay.[Day]
) Rolling7DayCount
group by [Day], Rolling7DayCount
order by 1
Here it is at the Stack Exchange Data Explorer
理想情况下,我正在寻找四列结果:Day
,DailyPosters
,Rolling7DayCount
,Rolling7DayCount5xPosters
。 (示例查询返回前3列。)
要格外明确:我希望计算在特定日期结束的任何7天期间发布5倍的用户。因此,简单地向CTE添加Having
不会给我提供我需要的东西。
任何表现提示也会受到赞赏!
答案 0 :(得分:1)
在你的“PostsPerDay”中,CTE将其更改为:
SELECT
CAST(CreationDate AS DATE) [Day]
,OwnerUserId
,COUNT(*) Post
FROM
Posts
WHERE
CreationDate > '2017-07-01'
GROUP BY
CAST(CreationDate AS DATE)
,OwnerUserId
HAVING COUNT(*) >= 5
我只添加了“HAVING”过滤器。
答案 1 :(得分:1)
经过Stack Overflow开发人员@BenjaminHodgson的一些故障排除和一些帮助后,我发现了它。
以下是代码:
LOG_DIR
您可以在the Stack Exchange Data Explorer处看到代码。